Module: GoogleHelper

Defined in:
app/helpers/google_helper.rb

Instance Method Summary collapse

Instance Method Details

#google_area(id = nil, size = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/helpers/google_helper.rb', line 38

def google_area id=nil, size=nil
  html = "<div id=\"chart_div\" style=\"width: 900px; height: 500px;\"></div>".html_safe
  script = javascript_tag do
    <<-END.html_safe
      google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(drawChart);
      function drawChart() {
      var data = google.visualization.arrayToDataTable([
        ['Year', 'Sales', 'Expenses'],
        ['2013',  1000,      400],
        ['2014',  1170,      460],
        ['2015',  660,       1120],
        ['2016',  1030,      540]
      ]);

      var options = {
        title: 'Company Performance',
        hAxis: {title: 'Year',  titleTextStyle: {color: '#333'}},
        vAxis: {minValue: 0}
      };

      var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
      chart.draw(data, options);
      }
    END
  end
  return html + script
end

#google_bar(id = nil, size = nil) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'app/helpers/google_helper.rb', line 142

def google_bar id=nil, size=nil
  html = "<div id=\"barchart_values\" style=\"width: 900px; height: 300px;\"></div>".html_safe
  script = javascript_tag do
    <<-END.html_safe
    google.charts.load("current", {packages:["corechart"]});
    google.charts.setOnLoadCallback(drawChart);
    function drawChart() {
    var data = google.visualization.arrayToDataTable([
      ["Element", "Density", { role: "style" } ],
      ["Copper", 8.94, "#b87333"],
      ["Silver", 10.49, "silver"],
      ["Gold", 19.30, "gold"],
      ["Platinum", 21.45, "color: #e5e4e2"]
    ]);

    var view = new google.visualization.DataView(data);
    view.setColumns([0, 1,
                     { calc: "stringify",
                       sourceColumn: 1,
                       type: "string",
                       role: "annotation" },
                     2]);

    var options = {
      title: "Density of Precious Metals, in g/cm^3",
      width: 600,
      height: 400,
      bar: {groupWidth: "95%"},
      legend: { position: "none" },
    };
    var chart = new google.visualization.BarChart(document.getElementById("barchart_values"));
    chart.draw(view, options);
    }

    END
  end
  return html + script
end

#google_bubble(id = nil, size = nil) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'app/helpers/google_helper.rb', line 181

def google_bubble id=nil, size=nil
  html = "<div id=\"series_chart_div\" style=\"width: 900px; height: 500px;\"></div>".html_safe
  script = javascript_tag do
    <<-END.html_safe
      google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(drawSeriesChart);

      function drawSeriesChart() {

      var data = google.visualization.arrayToDataTable([
      ['ID', 'Life Expectancy', 'Fertility Rate', 'Region',     'Population'],
      ['CAN',    80.66,              1.67,      'North America',  33739900],
      ['DEU',    79.84,              1.36,      'Europe',         81902307],
      ['DNK',    78.6,               1.84,      'Europe',         5523095],
      ['EGY',    72.73,              2.78,      'Middle East',    79716203],
      ['GBR',    80.05,              2,         'Europe',         61801570],
      ['IRN',    72.49,              1.7,       'Middle East',    73137148],
      ['IRQ',    68.09,              4.77,      'Middle East',    31090763],
      ['ISR',    81.55,              2.96,      'Middle East',    7485600],
      ['RUS',    68.6,               1.54,      'Europe',         141850000],
      ['USA',    78.09,              2.05,      'North America',  307007000]
      ]);

      var options = {
      title: 'Correlation between life expectancy, fertility rate ' +
             'and population of some world countries (2010)',
      hAxis: {title: 'Life Expectancy'},
      vAxis: {title: 'Fertility Rate'},
      bubble: {textStyle: {fontSize: 11}}
      };

      var chart = new google.visualization.BubbleChart(document.getElementById('series_chart_div'));
      chart.draw(data, options);
      }

    END
  end
  return html + script
end

#google_calendar(id = nil, size = nil) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'app/helpers/google_helper.rb', line 221

def google_calendar id=nil, size=nil
  html = "<div id=\"calendar_basic\" style=\"width: 1000px; height: 350px;\"></div>".html_safe
  script = javascript_tag do
    <<-END.html_safe
      google.charts.load("current", {packages:["calendar"]});
      google.charts.setOnLoadCallback(drawChart);

      function drawChart() {
      var dataTable = new google.visualization.DataTable();
      dataTable.addColumn({ type: 'date', id: 'Date' });
      dataTable.addColumn({ type: 'number', id: 'Won/Loss' });
      dataTable.addRows([
        [ new Date(2015, 3, 13), 37032 ],
        [ new Date(2015, 3, 14), 38024 ],
        [ new Date(2015, 3, 15), 38024 ],
        [ new Date(2015, 3, 16), 38108 ],
        [ new Date(2015, 3, 17), 38229 ],
        // Many rows omitted for brevity.
        [ new Date(2016, 9, 4), 38177 ],
        [ new Date(2016, 9, 5), 38705 ],
        [ new Date(2016, 9, 12), 38210 ],
        [ new Date(2016, 9, 13), 38029 ],
        [ new Date(2016, 9, 19), 38823 ],
        [ new Date(2016, 9, 23), 38345 ],
        [ new Date(2016, 9, 24), 38436 ],
        [ new Date(2016, 9, 30), 38447 ]
      ]);

      var chart = new google.visualization.Calendar(document.getElementById('calendar_basic'));

      var options = {
       title: "Red Sox Attendance",
       height: 350,
      };

      chart.draw(dataTable, options);
      }

    END
  end
  return html + script
end

#google_gauge_charts(id = nil, size = nil) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/helpers/google_helper.rb', line 97

def google_gauge_charts id=nil, size=nil
  html = "<div id=\"chart_div\" style=\"width: 400px; height: 120px;\"></div>".html_safe
  script = javascript_tag do
    <<-END.html_safe
      google.charts.load('current', {'packages':['gauge']});
      google.charts.setOnLoadCallback(drawChart);
      function drawChart() {

      var data = google.visualization.arrayToDataTable([
        ['Label', 'Value'],
        ['Memory', 80],
        ['CPU', 55],
        ['Network', 68]
      ]);

      var options = {
        width: 400, height: 120,
        redFrom: 90, redTo: 100,
        yellowFrom:75, yellowTo: 90,
        minorTicks: 5
      };

      var chart = new google.visualization.Gauge(document.getElementById('chart_div'));

      chart.draw(data, options);

      setInterval(function() {
        data.setValue(0, 1, 40 + Math.round(60 * Math.random()));
        chart.draw(data, options);
      }, 13000);
      setInterval(function() {
        data.setValue(1, 1, 40 + Math.round(60 * Math.random()));
        chart.draw(data, options);
      }, 5000);
      setInterval(function() {
        data.setValue(2, 1, 60 + Math.round(20 * Math.random()));
        chart.draw(data, options);
      }, 26000);
      }

    END
  end
  return html + script
end

#google_geochart(id = nil, size = nil) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/helpers/google_helper.rb', line 67

def google_geochart id=nil, size=nil
  html = "<div id=\"regions_div\" style=\"width: 900px; height: 500px;\"></div>".html_safe
  script = javascript_tag do
    <<-END.html_safe
        google.charts.load('current', {'packages':['geochart']});
      google.charts.setOnLoadCallback(drawRegionsMap);

      function drawRegionsMap() {

        var data = google.visualization.arrayToDataTable([
          ['Country', 'Popularity'],
          ['Germany', 200],
          ['United States', 300],
          ['Brazil', 400],
          ['Canada', 500],
          ['France', 600],
          ['RU', 700]
        ]);

        var options = {};

        var chart = new google.visualization.GeoChart(document.getElementById('regions_div'));

        chart.draw(data, options);
      }
    END
  end
  return html + script
end

#google_pie(id = nil, size = nil) ⇒ Object

Google Chart



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/helpers/google_helper.rb', line 6

def google_pie id=nil, size=nil
  html = "<div id=\"piechart\" style=\"width: 900px; height: 500px;\"></div>".html_safe
  script = javascript_tag do
    <<-END.html_safe
    google.charts.load('current', {'packages':['corechart']});
  google.charts.setOnLoadCallback(drawChart);
  function drawChart() {

    var data = google.visualization.arrayToDataTable([
      ['Task', 'Hours per Day'],
      ['Work',     11],
      ['Eat',      2],
      ['Commute',  2],
      ['Watch TV', 2],
      ['Sleep',    7]
    ]);

    var options = {
      title: 'My Daily Activities'
    };

    var chart = new google.visualization.PieChart(document.getElementById('piechart'));

    chart.draw(data, options);
  }


    END
  end
  return html + script
end

#google_scatterchart(id = nil, size = nil) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'app/helpers/google_helper.rb', line 264

def google_scatterchart id=nil, size=nil
  html = "<div id=\"chart_div\" style=\"width: 900px; height: 500px;\"></div>".html_safe
  script = javascript_tag do
    <<-END.html_safe
      google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['Age', 'Weight'],
          [ 8,      12],
          [ 4,      5.5],
          [ 11,     14],
          [ 4,      5],
          [ 3,      3.5],
          [ 6.5,    7]
        ]);
        var options = {
          title: 'Age vs. Weight comparison',
          hAxis: {title: 'Age', minValue: 0, maxValue: 15},
          vAxis: {title: 'Weight', minValue: 0, maxValue: 15},
          legend: 'none'
        };
        var chart = new google.visualization.ScatterChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }

    END
  end
  return html + script
end