In [1]:
require 'daru/view'
Install the spreadsheet gem version ~>1.1.1 for using spreadsheet functions.

Install the mechanize gem version ~>2.7.5 for using mechanize functions.
Out[1]:
true
In [2]:
Daru::View.plotting_library = :googlecharts
Out[2]:
:googlecharts

Basic column chart

In [14]:
idx = Daru::Index.new ["Element", "Density", "{ role: \"style\" }" ]
data_rows = [
  ["Copper", 8.94, "#b87333"],
  ["Silver", 10.49, "silver"],
  ["Gold", 19.30, "gold"],
  ["Platinum", 21.45, "color: #e5e4e2"]
]
df_element_density = Daru::DataFrame.rows(data_rows)
df_element_density.vectors = idx
df_element_density

# removing last column. For annotation need some config.
# refer : https://developers.google.com/chart/interactive/docs/gallery/columnchart
df_element_density.delete_vector "{ role: \"style\" }"
Out[14]:
Daru::DataFrame(4x2)
Element Density
0 Copper 8.94
1 Silver 10.49
2 Gold 19.3
3 Platinum 21.45
In [15]:
col_table = Daru::View::Table.new(df_element_density, height: 300, width: 200)
col_table.table
Out[15]:
In [16]:
col_options = {
  title: "Density of Precious Metals, in g/cm^3",
  width: 600,
  height: 400,
  bar: {groupWidth: "95%"},
  legend: { position: "none" },
   type: :column
}
col_chart = Daru::View::Plot.new(col_table.table, col_options)
col_chart.show_in_iruby
Out[16]:

Stacked column charts

In [17]:
idx = Daru::Index.new ['Genre', 'Fantasy & Sci Fi', 'Romance', 'Mystery/Crime', 'General',
         'Western', 'Literature'] 
data_rows = [
  ['2010', 10, 24, 20, 32, 18, 5],
  ['2020', 16, 22, 23, 30, 16, 9],
  ['2030', 28, 19, 29, 30, 12, 13]
]
df_stacked = Daru::DataFrame.rows(data_rows)
df_stacked.vectors = idx
df_stacked
Out[17]:
Daru::DataFrame(3x7)
Genre Fantasy & Sci Fi Romance Mystery/Crime General Western Literature
0 2010 10 24 20 32 18 5
1 2020 16 22 23 30 16 9
2 2030 28 19 29 30 12 13
In [19]:
col_stacked_table = Daru::View::Table.new(df_stacked, height: 300, width: 700)
col_stacked_table.table
Out[19]:
In [20]:
col_stacked_options = {
        width: 600,
        height: 400,
        legend: { position: 'top', maxLines: 3 },
        bar: { groupWidth: '75%' },
        isStacked: true,
   type: :column
}
col_stacked_chart = Daru::View::Plot.new(col_stacked_table.table, col_stacked_options)
col_stacked_chart.show_in_iruby
Out[20]:
In [22]:
col_stacked_options = {
          isStacked: true,
          height: 300,
          legend: {position: 'top', maxLines: 3},
          vAxis: {minValue: 0},
   type: :column
}
col_stacked_chart = Daru::View::Plot.new(col_stacked_table.table, col_stacked_options)
col_stacked_chart.show_in_iruby
Out[22]:
In [25]:
col_stacked_options = {
          isStacked: 'percent',
          height: 400,
          legend: {position: 'top', maxLines: 3},
          vAxis: {
            minValue: 0,
            ticks: [0, 0.3, 0.6, 0.9, 1]
          },
   type: :column
}
col_stacked_chart = Daru::View::Plot.new(col_stacked_table.table, col_stacked_options)
col_stacked_chart.show_in_iruby
Out[25]:

TODO: Material column chart using google_visualr. Refer : https://developers.google.com/chart/interactive/docs/gallery/columnchart

Dual-Y charts

In [26]:
idx = Daru::Index.new ['Galaxy', 'Distance', 'Brightness']
data_rows = [
  ['Canis Major Dwarf', 8000, 23.3],
  ['Sagittarius Dwarf', 24000, 4.5],
  ['Ursa Major II Dwarf', 30000, 14.3],
  ['Lg. Magellanic Cloud', 50000, 0.9],
  ['Bootes I', 60000, 13.1]
]
df_dualY = Daru::DataFrame.rows(data_rows)
df_dualY.vectors = idx
df_dualY
Out[26]:
Daru::DataFrame(5x3)
Galaxy Distance Brightness
0 Canis Major Dwarf 8000 23.3
1 Sagittarius Dwarf 24000 4.5
2 Ursa Major II Dwarf 30000 14.3
3 Lg. Magellanic Cloud 50000 0.9
4 Bootes I 60000 13.1
In [27]:
col_dualY_table = Daru::View::Table.new(df_dualY, height: 300, width: 700)
col_dualY_table.table
Out[27]:
In [41]:
col_dualY_options = {
          chart: {
            title: 'Nearby galaxies',
            subtitle: 'distance on the left, brightness on the right'
          },
  
#           series: {
#             0: { axis: 'distance' }, # Bind series 0 to an axis named 'distance'.
#             1: { axis: 'brightness' } # Bind series 1 to an axis named 'brightness'.
#           },
#           axes: {
#             y: {
#               distance: {label: 'parsecs'}, # Left y-axis.
#               brightness: {side: 'right', label: 'apparent magnitude'} # Right y-axis.
#             }
#           }
     type: :column
}
col_dualY_chart = Daru::View::Plot.new(col_dualY_table.table, col_dualY_options)
col_dualY_chart.show_in_iruby
Out[41]:
In [51]:
col_dualY_options = {
          width: 900,
# Fixme: below line not working. expecting :
# https://developers.google.com/chart/interactive/docs/gallery/columnchart
#           series: {
#             0: {targetAxisIndex: 0},
#             1: {targetAxisIndex: 1}
#           },
          title: 'Nearby galaxies - distance on the left, brightness on the right',
#           vAxes: {
#             # Adds titles to each axis.
#             0: {title: 'parsecs'},
#             1: {title: 'apparent magnitude'}
#           }
     type: :column
}
col_dualY_chart = Daru::View::Plot.new(col_dualY_table.table, col_dualY_options)
col_dualY_chart.show_in_iruby
Out[51]: