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
In [3]:
idx = Daru::Index.new ['President', 'Start', 'End']
data_rows = [
          [ 'Washington', DateTime.new(1789, 3, 30), DateTime.new(1797, 2, 4) ],
          [ 'Adams',      DateTime.new(1797, 2, 4),  DateTime.new(1801, 2, 4) ],
          [ 'Jefferson',  DateTime.new(1801, 2, 4),  DateTime.new(1809, 2, 4) ]
]
df_presidents = Daru::DataFrame.rows(data_rows)
df_presidents.vectors = idx
df_presidents
Out[3]:
Daru::DataFrame(3x3)
President Start End
0 Washington 1789-03-30T00:00:00+00:00 1797-02-04T00:00:00+00:00
1 Adams 1797-02-04T00:00:00+00:00 1801-02-04T00:00:00+00:00
2 Jefferson 1801-02-04T00:00:00+00:00 1809-02-04T00:00:00+00:00
In [4]:
presidents_chart = Daru::View::Plot.new(df_presidents, type: :timeline)
presidents_chart.show_in_iruby
Out[4]:

Labeling the bars

In [6]:
df_presidents['Name'] = ['George Washington', 'John Adams', 'Thomas Jefferson']
Out[6]:
["George Washington", "John Adams", "Thomas Jefferson"]
In [8]:
df_presidents['Term'] = ['1', '2', '3']
Out[8]:
["1", "2", "3"]
In [9]:
df_presidents.delete_vector('President')
Out[9]:
Daru::DataFrame(3x4)
Start End Name Term
0 1789-03-30T00:00:00+00:00 1797-02-04T00:00:00+00:00 George Washington 1
1 1797-02-04T00:00:00+00:00 1801-02-04T00:00:00+00:00 John Adams 2
2 1801-02-04T00:00:00+00:00 1809-02-04T00:00:00+00:00 Thomas Jefferson 3
In [12]:
df_presidents.order = ['Term', 'Name', 'Start', 'End']
Out[12]:
["Term", "Name", "Start", "End"]
In [14]:
df_presidents
Out[14]:
Daru::DataFrame(3x4)
Term Name Start End
0 1 George Washington 1789-03-30T00:00:00+00:00 1797-02-04T00:00:00+00:00
1 2 John Adams 1797-02-04T00:00:00+00:00 1801-02-04T00:00:00+00:00
2 3 Thomas Jefferson 1801-02-04T00:00:00+00:00 1809-02-04T00:00:00+00:00
In [13]:
presidents_label_chart = Daru::View::Plot.new(df_presidents, type: :timeline)
presidents_label_chart.show_in_iruby
Out[13]:

An advanced example

In [25]:
data = [
  [ 'Position', 'Name', 'Start' ,'End'],
  [ 'President', 'George Washington', DateTime.new(1789, 3, 30), DateTime.new(1797, 2, 4) ],
      [ 'President', 'John Adams', DateTime.new(1797, 2, 4), DateTime.new(1801, 2, 4) ],
      [ 'President', 'Thomas Jefferson', DateTime.new(1801, 2, 4), DateTime.new(1809, 2, 4) ],
      [ 'Vice President', 'John Adams', DateTime.new(1789, 3, 21), DateTime.new(1797, 2, 4)],
      [ 'Vice President', 'Thomas Jefferson', DateTime.new(1797, 2, 4), DateTime.new(1801, 2, 4)],
      [ 'Vice President', 'Aaron Burr', DateTime.new(1801, 2, 4), DateTime.new(1805, 2, 4)],
      [ 'Vice President', 'George Clinton', DateTime.new(1805, 2, 4), DateTime.new(1812, 3, 20)],
      [ 'Secretary of State', 'John Jay', DateTime.new(1789, 8, 25), DateTime.new(1790, 2, 22)],
      [ 'Secretary of State', 'Thomas Jefferson',DateTime.new(1790, 2, 22), DateTime.new(1793, 11, 30)],
      [ 'Secretary of State', 'Edmund Randolph', DateTime.new(1794, 1, 2), DateTime.new(1795, 7, 20)],
      [ 'Secretary of State', 'Timothy Pickering', DateTime.new(1795, 7, 20), DateTime.new(1800, 4, 12)],
      [ 'Secretary of State', 'Charles Lee', DateTime.new(1800, 4, 13), DateTime.new(1800, 5, 5)],
      [ 'Secretary of State', 'John Marshall', DateTime.new(1800, 5, 13), DateTime.new(1801, 2, 4)],
      [ 'Secretary of State', 'Levi Lincoln', DateTime.new(1801, 2, 5), DateTime.new(1801, 4, 1)],
      [ 'Secretary of State', 'James Madison', DateTime.new(1801, 4, 2), DateTime.new(1809, 2, 3)]
    
  ]

timeline_adv_table = Daru::View::Table.new(data, pageSize: 6)
timeline_adv_table.show_in_iruby
Out[25]:
In [26]:
position_chart = Daru::View::Plot.new(timeline_adv_table, type: :timeline)
position_chart.show_in_iruby
Out[26]:

Putting bars on one row

In [27]:
presidents_label_chart = Daru::View::Plot.new(df_presidents, type: :timeline)
presidents_label_chart.show_in_iruby
Out[27]:
In [28]:
presidents_label_chart = Daru::View::Plot.new(df_presidents, type: :timeline, timeline: { groupByRowLabel: false })
presidents_label_chart.show_in_iruby
Out[28]:
In [31]:
data = {
  cols: [{ type: 'string', id: 'Role' },
          { type: 'string', label: 'Name' },
          { type: 'date', label: 'Start' },{ type: 'date', label: 'End' }
           ],
  rows: [{c:[{v: 'President'}, {v: 'George Washington'}, {v: DateTime.new(1789, 3, 30)}, {v: DateTime.new(1797, 2, 4)}]},
           {c:[{v: 'President'}, {v: 'John Adams'}, {v: DateTime.new(1797, 2, 4)}, {v: DateTime.new(1801, 2, 4) }]},
    {c:[{v: 'President'}, {v: 'Thomas Jefferson'}, {v: DateTime.new(1801, 2, 4)}, {v: DateTime.new(1809, 2, 4)}]},
  ]
  }
timeline_1row_table = Daru::View::Table.new(data, height: 300, width: 200)
timeline_1row_table.show_in_iruby
Out[31]:
In [33]:
presidents_1row_chart = Daru::View::Plot.new(timeline_1row_table, type: :timeline, timeline: { groupByRowLabel: true })
presidents_1row_chart.show_in_iruby
Out[33]:
In [35]:
presidents_1row_chart = Daru::View::Plot.new(timeline_1row_table, type: :timeline, timeline: { groupByRowLabel: false })
presidents_1row_chart.show_in_iruby
Out[35]:

Controlling the colors

In [38]:
data = [
  [ 'Room', 'Name', 'Start' ,'End'],
     [ 'Magnolia Room', 'Beginning JavaScript',       DateTime.new(2017,7,31,12,0,0),  DateTime.new(2017,7,31,13,30,0) ],
      [ 'Magnolia Room', 'Intermediate JavaScript',  DateTime.new(2017,7,31,14,0,0),  DateTime.new(2017,7,31,15,30,0) ],
      [ 'Magnolia Room', 'Advanced JavaScript',        DateTime.new(2017,7,31,16,0,0),  DateTime.new(2017,7,31,17,30,0) ],
      [ 'Willow Room',   'Beginning Google Charts',    DateTime.new(2017,7,31,12,30,0), DateTime.new(2017,7,31,14,0,0) ],
      [ 'Willow Room',   'Intermediate Google Charts', DateTime.new(2017,7,31,14,30,0), DateTime.new(2017,7,31,16,0,0) ],
      [ 'Willow Room',   'Advanced Google Charts',     DateTime.new(2017,7,31,16,30,0), DateTime.new(2017,7,31,18,0,0) ]
    
  ]

timeline_color_table = Daru::View::Table.new(data, pageSize: 6)
timeline_color_table.show_in_iruby
Out[38]:
In [40]:
timeline_color_chart = Daru::View::Plot.new(timeline_color_table.table, type: :timeline, timeline: { colorByRowLabel: true })
timeline_color_chart.show_in_iruby
Out[40]:
In [42]:
timeline_single_color_chart = Daru::View::Plot.new(timeline_color_table.table, type: :timeline,  timeline: { singleColor: '#8d8' })
timeline_single_color_chart.show_in_iruby
Out[42]:
In [43]:
opts = {
  timeline: { colorByRowLabel: true },
      backgroundColor: '#ffd',
  type: :timeline
  }
timeline_bgcolor_chart = Daru::View::Plot.new(timeline_color_table.table, opts)
timeline_bgcolor_chart.show_in_iruby
Out[43]:
In [44]:
opts = {
colors: ['#cbb69d', '#603913', '#c69c6e'],
  type: :timeline
  }
timeline_bgcolor_chart = Daru::View::Plot.new(timeline_color_table.table, opts)
timeline_bgcolor_chart.show_in_iruby
Out[44]:

Changing the fonts

In [47]:
opts = {
      colors: ['#cbb69d', '#603913', '#c69c6e'],
      timeline: { rowLabelStyle: {fontName: 'Helvetica', fontSize: 24, color: '#603913' },
                     barLabelStyle: { fontName: 'Garamond', fontSize: 14 } },
      type: :timeline
  }
timeline_font_chart = Daru::View::Plot.new(timeline_color_table.table, opts)
timeline_font_chart.show_in_iruby
Out[47]:

Overlapping gridlines

In [48]:
presidents_1row_chart = Daru::View::Plot.new(timeline_1row_table, type: :timeline, timeline: { groupByRowLabel: true })
presidents_1row_chart.show_in_iruby
Out[48]:
In [52]:
opts = {
  timeline: { showRowLabels: false },
  # FixMe : not workin. refer : https://developers.google.com/chart/interactive/docs/gallery/timeline
  avoidOverlappingGridLines: false,
  type: :timeline
  }
presidents_1row_chart = Daru::View::Plot.new(timeline_1row_table, opts)
presidents_1row_chart.show_in_iruby
Out[52]:

Customizing tooltips

In [53]:
data = {
  cols: [{ type: 'string', id: 'President' },
          { type: 'string', label: 'dummy bar label' },
          { type: 'string', role: 'tooltip' },
          { type: 'date', label: 'Start' },{ type: 'date', label: 'End' }
           ],
  rows: [{c:[{v: 'Washington'}, {v: nil},{v: 'George'},  {v: DateTime.new(1789, 3, 30)}, {v: DateTime.new(1797, 2, 4)}]},
           {c:[{v: 'Adams'}, {v: nil},{v: 'John'},  {v: DateTime.new(1797, 2, 4)}, {v: DateTime.new(1801, 2, 4) }]},
    {c:[{v: 'Jefferson'}, {v: nil},{v: 'Thomas'},  {v: DateTime.new(1801, 2, 4)}, {v: DateTime.new(1809, 2, 4)}]},
  ]
  }
timeline_tooltip_table = Daru::View::Table.new(data, height: 300, width: 200)
timeline_tooltip_table.show_in_iruby
Out[53]:
In [55]:
opts = {
  type: :timeline
  }
presidents_tooltip_chart = Daru::View::Plot.new(timeline_tooltip_table.table, opts)
presidents_tooltip_chart.show_in_iruby
Out[55]: