chartkick 1.1.3 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of chartkick might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +14 -2
- data/app/assets/javascripts/chartkick.js +132 -21
- data/lib/chartkick/helper.rb +8 -0
- data/lib/chartkick/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b3fe5dc62f6b7171a27d19d76dd81e10b045a909
|
4
|
+
data.tar.gz: 9a7a9a2e5a3198176367141c7612cc28ae071296
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e77ef7d0f14c393d270c7cbc4c340a14559c85b524593c7bfa7d773f4109e61d0bad93017193bf4edebb4897f9cf5fecb2ba5900eac7369ca7a61dfd850356f4
|
7
|
+
data.tar.gz: 0c0196a6877fd1a33eeece1e6990f3c9fbe6c1008890754ff9ce4d78ecde2669499b7947ecffcc6cfa3eeda1c6fbbf30d6620bbb39ebc284f1d1bad27906b4fd
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -28,6 +28,18 @@ Column chart
|
|
28
28
|
<%= column_chart Task.group_by_hour_of_day(:created_at).count %>
|
29
29
|
```
|
30
30
|
|
31
|
+
Bar chart
|
32
|
+
|
33
|
+
```erb
|
34
|
+
<%= column_chart Event.group_by_day_of_week(:starts_at).count %>
|
35
|
+
```
|
36
|
+
|
37
|
+
Area chart
|
38
|
+
|
39
|
+
```erb
|
40
|
+
<%= area_chart Visit.group_by_minute(:created_at).count %>
|
41
|
+
```
|
42
|
+
|
31
43
|
Multiple series (except pie chart)
|
32
44
|
|
33
45
|
```erb
|
@@ -84,8 +96,8 @@ You can also pass a content_for option, which will put the javascript in a conte
|
|
84
96
|
Then, in your layout:
|
85
97
|
|
86
98
|
```erb
|
87
|
-
<%= yield :js_initialization
|
88
|
-
<%= yield_content :js_initialization
|
99
|
+
<%= yield :js_initialization %> <%# Rails %>
|
100
|
+
<%= yield_content :js_initialization %> <%# Padrino %>
|
89
101
|
```
|
90
102
|
|
91
103
|
### Data
|
@@ -2,7 +2,7 @@
|
|
2
2
|
* Chartkick.js
|
3
3
|
* Create beautiful Javascript charts with minimal code
|
4
4
|
* https://github.com/ankane/chartkick.js
|
5
|
-
* v1.0
|
5
|
+
* v1.1.0
|
6
6
|
* MIT License
|
7
7
|
*/
|
8
8
|
|
@@ -97,8 +97,9 @@
|
|
97
97
|
}
|
98
98
|
|
99
99
|
function jsOptionsFunc(defaultOptions, hideLegend, setMin, setMax) {
|
100
|
-
return function(series, opts) {
|
100
|
+
return function(series, opts, chartOptions) {
|
101
101
|
var options = merge({}, defaultOptions);
|
102
|
+
options = merge(options, chartOptions || {});
|
102
103
|
|
103
104
|
// hide legend
|
104
105
|
// this is *not* an external option!
|
@@ -127,11 +128,12 @@
|
|
127
128
|
}
|
128
129
|
|
129
130
|
// only functions that need defined specific to charting library
|
130
|
-
var renderLineChart, renderPieChart, renderColumnChart;
|
131
|
+
var renderLineChart, renderPieChart, renderColumnChart, renderBarChart, renderAreaChart;
|
131
132
|
|
132
133
|
if ("Highcharts" in window) {
|
133
134
|
|
134
135
|
var defaultOptions = {
|
136
|
+
chart: {},
|
135
137
|
xAxis: {
|
136
138
|
labels: {
|
137
139
|
style: {
|
@@ -162,6 +164,12 @@
|
|
162
164
|
style: {
|
163
165
|
fontSize: "12px"
|
164
166
|
}
|
167
|
+
},
|
168
|
+
plotOptions: {
|
169
|
+
areaspline: {},
|
170
|
+
series: {
|
171
|
+
marker: {}
|
172
|
+
}
|
165
173
|
}
|
166
174
|
};
|
167
175
|
|
@@ -179,10 +187,27 @@
|
|
179
187
|
|
180
188
|
var jsOptions = jsOptionsFunc(defaultOptions, hideLegend, setMin, setMax);
|
181
189
|
|
182
|
-
renderLineChart = function(element, series, opts) {
|
183
|
-
|
190
|
+
renderLineChart = function(element, series, opts, chartType) {
|
191
|
+
chartType = chartType || "spline";
|
192
|
+
var chartOptions = {};
|
193
|
+
if (chartType === "areaspline") {
|
194
|
+
chartOptions = {
|
195
|
+
plotOptions: {
|
196
|
+
areaspline: {
|
197
|
+
stacking: "normal"
|
198
|
+
},
|
199
|
+
series: {
|
200
|
+
marker: {
|
201
|
+
enabled: false
|
202
|
+
}
|
203
|
+
}
|
204
|
+
}
|
205
|
+
};
|
206
|
+
}
|
207
|
+
var options = jsOptions(series, opts, chartOptions), data, i, j;
|
184
208
|
options.xAxis.type = "datetime";
|
185
|
-
options.chart =
|
209
|
+
options.chart.type = chartType;
|
210
|
+
options.chart.renderTo = element.id;
|
186
211
|
|
187
212
|
for (i = 0; i < series.length; i++) {
|
188
213
|
data = series[i].data;
|
@@ -197,7 +222,7 @@
|
|
197
222
|
|
198
223
|
renderPieChart = function(element, series, opts) {
|
199
224
|
var options = merge(defaultOptions, opts.library || {});
|
200
|
-
options.chart =
|
225
|
+
options.chart.renderTo = element.id;
|
201
226
|
options.series = [{
|
202
227
|
type: "pie",
|
203
228
|
name: "Value",
|
@@ -206,9 +231,11 @@
|
|
206
231
|
new Highcharts.Chart(options);
|
207
232
|
};
|
208
233
|
|
209
|
-
renderColumnChart = function(element, series, opts) {
|
234
|
+
renderColumnChart = function(element, series, opts, chartType) {
|
235
|
+
chartType = chartType || "column";
|
210
236
|
var options = jsOptions(series, opts), i, j, s, d, rows = [];
|
211
|
-
options.chart =
|
237
|
+
options.chart.type = chartType;
|
238
|
+
options.chart.renderTo = element.id;
|
212
239
|
|
213
240
|
for (i = 0; i < series.length; i++) {
|
214
241
|
s = series[i];
|
@@ -234,7 +261,7 @@
|
|
234
261
|
for (i = 0; i < series.length; i++) {
|
235
262
|
d = [];
|
236
263
|
for (j = 0; j < categories.length; j++) {
|
237
|
-
d.push(rows[categories[j]][i]);
|
264
|
+
d.push(rows[categories[j]][i] || 0);
|
238
265
|
}
|
239
266
|
|
240
267
|
newSeries.push({
|
@@ -246,6 +273,14 @@
|
|
246
273
|
|
247
274
|
new Highcharts.Chart(options);
|
248
275
|
};
|
276
|
+
|
277
|
+
renderBarChart = function(element, series, opts) {
|
278
|
+
renderColumnChart(element, series, opts, "bar");
|
279
|
+
};
|
280
|
+
|
281
|
+
renderAreaChart = function(element, series, opts) {
|
282
|
+
renderLineChart(element, series, opts, "areaspline");
|
283
|
+
};
|
249
284
|
} else if ("google" in window) { // Google charts
|
250
285
|
// load from google
|
251
286
|
var loaded = false;
|
@@ -263,6 +298,7 @@
|
|
263
298
|
|
264
299
|
// Set chart options
|
265
300
|
var defaultOptions = {
|
301
|
+
chartArea: {},
|
266
302
|
fontName: "'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Helvetica, sans-serif",
|
267
303
|
pointSize: 6,
|
268
304
|
legend: {
|
@@ -282,7 +318,8 @@
|
|
282
318
|
gridlines: {
|
283
319
|
color: "transparent"
|
284
320
|
},
|
285
|
-
baselineColor: "#ccc"
|
321
|
+
baselineColor: "#ccc",
|
322
|
+
viewWindow: {}
|
286
323
|
},
|
287
324
|
vAxis: {
|
288
325
|
textStyle: {
|
@@ -312,6 +349,14 @@
|
|
312
349
|
options.vAxis.viewWindow.max = max;
|
313
350
|
};
|
314
351
|
|
352
|
+
var setBarMin = function(options, min) {
|
353
|
+
options.hAxis.viewWindow.min = min;
|
354
|
+
};
|
355
|
+
|
356
|
+
var setBarMax = function(options, max) {
|
357
|
+
options.hAxis.viewWindow.max = max;
|
358
|
+
};
|
359
|
+
|
315
360
|
var jsOptions = jsOptionsFunc(defaultOptions, hideLegend, setMin, setMax);
|
316
361
|
|
317
362
|
// cant use object as key
|
@@ -348,22 +393,36 @@
|
|
348
393
|
return data;
|
349
394
|
};
|
350
395
|
|
396
|
+
var resize = function(callback) {
|
397
|
+
if (window.attachEvent) {
|
398
|
+
window.attachEvent("onresize", callback);
|
399
|
+
}
|
400
|
+
else if (window.addEventListener) {
|
401
|
+
window.addEventListener("resize", callback, true);
|
402
|
+
}
|
403
|
+
callback();
|
404
|
+
};
|
405
|
+
|
351
406
|
renderLineChart = function(element, series, opts) {
|
352
407
|
waitForLoaded(function() {
|
353
408
|
var options = jsOptions(series, opts);
|
354
409
|
var data = createDataTable(series, "datetime");
|
355
410
|
var chart = new google.visualization.LineChart(element);
|
356
|
-
|
411
|
+
resize( function() {
|
412
|
+
chart.draw(data, options);
|
413
|
+
});
|
357
414
|
});
|
358
415
|
};
|
359
416
|
|
360
417
|
renderPieChart = function(element, series, opts) {
|
361
418
|
waitForLoaded(function() {
|
362
|
-
var
|
363
|
-
|
364
|
-
|
365
|
-
|
419
|
+
var chartOptions = {
|
420
|
+
chartArea: {
|
421
|
+
top: "10%",
|
422
|
+
height: "80%"
|
423
|
+
}
|
366
424
|
};
|
425
|
+
var options = merge(merge(defaultOptions, chartOptions), opts.library || {});
|
367
426
|
|
368
427
|
var data = new google.visualization.DataTable();
|
369
428
|
data.addColumn("string", "");
|
@@ -371,7 +430,9 @@
|
|
371
430
|
data.addRows(series);
|
372
431
|
|
373
432
|
var chart = new google.visualization.PieChart(element);
|
374
|
-
|
433
|
+
resize( function() {
|
434
|
+
chart.draw(data, options);
|
435
|
+
});
|
375
436
|
});
|
376
437
|
};
|
377
438
|
|
@@ -380,11 +441,47 @@
|
|
380
441
|
var options = jsOptions(series, opts);
|
381
442
|
var data = createDataTable(series, "string");
|
382
443
|
var chart = new google.visualization.ColumnChart(element);
|
383
|
-
|
444
|
+
resize( function() {
|
445
|
+
chart.draw(data, options);
|
446
|
+
});
|
447
|
+
});
|
448
|
+
};
|
449
|
+
|
450
|
+
renderBarChart = function(element, series, opts) {
|
451
|
+
waitForLoaded(function() {
|
452
|
+
var chartOptions = {
|
453
|
+
hAxis: {
|
454
|
+
gridlines: {
|
455
|
+
color: "#ccc"
|
456
|
+
}
|
457
|
+
}
|
458
|
+
};
|
459
|
+
var options = jsOptionsFunc(defaultOptions, hideLegend, setBarMin, setBarMax)(series, opts, chartOptions);
|
460
|
+
var data = createDataTable(series, "string");
|
461
|
+
var chart = new google.visualization.BarChart(element);
|
462
|
+
resize( function() {
|
463
|
+
chart.draw(data, options);
|
464
|
+
});
|
465
|
+
});
|
466
|
+
};
|
467
|
+
|
468
|
+
renderAreaChart = function(element, series, opts) {
|
469
|
+
waitForLoaded(function() {
|
470
|
+
var chartOptions = {
|
471
|
+
isStacked: true,
|
472
|
+
pointSize: 0,
|
473
|
+
areaOpacity: 0.5
|
474
|
+
};
|
475
|
+
var options = jsOptions(series, opts, chartOptions);
|
476
|
+
var data = createDataTable(series, "datetime");
|
477
|
+
var chart = new google.visualization.AreaChart(element);
|
478
|
+
resize( function() {
|
479
|
+
chart.draw(data, options);
|
480
|
+
});
|
384
481
|
});
|
385
482
|
};
|
386
483
|
} else { // no chart library installed
|
387
|
-
renderLineChart = renderPieChart = renderColumnChart = function() {
|
484
|
+
renderLineChart = renderPieChart = renderColumnChart = renderBarChart = renderAreaChart = function() {
|
388
485
|
throw new Error("Please install Google Charts or Highcharts");
|
389
486
|
};
|
390
487
|
}
|
@@ -521,6 +618,14 @@
|
|
521
618
|
renderPieChart(element, perfectData, opts);
|
522
619
|
}
|
523
620
|
|
621
|
+
function processBarData(element, data, opts) {
|
622
|
+
renderBarChart(element, processSeries(data, opts, false), opts);
|
623
|
+
}
|
624
|
+
|
625
|
+
function processAreaData(element, data, opts) {
|
626
|
+
renderAreaChart(element, processSeries(data, opts, true), opts);
|
627
|
+
}
|
628
|
+
|
524
629
|
function setElement(element, data, opts, callback) {
|
525
630
|
if (typeof element === "string") {
|
526
631
|
element = document.getElementById(element);
|
@@ -534,11 +639,17 @@
|
|
534
639
|
LineChart: function(element, dataSource, opts) {
|
535
640
|
setElement(element, dataSource, opts, processLineData);
|
536
641
|
},
|
642
|
+
PieChart: function(element, dataSource, opts) {
|
643
|
+
setElement(element, dataSource, opts, processPieData);
|
644
|
+
},
|
537
645
|
ColumnChart: function(element, dataSource, opts) {
|
538
646
|
setElement(element, dataSource, opts, processColumnData);
|
539
647
|
},
|
540
|
-
|
541
|
-
setElement(element, dataSource, opts,
|
648
|
+
BarChart: function(element, dataSource, opts) {
|
649
|
+
setElement(element, dataSource, opts, processBarData);
|
650
|
+
},
|
651
|
+
AreaChart: function(element, dataSource, opts) {
|
652
|
+
setElement(element, dataSource, opts, processAreaData);
|
542
653
|
}
|
543
654
|
};
|
544
655
|
|
data/lib/chartkick/helper.rb
CHANGED
@@ -16,6 +16,14 @@ module Chartkick
|
|
16
16
|
chartkick_chart "ColumnChart", data_source, options
|
17
17
|
end
|
18
18
|
|
19
|
+
def bar_chart(data_source, options = {})
|
20
|
+
chartkick_chart "BarChart", data_source, options
|
21
|
+
end
|
22
|
+
|
23
|
+
def area_chart(data_source, options = {})
|
24
|
+
chartkick_chart "AreaChart", data_source, options
|
25
|
+
end
|
26
|
+
|
19
27
|
private
|
20
28
|
|
21
29
|
def chartkick_chart(klass, data_source, options, &block)
|
data/lib/chartkick/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chartkick
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kane
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-07-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|