chart 0.1.2 → 0.1.3.beta1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b3e2b48fd196a4e6a9ce6e03f0a0b399e1e39c7c
4
- data.tar.gz: 8504b3056f55ccd0d787ad988c41b0fc9af13d17
3
+ metadata.gz: db8a451b4829c7ea90c5cdcf8bd0033310c9d4e4
4
+ data.tar.gz: c47f163bb79379618fdcf52e9c34b483b33d1405
5
5
  SHA512:
6
- metadata.gz: 32b2018f8526108ee75ad4feb23a85de7cacbe47705ef7986f0de2843b7232531e40fd470ad096f8a008da61ae6e5cfd058ccdb1a455e1220bdfd2474832ed59
7
- data.tar.gz: ac7914c67337647a5d16c481dc1da936555fc0c07f8cbfca714e83d9699e201aaab472f636ed34cf5f6b7fefc901e76029ff2b172e49052b816df197efc70bc8
6
+ metadata.gz: 2d9d91c8573ade03d6d5b1418362e4c9d9cb11b6a29dcd8c60ba0b71468bab863a347086cb04d316f07c0859730d473a527573714500587eb9b71d9e0e9b814d
7
+ data.tar.gz: b68e02b179dc394dd7fa04a066b068bd55caa1a16939d7551e0e90dd5699d611475a5934430cd709b151baa104fe4c7aa29190b4282e9af360a17c5e4ee311ee
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2015 ytbryan
3
+ Copyright (c) 2015 Bryan Lim
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -9,13 +9,14 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
9
  copies of the Software, and to permit persons to whom the Software is
10
10
  furnished to do so, subject to the following conditions:
11
11
 
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
14
 
15
15
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
16
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
17
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
18
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
19
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md CHANGED
@@ -1,9 +1,44 @@
1
- ![Chart](assets/images/chart.gif)
1
+ ![Chart](assets/images/example.gif)
2
2
 
3
- [See an example on heroku](http://chartgemdemo.herokuapp.com) ([Source](http://github.com/ytbryan/chartdemo))
3
+ [See an example on heroku](http://chartgemdemo.herokuapp.com) ([Example Source Code](http://github.com/ytbryan/chartdemo))
4
4
 
5
5
  # Chart
6
6
 
7
+ ## The motivation for making `chart`
8
+
9
+ I was reading up on rails engine and decided to test out my new knowledge by making a gem. As my previous project involves making multiple charts, I understand the pain of having duplicate data structure and functions in javascript and ruby just for the different charts.
10
+
11
+ Chart is my solution to quickly add Javascript/HTML chart into rails. You can write coffeescript/javascript to further interact with the rendered chart. Chart is inspired by another awesome gem `chartkick`
12
+
13
+ The limitation of chart is that it generates in-line javascript and it does not aim to be a 100% wrapper for all chart.
14
+
15
+ Chart works with Sprockets and your asset pipeline.
16
+
17
+ Several flavours to choose from.
18
+
19
+ ChartJS (MIT)
20
+ * Bar Chart `<%= bar :id_of_your_chart, size, data %>`
21
+ * Line Chart `<%= line :id, size, data %>`
22
+ * Pie Chart `<%= pie :id, size, data %>`
23
+ * Radar Chart `<%= radar :id, size, data %>`
24
+
25
+ NVD3 (MIT)
26
+ * Box Plot `<%= boxplot :id, size, data %>`
27
+ * Discrete Bar `<%= discrete_bar :id, size, data %>`
28
+ * Horizontal Grouped Bar `<%= horizontal_grouped_bar :id, size, data %>`
29
+
30
+ Google Chart (Open Source)
31
+ * Bar Chart `<%= gBar :id, size, data %>`
32
+ * Line Chart `<%= gLine :id, size, data %>`
33
+ * Pie Chart `<%= gPie :id, size, data %>`
34
+
35
+ Highchart (Free for Personal usage but paid for Commercial)
36
+ * Bar Chart `<%= hBar %>`
37
+ * Line Chart `<%= hLine %>`
38
+ * Pie Chart `<%= hPie %>`
39
+ * Area Chart `<%= hArea %>`
40
+
41
+
7
42
  ## Installation
8
43
 
9
44
  Add this line to your application's Gemfile:
@@ -12,18 +47,84 @@ Add this line to your application's Gemfile:
12
47
  gem 'chart'
13
48
  ```
14
49
 
15
- At application.js, write `//= require chart` after turbolinks
50
+ At Application.js, write `//= require chart` after turbolinks. Only require the chart if you need them.
16
51
 
17
52
  ```
18
53
  //= require turbolinks
19
54
  //= require chart
55
+ //= require nv.d3
56
+ //= require google
57
+ //= require highchart
58
+ ```
59
+
60
+ At Application.css, write `*= require nv.d3`
61
+
62
+ ```
63
+ *= require_tree .
64
+ *= require nv.d3
20
65
  ```
21
66
 
22
67
  ## Important
23
68
 
24
- Chart ships with ChartJs (1.02).
69
+ Chart ships with the following awesome charts:
70
+
71
+ * [ChartJS](https://github.com/nnnick/Chart.js/)(1.02)
72
+ * [NVD3](https://github.com/novus/nvd3)(1.8.2)
73
+ * [Google Chart](https://developers.google.com/chart/)
74
+ * [Highchart](http://www.highcharts.com)(4.2.3)
75
+
76
+ ## Example Usage
77
+
78
+ ![Chart](assets/images/pie.gif)
25
79
 
26
80
 
81
+ At your View
82
+ ```
83
+ <%= pie :pie_chart, @pieSize, @pieData%>
84
+ ```
85
+
86
+ At your Controller
87
+ ```
88
+ @pieSize = {
89
+ :height => 500,
90
+ :width => 500
91
+ }
92
+
93
+ @pieData = [
94
+ {
95
+ value: 300,
96
+ color:"#F7464A",
97
+ highlight: "#FF5A5E",
98
+ label: "Red"
99
+ },
100
+ {
101
+ value: 50,
102
+ color: "#46BFBD",
103
+ highlight: "#5AD3D1",
104
+ label: "Green"
105
+ },
106
+ {
107
+ value: 100,
108
+ color: "#FDB45C",
109
+ highlight: "#FFC870",
110
+ label: "Yellow"
111
+ },
112
+ {
113
+ value: 40,
114
+ color: "#949FB1",
115
+ highlight: "#A8B3C5",
116
+ label: "Grey"
117
+ },
118
+ {
119
+ value: 120,
120
+ color: "#4D5360",
121
+ highlight: "#616774",
122
+ label: "Dark Grey"
123
+ }
124
+
125
+ ].to_json
126
+ ```
127
+
27
128
  ## Development
28
129
 
29
130
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -1,8 +1,396 @@
1
1
  module ChartHelper
2
+
3
+
4
+ #
5
+ #
6
+ # Highcharts
7
+ def hLine
8
+ html = "<div id=\"container\" style=\"min-width: 310px; height: 400px; margin: 0 auto\"></div>".html_safe
9
+ script = javascript_tag do
10
+ <<-END.html_safe
11
+ $(function () {
12
+ $('#container').highcharts({
13
+ title: {
14
+ text: 'Monthly Average Temperature',
15
+ x: -20 //center
16
+ },
17
+ subtitle: {
18
+ text: 'Source: WorldClimate.com',
19
+ x: -20
20
+ },
21
+ xAxis: {
22
+ categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
23
+ 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
24
+ },
25
+ yAxis: {
26
+ title: {
27
+ text: 'Temperature (°C)'
28
+ },
29
+ plotLines: [{
30
+ value: 0,
31
+ width: 1,
32
+ color: '#808080'
33
+ }]
34
+ },
35
+ tooltip: {
36
+ valueSuffix: '°C'
37
+ },
38
+ legend: {
39
+ layout: 'vertical',
40
+ align: 'right',
41
+ verticalAlign: 'middle',
42
+ borderWidth: 0
43
+ },
44
+ series: [{
45
+ name: 'Tokyo',
46
+ data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
47
+ }, {
48
+ name: 'New York',
49
+ data: [-0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5]
50
+ }, {
51
+ name: 'Berlin',
52
+ data: [-0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0]
53
+ }, {
54
+ name: 'London',
55
+ data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8]
56
+ }]
57
+ });
58
+ });
59
+ END
60
+ end
61
+ return html + script
62
+ end
63
+
64
+
65
+ def hArea
66
+ html = "<div id=\"container_area\" style=\"min-width: 310px; height: 400px; margin: 0 auto\"></div>".html_safe
67
+ script = javascript_tag do
68
+ <<-END.html_safe
69
+ $(function () {
70
+ $('#container_area').highcharts({
71
+ chart: {
72
+ type: 'area'
73
+ },
74
+ title: {
75
+ text: 'US and USSR nuclear stockpiles'
76
+ },
77
+ subtitle: {
78
+ text: 'Source: <a href="http://thebulletin.metapress.com/content/c4120650912x74k7/fulltext.pdf">' +
79
+ 'thebulletin.metapress.com</a>'
80
+ },
81
+ xAxis: {
82
+ allowDecimals: false,
83
+ labels: {
84
+ formatter: function () {
85
+ return this.value; // clean, unformatted number for year
86
+ }
87
+ }
88
+ },
89
+ yAxis: {
90
+ title: {
91
+ text: 'Nuclear weapon states'
92
+ },
93
+ labels: {
94
+ formatter: function () {
95
+ return this.value / 1000 + 'k';
96
+ }
97
+ }
98
+ },
99
+ tooltip: {
100
+ pointFormat: '{series.name} produced <b>{point.y:,.0f}</b><br/>warheads in {point.x}'
101
+ },
102
+ plotOptions: {
103
+ area: {
104
+ pointStart: 1940,
105
+ marker: {
106
+ enabled: false,
107
+ symbol: 'circle',
108
+ radius: 2,
109
+ states: {
110
+ hover: {
111
+ enabled: true
112
+ }
113
+ }
114
+ }
115
+ }
116
+ },
117
+ series: [{
118
+ name: 'USA',
119
+ data: [null, null, null, null, null, 6, 11, 32, 110, 235, 369, 640,
120
+ 1005, 1436, 2063, 3057, 4618, 6444, 9822, 15468, 20434, 24126,
121
+ 27387, 29459, 31056, 31982, 32040, 31233, 29224, 27342, 26662,
122
+ 26956, 27912, 28999, 28965, 27826, 25579, 25722, 24826, 24605,
123
+ 24304, 23464, 23708, 24099, 24357, 24237, 24401, 24344, 23586,
124
+ 22380, 21004, 17287, 14747, 13076, 12555, 12144, 11009, 10950,
125
+ 10871, 10824, 10577, 10527, 10475, 10421, 10358, 10295, 10104]
126
+ }, {
127
+ name: 'USSR/Russia',
128
+ data: [null, null, null, null, null, null, null, null, null, null,
129
+ 5, 25, 50, 120, 150, 200, 426, 660, 869, 1060, 1605, 2471, 3322,
130
+ 4238, 5221, 6129, 7089, 8339, 9399, 10538, 11643, 13092, 14478,
131
+ 15915, 17385, 19055, 21205, 23044, 25393, 27935, 30062, 32049,
132
+ 33952, 35804, 37431, 39197, 45000, 43000, 41000, 39000, 37000,
133
+ 35000, 33000, 31000, 29000, 27000, 25000, 24000, 23000, 22000,
134
+ 21000, 20000, 19000, 18000, 18000, 17000, 16000]
135
+ }]
136
+ });
137
+ });
138
+ END
139
+ end
140
+ return html + script
141
+ end
142
+
143
+
144
+ def hBar
145
+ html = "<div id=\"container_bar\" style=\"min-width: 310px; height: 400px; margin: 0 auto\"></div>".html_safe
146
+ script = javascript_tag do
147
+ <<-END.html_safe
148
+ $(function () {
149
+ $('#container_bar').highcharts({
150
+ chart: {
151
+ type: 'bar'
152
+ },
153
+ title: {
154
+ text: 'Historic World Population by Region'
155
+ },
156
+ subtitle: {
157
+ text: 'Source: <a href="https://en.wikipedia.org/wiki/World_population">Wikipedia.org</a>'
158
+ },
159
+ xAxis: {
160
+ categories: ['Africa', 'America', 'Asia', 'Europe', 'Oceania'],
161
+ title: {
162
+ text: null
163
+ }
164
+ },
165
+ yAxis: {
166
+ min: 0,
167
+ title: {
168
+ text: 'Population (millions)',
169
+ align: 'high'
170
+ },
171
+ labels: {
172
+ overflow: 'justify'
173
+ }
174
+ },
175
+ tooltip: {
176
+ valueSuffix: ' millions'
177
+ },
178
+ plotOptions: {
179
+ bar: {
180
+ dataLabels: {
181
+ enabled: true
182
+ }
183
+ }
184
+ },
185
+ legend: {
186
+ layout: 'vertical',
187
+ align: 'right',
188
+ verticalAlign: 'top',
189
+ x: -40,
190
+ y: 80,
191
+ floating: true,
192
+ borderWidth: 1,
193
+ backgroundColor: ((Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'),
194
+ shadow: true
195
+ },
196
+ credits: {
197
+ enabled: false
198
+ },
199
+ series: [{
200
+ name: 'Year 1800',
201
+ data: [107, 31, 635, 203, 2]
202
+ }, {
203
+ name: 'Year 1900',
204
+ data: [133, 156, 947, 408, 6]
205
+ }, {
206
+ name: 'Year 2012',
207
+ data: [1052, 954, 4250, 740, 38]
208
+ }]
209
+ });
210
+ });
211
+ END
212
+ end
213
+ return html + script
214
+ end
215
+
216
+
217
+
218
+
219
+
220
+ def hPie
221
+ html = "<div id=\"container_pie\" style=\"min-width: 310px; height: 400px; margin: 0 auto\"></div>".html_safe
222
+ script = javascript_tag do
223
+ <<-END.html_safe
224
+ $(function () {
225
+ $('#container_pie').highcharts({
226
+ chart: {
227
+ plotBackgroundColor: null,
228
+ plotBorderWidth: null,
229
+ plotShadow: false,
230
+ type: 'pie'
231
+ },
232
+ title: {
233
+ text: 'Browser market shares January, 2015 to May, 2015'
234
+ },
235
+ tooltip: {
236
+ pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
237
+ },
238
+ plotOptions: {
239
+ pie: {
240
+ allowPointSelect: true,
241
+ cursor: 'pointer',
242
+ dataLabels: {
243
+ enabled: true,
244
+ format: '<b>{point.name}</b>: {point.percentage:.1f} %',
245
+ style: {
246
+ color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
247
+ }
248
+ }
249
+ }
250
+ },
251
+ series: [{
252
+ name: 'Brands',
253
+ colorByPoint: true,
254
+ data: [{
255
+ name: 'Microsoft Internet Explorer',
256
+ y: 56.33
257
+ }, {
258
+ name: 'Chrome',
259
+ y: 24.03,
260
+ sliced: true,
261
+ selected: true
262
+ }, {
263
+ name: 'Firefox',
264
+ y: 10.38
265
+ }, {
266
+ name: 'Safari',
267
+ y: 4.77
268
+ }, {
269
+ name: 'Opera',
270
+ y: 0.91
271
+ }, {
272
+ name: 'Proprietary or Undetectable',
273
+ y: 0.2
274
+ }]
275
+ }]
276
+ });
277
+ });
278
+ END
279
+ end
280
+ return html + script
281
+ end
282
+
283
+
284
+ #
285
+ #
286
+ # Google Chart
287
+
288
+ def gBar
289
+
290
+
291
+ end
292
+
293
+
294
+ def gLine
295
+
296
+ end
297
+
298
+
299
+
300
+
301
+
302
+
2
303
  #
3
304
  # D3.js related chart
4
305
  #
5
306
 
307
+ def horizontal_grouped_bar id=nil, size=nil, data=nil, options=nil
308
+ html = "<div style=\"width: #{size[:width]}px; height: #{size[:height]}px;\" id=\"horizontal_grouped_bar_#{id}\"><svg></svg></div>".html_safe
309
+ script = javascript_tag do
310
+ <<-END.html_safe
311
+ var chart;
312
+ nv.addGraph(function() {
313
+ chart = nv.models.multiBarHorizontalChart()
314
+ .x(function(d) { return d.label })
315
+ .y(function(d) { return d.value })
316
+ //.yErr(function(d) { return [-Math.abs(d.value * Math.random() * 0.3), Math.abs(d.value * Math.random() * 0.3)] })
317
+ //.barColor(d3.scale.category20().range())
318
+ //.duration(250)
319
+ .showValues(true)
320
+ //.margin({left: 100})
321
+ .stacked(false);
322
+
323
+ chart.yAxis.tickFormat(d3.format(',.2f'));
324
+
325
+ chart.yAxis.axisLabel('Y Axis');
326
+ chart.xAxis.axisLabel('X Axis').axisLabelDistance(100);
327
+
328
+ d3.select('#horizontal_grouped_bar_#{id} svg')
329
+ .datum(#{data})
330
+ .call(chart);
331
+
332
+ nv.utils.windowResize(chart.update);
333
+
334
+ chart.dispatch.on('stateChange', function(e) { nv.log('New State:', JSON.stringify(e)); });
335
+ chart.state.dispatch.on('change', function(state){
336
+ nv.log('state', JSON.stringify(state));
337
+ });
338
+ return chart;
339
+ });
340
+
341
+ END
342
+ end
343
+
344
+ return html + script
345
+ end
346
+
347
+ def discrete_bar id=nil, size=nil, data=nil, options=nil
348
+ html = "<div style=\"width: #{size[:width]}px; height: #{size[:height]}px;\" id=\"discrete_bar_#{id}\"><svg></svg></div>".html_safe
349
+ script = javascript_tag do
350
+ <<-END.html_safe
351
+ nv.addGraph(function() {
352
+ var chart = nv.models.discreteBarChart()
353
+ .x(function(d) { return d.label })
354
+ .y(function(d) { return d.value })
355
+ .staggerLabels(true)
356
+ //.staggerLabels(historicalBarChart[0].values.length > 8)
357
+ .showValues(true)
358
+ //.duration(250);
359
+
360
+ d3.select('#discrete_bar_#{id} svg')
361
+ .datum(#{data})
362
+ .call(chart);
363
+ nv.utils.windowResize(chart.update);
364
+ return chart;
365
+ });
366
+ END
367
+ end
368
+
369
+ return html + script
370
+ end
371
+
372
+ def boxplot id=nil, size=nil, data=nil, options=nil
373
+ html = "<div style=\"width: #{size[:width]}px; height: #{size[:height]}px;\" class=\"gallery\" id=\"boxplot_#{id}\"><svg></svg></div>".html_safe
374
+ script = javascript_tag do
375
+ <<-END.html_safe
376
+ nv.addGraph(function() {
377
+ var chart = nv.models.boxPlotChart()
378
+ .x(function(d) { return d.label })
379
+ .y(function(d) { return d.values.Q3 })
380
+ .staggerLabels(true)
381
+ .maxBoxWidth(10) // prevent boxes from being incredibly wide
382
+ .yDomain([0, 700]);
383
+ d3.select('#boxplot_#{id} svg')
384
+ .datum(#{data})
385
+ .call(chart);
386
+ nv.utils.windowResize(chart.update);
387
+ return chart;
388
+ });
389
+ END
390
+ end
391
+
392
+ return html + script
393
+ end
6
394
 
7
395
  #
8
396
  # Chartjs
@@ -16,7 +404,7 @@ module ChartHelper
16
404
  window.myBar_#{id} = new Chart(ctx_#{id}).Bar(barChartData, {});
17
405
  END
18
406
  end
19
- html + script
407
+ return html + script
20
408
  end
21
409
 
22
410
  def line id=nil, size=nil, data=nil
@@ -32,7 +420,7 @@ module ChartHelper
32
420
  end
33
421
 
34
422
  def pie id=nil, size=nil, data=nil
35
- html = "<canvas id=\"pie_#{id}\" height=\"450\" width=\"600\"></canvas>".html_safe
423
+ html = "<canvas id=\"pie_#{id}\" height=\"#{size[:height]}\" width=\"#{size[:width]}\"></canvas>".html_safe
36
424
  script = javascript_tag do
37
425
  <<-END.html_safe
38
426
  var pieData = #{data}
@@ -44,7 +432,7 @@ module ChartHelper
44
432
  end
45
433
 
46
434
  def radar id=nil, size=nil, data=nil
47
- html = "<canvas id=\"radar_#{id}\" height=\"450\" width=\"600\"></canvas>".html_safe
435
+ html = "<canvas id=\"radar_#{id}\" height=\"#{size[:height]}\" width=\"#{size[:width]}\"></canvas>".html_safe
48
436
  script = javascript_tag do
49
437
  <<-END.html_safe
50
438
  var radarChartData = #{data}