require 'daru/view'
true
Daru::View.plotting_library = :googlecharts
:googlecharts
idx = Daru::Index.new ['City', '2010 Population',]
data_rows = [
['New York City, NY', 8175000],
['Los Angeles, CA', 3792000],
['Chicago, IL', 2695000],
['Houston, TX', 2099000],
['Philadelphia, PA', 1526000]
]
df_city_pop = Daru::DataFrame.rows(data_rows)
df_city_pop.vectors = idx
df_city_pop
City | 2010 Population | |
---|---|---|
0 | New York City, NY | 8175000 |
1 | Los Angeles, CA | 3792000 |
2 | Chicago, IL | 2695000 |
3 | Houston, TX | 2099000 |
4 | Philadelphia, PA | 1526000 |
bar_basic_table = Daru::View::Table.new(df_city_pop)
bar_basic_table.show_in_iruby
bar_basic_options = {
title: 'Population of Largest U.S. Cities',
type: :bar,
}
bar_basic_chart = Daru::View::Plot.new(bar_basic_table.table, bar_basic_options)
bar_basic_chart.show_in_iruby
bar_basic_options = {
title: 'Population of Largest U.S. Cities',
chartArea: {width: '50%'},
hAxis: {
title: 'Total Population',
minValue: 0
},
vAxis: {
title: 'City'
},
type: :bar,
height: 400
}
bar_basic_chart = Daru::View::Plot.new(bar_basic_table.table, bar_basic_options)
bar_basic_chart.show_in_iruby
# Note: If youwant to use role, label, pattern, etc column, then directly use the google datatable
# (don't create the daru dataframe/vector, since daru dataframe vectors can be string or number. So can't be a hash )
# Refer this link for more datatable options : https://developers.google.com/chart/interactive/docs/reference
data = {
cols: [{id: 'Element', label: 'Element', type: 'string'},
{id: 'Density', label: 'Density', type: 'number'},
{type: 'string', role: "style" },
],
rows: [{c:[{v: 'Copper'}, {v: 8.94}, {v: '#b87333'}]},
{c:[{v: 'Silver'}, {v: 10.49}, {v: 'silver'}]},
{c:[{v: 'Gold'}, {v: 19.30}, {v: 'gold'}]},
{c:[{v: 'Platinum'}, {v: 21.45}, {v: '#e5e4e2'}]}]
}
bar_table = Daru::View::Table.new(data, height: 300, width: 200)
bar_table.show_in_iruby
bar_options = {
title: "Density of Precious Metals, in g/cm^3",
width: 600,
height: 400,
bar: {groupWidth: "95%"},
legend: { position: "none" },
type: :bar,
height: 400
}
bar_chart = Daru::View::Plot.new(bar_table, bar_options)
bar_chart.show_in_iruby
(pry):55: warning: key :height is duplicated and overwritten on line 59
data = {
cols: [{id: 'Element', label: 'Element', type: 'string'},
{id: 'Density', label: 'Density', type: 'number'},
{type: 'string', role: "style" },
],
rows: [
{c:[{v: 'Copper'}, {v: 8.94}, {v: 'color: gray'}]},
{c:[{v: 'Silver'}, {v: 10.49}, {v: 'color: #76A7FA'}]},
{c:[{v: 'Gold'}, {v: 19.30}, {v: 'opacity: 0.2'}]},
{c:[{v: 'Platinum'}, {v: 21.45}, {v: 'stroke-color: #703593; stroke-width: 4; fill-color: #C5A5CF'}]},
{c:[{v: 'Other'}, {v: 31.45}, {v: 'stroke-color: #871B47; stroke-opacity: 0.6; stroke-width: 8; fill-color: #BC5679; fill-opacity: 0.2'}]}
]
}
bar_table = Daru::View::Table.new(data, height: 300, width: 200)
bar_table.show_in_iruby
bar_options = {
title: "Density of Precious Metals, in g/cm^3",
width: 600,
height: 400,
bar: {groupWidth: "95%"},
legend: { position: "none" },
type: :bar,
height: 400
}
bar_chart = Daru::View::Plot.new(bar_table, bar_options)
bar_chart.show_in_iruby
(pry):81: warning: key :height is duplicated and overwritten on line 85
data = {
cols: [{id: 'Element', label: 'Element', type: 'string'},
{id: 'Density', label: 'Density', type: 'number'},
{type: 'string', role: "style" },{type: 'string', role: "annotation" }
],
rows: [{c:[{v: 'Copper'}, {v: 8.94}, {v: '#b87333'}, {v: 'Cu'}]},
{c:[{v: 'Silver'}, {v: 10.49}, {v: 'silver'}, {v: 'Ag'}]},
{c:[{v: 'Gold'}, {v: 19.30}, {v: 'gold'}, {v: 'Au'}]},
{c:[{v: 'Platinum'}, {v: 21.45}, {v: '#e5e4e2'}, {v: 'Pt'}]}]
}
bar_table = Daru::View::Table.new(data)
bar_table.show_in_iruby
bar_options = {
title: "Density of Precious Metals, in g/cm^3",
width: 600,
height: 400,
bar: {groupWidth: "95%"},
legend: { position: "none" },
type: :bar,
height: 400
}
bar_chart = Daru::View::Plot.new(bar_table, bar_options)
bar_chart.show_in_iruby
(pry):104: warning: key :height is duplicated and overwritten on line 108
data = {
cols: [{id: 'Genre', label: 'Genre', type: 'string'},
{id: 'Fantasy & Sci Fi', label: 'Fantasy & Sci Fi', type: 'number'},
{id: 'Romance', label: 'Romance', type: 'number'},
{id: 'Mystery/Crime', label: 'Mystery/Crime', type: 'number'},
{id: 'General', label: 'General', type: 'number'},
{id: 'Western', label: 'Western', type: 'number'},
{id: 'Literature', label: 'Literature', type: 'number'},
{type: 'string', role: "annotation" }
],
rows: [{c:[{v: '2010'}, {v: 10}, {v: 24}, {v: 20}, {v: 32}, {v: 18}, {v: 5}, {v: ''}]},
{c:[{v: '2020'}, {v: 16}, {v: 22}, {v: 23}, {v: 30}, {v: 16}, {v: 9}, {v: ''}]},
{c:[{v: '2030'}, {v: 28}, {v: 19}, {v: 29}, {v: 30}, {v: 12}, {v: 13}, {v: ''}]},
]
}
bar_stacked_table = Daru::View::Table.new(data)
bar_stacked_table.show_in_iruby
bar_stacked_options = {
width: 600,
height: 400,
legend: { position: 'top', maxLines: 3 },
bar: { groupWidth: '75%' },
isStacked: true,
type: :bar,
}
bar_stacked_chart = Daru::View::Plot.new(bar_stacked_table, bar_stacked_options)
bar_stacked_chart.show_in_iruby
# Same data in array of rows. Not including annotation role
data = [
['Genre', 'Fantasy & Sci Fi', 'Romance', 'Mystery/Crime', 'General',
'Western', 'Literature' ],
['2010', 10, 24, 20, 32, 18, 5],
['2020', 16, 22, 23, 30, 16, 9],
['2030', 28, 19, 29, 30, 12, 13]
]
bar_stacked_table = Daru::View::Table.new(data)
bar_stacked_table.show_in_iruby
bar_stacked_options = {
width: 600,
height: 400,
isStacked: true,
type: :bar,
}
bar_stacked_chart = Daru::View::Plot.new(bar_stacked_table, bar_stacked_options)
bar_stacked_chart.show_in_iruby
# Percent
bar_stacked_options = {
width: 600,
height: 400,
isStacked: 'percent',
type: :bar,
}
bar_stacked_chart = Daru::View::Plot.new(bar_stacked_table, bar_stacked_options)
bar_stacked_chart.show_in_iruby
# We can manage the x axis ticks
bar_stacked_options = {
width: 600,
height: 400,
isStacked: 'percent',
type: :bar,
hAxis: {
minValue: 0,
ticks: [0, 0.3, 0.6, 0.9, 1]
}
}
bar_stacked_chart = Daru::View::Plot.new(bar_stacked_table, bar_stacked_options)
bar_stacked_chart.show_in_iruby
TODO : Work on Material bar charts . It is defined in google_visualr: https://developers.google.com/chart/interactive/docs/gallery/barchart
# Same data in array of rows. Not including annotation role
data = [
['Year', 'Sales', 'Expenses', 'Profit'],
['2014', 1000, 400, 200],
['2015', 1170, 460, 250],
['2016', 660, 1120, 300],
['2017', 1030, 540, 350]
]
bar_multiple_table = Daru::View::Table.new(data)
bar_multiple_table.show_in_iruby
# We can manage the x axis ticks
bar_multiple_options = {
chart: {
title: 'Company Performance',
subtitle: 'Sales, Expenses, and Profit: 2014-2017',
},
bars: 'horizontal',
type: :bar, height: 400, widht: 400
}
bar_multiple_chart = Daru::View::Plot.new(bar_multiple_table.table, bar_multiple_options)
bar_multiple_chart.show_in_iruby
# Same data in array of rows. Not including annotation role
data = [
['Galaxy', 'Distance', 'Brightness'],
['Canis Major Dwarf', 8000, 230.3],
['Sagittarius Dwarf', 24000, 4000.5],
['Ursa Major II Dwarf', 30000, 1412.3],
['Lg. Magellanic Cloud', 50000, 120.9],
['Bootes I', 60000, 1223.1]
]
bar_multiple_table = Daru::View::Table.new(data)
bar_multiple_table.show_in_iruby
bar_multiple_options = {
width: 800,
title: 'Nearby galaxies',
subtitle: 'distance on the left, brightness on the right',
bars: 'horizontal', # Required for Material Bar Charts.
type: :bar, height: 400
}
bar_multiple_chart = Daru::View::Plot.new(bar_multiple_table.table, bar_multiple_options)
bar_multiple_chart.show_in_iruby