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 Scatter Chart

In [3]:
idx = Daru::Index.new ['Age', 'Weight']
data_rows = [
  [ 8,      12],
  [ 4,      5.5],
  [ 11,     14],
  [ 4,      5],
  [ 3,      3.5],
  [ 6.5,    7]
]
df_age_wt = Daru::DataFrame.rows(data_rows)
df_age_wt.vectors = idx
df_age_wt
Out[3]:
Daru::DataFrame(6x2)
Age Weight
0 8 12
1 4 5.5
2 11 14
3 4 5
4 3 3.5
5 6.5 7
In [8]:
scatter_table = Daru::View::Table.new(df_age_wt, pageSize: 5, height: 200, width: 100)
scatter_table.table
Out[8]:
In [9]:
scatter_options = {
        title: 'Age vs. Weight comparison',
        hAxis: {title: 'Age', minValue: 0, maxValue: 15},
        vAxis: {title: 'Weight', minValue: 0, maxValue: 15},
        legend: 'none',
        type: :scatter
      }
scatter_chart = Daru::View::Plot.new(
    scatter_table.table, scatter_options)
scatter_chart.show_in_iruby
Out[9]:

Creating Material scatter charts

In [10]:
idx = Daru::Index.new ['Hours Studied', 'Final']
data_rows = [
  [0, 67], [1, 88], [2, 77],
  [3, 93], [4, 85], [5, 91],
  [6, 71], [7, 78], [8, 93],
  [9, 80], [10, 82],[0, 75],
  [5, 80], [3, 90], [1, 72],
  [5, 75], [6, 68], [7, 98],
  [3, 82], [9, 94], [2, 79],
  [2, 95], [2, 86], [3, 67],
  [4, 60], [2, 80], [6, 92],
  [2, 81], [8, 79], [9, 83],
  [3, 75], [1, 80], [3, 71],
  [3, 89], [4, 92], [5, 85],
  [6, 92], [7, 78], [6, 95],
  [3, 81], [0, 64], [4, 85],
  [2, 83], [3, 96], [4, 77],
  [5, 89], [4, 89], [7, 84],
  [4, 92], [9, 98]
]
df_study_hours = Daru::DataFrame.rows(data_rows)
df_study_hours.vectors = idx
df_study_hours
Out[10]:
Daru::DataFrame(50x2)
Hours Studied Final
0 0 67
1 1 88
2 2 77
3 3 93
4 4 85
5 5 91
6 6 71
7 7 78
8 8 93
9 9 80
10 10 82
11 0 75
12 5 80
13 3 90
14 1 72
15 5 75
16 6 68
17 7 98
18 3 82
19 9 94
20 2 79
21 2 95
22 2 86
23 3 67
24 4 60
25 2 80
26 6 92
27 2 81
28 8 79
29 9 83
... ... ...
49 9 98
In [13]:
scatter_material_table = Daru::View::Table.new(df_study_hours, pageSize: 10, height: 300, width: 200)
scatter_material_table.table
Out[13]:
In [17]:
scatter_material_options = {
        width: 800,
        height: 500,
        chart: {
          title: 'Students\' Final Grades',
          subtitle: 'based on hours studied'
        },
        hAxis: {title: 'Hours Studied'},
        vAxis: {title: 'Grade'},
        type: :scatter
      }
scatter_material_chart = Daru::View::Plot.new(
    scatter_material_table.table, scatter_material_options)
scatter_material_chart.show_in_iruby
Out[17]:

Dual-Y charts

In [18]:
idx = Daru::Index.new ['Student ID', 'Hours Studied', 'Final']
data_rows = [
  [0, 0, 67],  [1, 1, 88],   [2, 2, 77],
  [3, 3, 93],  [4, 4, 85],   [5, 5, 91],
  [6, 6, 71],  [7, 7, 78],   [8, 8, 93],
  [9, 9, 80],  [10, 10, 82], [11, 0, 75],
  [12, 5, 80], [13, 3, 90],  [14, 1, 72],
  [15, 5, 75], [16, 6, 68],  [17, 7, 98],
  [18, 3, 82], [19, 9, 94],  [20, 2, 79],
  [21, 2, 95], [22, 2, 86],  [23, 3, 67],
  [24, 4, 60], [25, 2, 80],  [26, 6, 92],
  [27, 2, 81], [28, 8, 79],  [29, 9, 83]
]
df_id_study_hours = Daru::DataFrame.rows(data_rows)
df_id_study_hours.vectors = idx
df_id_study_hours
Out[18]:
Daru::DataFrame(30x3)
Student ID Hours Studied Final
0 0 0 67
1 1 1 88
2 2 2 77
3 3 3 93
4 4 4 85
5 5 5 91
6 6 6 71
7 7 7 78
8 8 8 93
9 9 9 80
10 10 10 82
11 11 0 75
12 12 5 80
13 13 3 90
14 14 1 72
15 15 5 75
16 16 6 68
17 17 7 98
18 18 3 82
19 19 9 94
20 20 2 79
21 21 2 95
22 22 2 86
23 23 3 67
24 24 4 60
25 25 2 80
26 26 6 92
27 27 2 81
28 28 8 79
29 29 9 83
In [24]:
scatter_dualY_table = Daru::View::Table.new(df_id_study_hours, pageSize: 10, height: 300, width: 300)
scatter_dualY_table.table
Out[24]:
In [48]:
scatter_dualY_options = {
        chart: {
          title: 'Students\' Final Grades',
          subtitle: 'based on hours studied'
        },
        width: 800,
        height: 500,
# FixMe: There is some problem in below code execution. So similar charts 
# options is not working from this 
# link : https://developers.google.com/chart/interactive/docs/gallery/scatterchart#fullhtml
#         series: {
#           '0': {axis: 'hours studied'},
#           '1': {axis: 'final grade'}
#         },
#         axes: {
#           y: {
#             'hours studied': {label: 'Hours Studied'},
#             'final grade': {label: 'Final Exam Grade'}
#           }
#         },
        type: :scatter
}
scatter_dualY_chart = Daru::View::Plot.new(
    scatter_dualY_table.table, scatter_dualY_options)
scatter_dualY_chart.show_in_iruby
Out[48]: