happy_seed 0.0.6 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/happy_seed_plugin +14 -0
- data/happy_seed.rb +1 -1
- data/lib/generators/happy_seed/admin/admin_generator.rb +23 -1
- data/lib/generators/happy_seed/admin/templates/app/admin/campaigns.rb +36 -0
- data/lib/generators/happy_seed/admin/templates/app/admin/dashboard.rb +54 -0
- data/lib/generators/happy_seed/admin/templates/app/admin/newsletter.rb +48 -0
- data/lib/generators/happy_seed/admin/templates/app/controllers/admin/stats_controller.rb +15 -0
- data/lib/generators/happy_seed/admin/templates/app/views/admin/_chart.html.haml +5 -0
- data/lib/generators/happy_seed/admin/templates/docs/README.07.admin.rdoc +3 -0
- data/lib/generators/happy_seed/admin/templates/spec/controllers/admin/stats_controller_spec.rb +40 -0
- data/lib/generators/happy_seed/admin/templates/spec/factories/admin_users.rb +7 -0
- data/lib/generators/happy_seed/admin/templates/vendor/assets/javascripts/chartkick.js +830 -0
- data/lib/generators/happy_seed/{foreman/foreman_generator.rb → base/base_generator.rb} +15 -1
- data/lib/generators/happy_seed/{foreman → base}/templates/.env +0 -0
- data/lib/generators/happy_seed/{foreman → base}/templates/.foreman +0 -0
- data/lib/generators/happy_seed/{foreman → base}/templates/Procfile +0 -0
- data/lib/generators/happy_seed/{foreman → base}/templates/app/controllers/setup_controller.rb +0 -0
- data/lib/generators/happy_seed/{foreman → base}/templates/app/views/setup/index.html.haml +0 -0
- data/lib/generators/happy_seed/{foreman → base}/templates/application_controller.rb +0 -0
- data/lib/generators/happy_seed/{foreman → base}/templates/config/unicorn.rb +0 -0
- data/lib/generators/happy_seed/base/templates/docs/README.00.base.rdoc +43 -0
- data/lib/generators/happy_seed/{foreman → base}/templates/spec/controllers/application_controller_spec.rb +0 -0
- data/lib/generators/happy_seed/{foreman → base}/templates/spec/controllers/setup_controller_spec.rb +0 -0
- data/lib/generators/happy_seed/bootstrap/templates/app/views/application/_footer.html.haml +7 -6
- data/lib/generators/happy_seed/bootstrap/templates/docs/README.01.bootstrap.rdoc +6 -0
- data/lib/generators/happy_seed/devise/devise_generator.rb +3 -0
- data/lib/generators/happy_seed/devise/templates/docs/README.03.devise.rdoc +5 -3
- data/lib/generators/happy_seed/devise/templates/spec/factories/users.rb +15 -0
- data/lib/generators/happy_seed/facebook/templates/docs/README.06.facebook.rdoc +4 -0
- data/lib/generators/happy_seed/omniauth/omniauth_generator.rb +1 -0
- data/lib/generators/happy_seed/omniauth/templates/docs/README.04.omniauth.rdoc +9 -5
- data/lib/generators/happy_seed/plugin/plugin_generator.rb +32 -0
- data/lib/generators/happy_seed/plugin/templates/.autotest +7 -0
- data/lib/generators/happy_seed/plugin/templates/.rspec +3 -0
- data/lib/generators/happy_seed/plugin/templates/Rakefile +25 -0
- data/lib/generators/happy_seed/plugin/templates/gemspec +8 -0
- data/lib/generators/happy_seed/plugin/templates/spec/rails_helper.rb +43 -0
- data/lib/generators/happy_seed/plugin/templates/spec/spec_helper.rb +20 -0
- data/lib/generators/happy_seed/splash/templates/app/assets/stylesheets/splash.css.scss +4 -74
- data/lib/generators/happy_seed/splash/templates/app/views/layouts/splash.html.haml +6 -5
- data/lib/generators/happy_seed/splash/templates/app/views/splash/index.html.haml +66 -178
- data/lib/generators/happy_seed/splash/templates/docs/README.02.splash.rdoc +6 -4
- data/lib/generators/happy_seed/twitter/templates/docs/README.05.twitter.rdoc +3 -1
- data/lib/happy_seed/version.rb +1 -1
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/test.log +5 -0
- metadata +35 -13
- data/lib/generators/happy_seed/foreman/templates/docs/README.00.base.rdoc +0 -34
@@ -0,0 +1,830 @@
|
|
1
|
+
@@ -0,0 +1,829 @@
|
2
|
+
/*
|
3
|
+
* Chartkick.js
|
4
|
+
* Create beautiful Javascript charts with minimal code
|
5
|
+
* https://github.com/ankane/chartkick.js
|
6
|
+
* v1.2.2
|
7
|
+
* MIT License
|
8
|
+
*/
|
9
|
+
|
10
|
+
/*jslint browser: true, indent: 2, plusplus: true, vars: true */
|
11
|
+
|
12
|
+
(function (window) {
|
13
|
+
'use strict';
|
14
|
+
|
15
|
+
var config = window.Chartkick || {};
|
16
|
+
var Chartkick, ISO8601_PATTERN, DECIMAL_SEPARATOR, adapters = [];
|
17
|
+
|
18
|
+
// helpers
|
19
|
+
|
20
|
+
function isArray(variable) {
|
21
|
+
return Object.prototype.toString.call(variable) === "[object Array]";
|
22
|
+
}
|
23
|
+
|
24
|
+
function isFunction(variable) {
|
25
|
+
return variable instanceof Function;
|
26
|
+
}
|
27
|
+
|
28
|
+
function isPlainObject(variable) {
|
29
|
+
return !isFunction(variable) && variable instanceof Object;
|
30
|
+
}
|
31
|
+
|
32
|
+
// https://github.com/madrobby/zepto/blob/master/src/zepto.js
|
33
|
+
function extend(target, source) {
|
34
|
+
var key;
|
35
|
+
for (key in source) {
|
36
|
+
if (isPlainObject(source[key]) || isArray(source[key])) {
|
37
|
+
if (isPlainObject(source[key]) && !isPlainObject(target[key])) {
|
38
|
+
target[key] = {};
|
39
|
+
}
|
40
|
+
if (isArray(source[key]) && !isArray(target[key])) {
|
41
|
+
target[key] = [];
|
42
|
+
}
|
43
|
+
extend(target[key], source[key]);
|
44
|
+
} else if (source[key] !== undefined) {
|
45
|
+
target[key] = source[key];
|
46
|
+
}
|
47
|
+
}
|
48
|
+
}
|
49
|
+
|
50
|
+
function merge(obj1, obj2) {
|
51
|
+
var target = {};
|
52
|
+
extend(target, obj1);
|
53
|
+
extend(target, obj2);
|
54
|
+
return target;
|
55
|
+
}
|
56
|
+
|
57
|
+
// https://github.com/Do/iso8601.js
|
58
|
+
ISO8601_PATTERN = /(\d\d\d\d)(\-)?(\d\d)(\-)?(\d\d)(T)?(\d\d)(:)?(\d\d)?(:)?(\d\d)?([\.,]\d+)?($|Z|([\+\-])(\d\d)(:)?(\d\d)?)/i;
|
59
|
+
DECIMAL_SEPARATOR = String(1.5).charAt(1);
|
60
|
+
|
61
|
+
function parseISO8601(input) {
|
62
|
+
var day, hour, matches, milliseconds, minutes, month, offset, result, seconds, type, year;
|
63
|
+
type = Object.prototype.toString.call(input);
|
64
|
+
if (type === '[object Date]') {
|
65
|
+
return input;
|
66
|
+
}
|
67
|
+
if (type !== '[object String]') {
|
68
|
+
return;
|
69
|
+
}
|
70
|
+
if (matches = input.match(ISO8601_PATTERN)) {
|
71
|
+
year = parseInt(matches[1], 10);
|
72
|
+
month = parseInt(matches[3], 10) - 1;
|
73
|
+
day = parseInt(matches[5], 10);
|
74
|
+
hour = parseInt(matches[7], 10);
|
75
|
+
minutes = matches[9] ? parseInt(matches[9], 10) : 0;
|
76
|
+
seconds = matches[11] ? parseInt(matches[11], 10) : 0;
|
77
|
+
milliseconds = matches[12] ? parseFloat(DECIMAL_SEPARATOR + matches[12].slice(1)) * 1000 : 0;
|
78
|
+
result = Date.UTC(year, month, day, hour, minutes, seconds, milliseconds);
|
79
|
+
if (matches[13] && matches[14]) {
|
80
|
+
offset = matches[15] * 60;
|
81
|
+
if (matches[17]) {
|
82
|
+
offset += parseInt(matches[17], 10);
|
83
|
+
}
|
84
|
+
offset *= matches[14] === '-' ? -1 : 1;
|
85
|
+
result -= offset * 60 * 1000;
|
86
|
+
}
|
87
|
+
return new Date(result);
|
88
|
+
}
|
89
|
+
}
|
90
|
+
// end iso8601.js
|
91
|
+
|
92
|
+
function negativeValues(series) {
|
93
|
+
var i, j, data;
|
94
|
+
for (i = 0; i < series.length; i++) {
|
95
|
+
data = series[i].data;
|
96
|
+
for (j = 0; j < data.length; j++) {
|
97
|
+
if (data[j][1] < 0) {
|
98
|
+
return true;
|
99
|
+
}
|
100
|
+
}
|
101
|
+
}
|
102
|
+
return false;
|
103
|
+
}
|
104
|
+
|
105
|
+
function jsOptionsFunc(defaultOptions, hideLegend, setMin, setMax, setStacked) {
|
106
|
+
return function (series, opts, chartOptions) {
|
107
|
+
var options = merge({}, defaultOptions);
|
108
|
+
options = merge(options, chartOptions || {});
|
109
|
+
|
110
|
+
// hide legend
|
111
|
+
// this is *not* an external option!
|
112
|
+
if (opts.hideLegend) {
|
113
|
+
hideLegend(options);
|
114
|
+
}
|
115
|
+
|
116
|
+
// min
|
117
|
+
if ("min" in opts) {
|
118
|
+
setMin(options, opts.min);
|
119
|
+
} else if (!negativeValues(series)) {
|
120
|
+
setMin(options, 0);
|
121
|
+
}
|
122
|
+
|
123
|
+
// max
|
124
|
+
if ("max" in opts) {
|
125
|
+
setMax(options, opts.max);
|
126
|
+
}
|
127
|
+
|
128
|
+
if (opts.stacked) {
|
129
|
+
setStacked(options);
|
130
|
+
}
|
131
|
+
|
132
|
+
if (opts.colors) {
|
133
|
+
options.colors = opts.colors;
|
134
|
+
}
|
135
|
+
|
136
|
+
// merge library last
|
137
|
+
options = merge(options, opts.library || {});
|
138
|
+
|
139
|
+
return options;
|
140
|
+
};
|
141
|
+
}
|
142
|
+
|
143
|
+
function setText(element, text) {
|
144
|
+
if (document.body.innerText) {
|
145
|
+
element.innerText = text;
|
146
|
+
} else {
|
147
|
+
element.textContent = text;
|
148
|
+
}
|
149
|
+
}
|
150
|
+
|
151
|
+
function chartError(element, message) {
|
152
|
+
setText(element, "Error Loading Chart: " + message);
|
153
|
+
element.style.color = "#ff0000";
|
154
|
+
}
|
155
|
+
|
156
|
+
function getJSON(element, url, success) {
|
157
|
+
var $ = window.jQuery || window.Zepto || window.$;
|
158
|
+
$.ajax({
|
159
|
+
dataType: "json",
|
160
|
+
url: url,
|
161
|
+
success: success,
|
162
|
+
error: function (jqXHR, textStatus, errorThrown) {
|
163
|
+
var message = (typeof errorThrown === "string") ? errorThrown : errorThrown.message;
|
164
|
+
chartError(element, message);
|
165
|
+
}
|
166
|
+
});
|
167
|
+
}
|
168
|
+
|
169
|
+
function errorCatcher(chart, callback) {
|
170
|
+
try {
|
171
|
+
callback(chart);
|
172
|
+
} catch (err) {
|
173
|
+
chartError(chart.element, err.message);
|
174
|
+
throw err;
|
175
|
+
}
|
176
|
+
}
|
177
|
+
|
178
|
+
function fetchDataSource(chart, callback) {
|
179
|
+
if (typeof chart.dataSource === "string") {
|
180
|
+
getJSON(chart.element, chart.dataSource, function (data, textStatus, jqXHR) {
|
181
|
+
chart.data = data;
|
182
|
+
errorCatcher(chart, callback);
|
183
|
+
});
|
184
|
+
} else {
|
185
|
+
chart.data = chart.dataSource;
|
186
|
+
errorCatcher(chart, callback);
|
187
|
+
}
|
188
|
+
}
|
189
|
+
|
190
|
+
// type conversions
|
191
|
+
|
192
|
+
function toStr(n) {
|
193
|
+
return "" + n;
|
194
|
+
}
|
195
|
+
|
196
|
+
function toFloat(n) {
|
197
|
+
return parseFloat(n);
|
198
|
+
}
|
199
|
+
|
200
|
+
function toDate(n) {
|
201
|
+
if (typeof n !== "object") {
|
202
|
+
if (typeof n === "number") {
|
203
|
+
n = new Date(n * 1000); // ms
|
204
|
+
} else { // str
|
205
|
+
// try our best to get the str into iso8601
|
206
|
+
// TODO be smarter about this
|
207
|
+
var str = n.replace(/ /, "T").replace(" ", "").replace("UTC", "Z");
|
208
|
+
n = parseISO8601(str) || new Date(n);
|
209
|
+
}
|
210
|
+
}
|
211
|
+
return n;
|
212
|
+
}
|
213
|
+
|
214
|
+
function toArr(n) {
|
215
|
+
if (!isArray(n)) {
|
216
|
+
var arr = [], i;
|
217
|
+
for (i in n) {
|
218
|
+
if (n.hasOwnProperty(i)) {
|
219
|
+
arr.push([i, n[i]]);
|
220
|
+
}
|
221
|
+
}
|
222
|
+
n = arr;
|
223
|
+
}
|
224
|
+
return n;
|
225
|
+
}
|
226
|
+
|
227
|
+
function sortByTime(a, b) {
|
228
|
+
return a[0].getTime() - b[0].getTime();
|
229
|
+
}
|
230
|
+
|
231
|
+
if ("Highcharts" in window) {
|
232
|
+
var HighchartsAdapter = new function () {
|
233
|
+
var Highcharts = window.Highcharts;
|
234
|
+
|
235
|
+
var defaultOptions = {
|
236
|
+
chart: {},
|
237
|
+
xAxis: {
|
238
|
+
labels: {
|
239
|
+
style: {
|
240
|
+
fontSize: "12px"
|
241
|
+
}
|
242
|
+
}
|
243
|
+
},
|
244
|
+
yAxis: {
|
245
|
+
title: {
|
246
|
+
text: null
|
247
|
+
},
|
248
|
+
labels: {
|
249
|
+
style: {
|
250
|
+
fontSize: "12px"
|
251
|
+
}
|
252
|
+
}
|
253
|
+
},
|
254
|
+
title: {
|
255
|
+
text: null
|
256
|
+
},
|
257
|
+
credits: {
|
258
|
+
enabled: false
|
259
|
+
},
|
260
|
+
legend: {
|
261
|
+
borderWidth: 0
|
262
|
+
},
|
263
|
+
tooltip: {
|
264
|
+
style: {
|
265
|
+
fontSize: "12px"
|
266
|
+
}
|
267
|
+
},
|
268
|
+
plotOptions: {
|
269
|
+
areaspline: {},
|
270
|
+
series: {
|
271
|
+
marker: {}
|
272
|
+
}
|
273
|
+
}
|
274
|
+
};
|
275
|
+
|
276
|
+
var hideLegend = function (options) {
|
277
|
+
options.legend.enabled = false;
|
278
|
+
};
|
279
|
+
|
280
|
+
var setMin = function (options, min) {
|
281
|
+
options.yAxis.min = min;
|
282
|
+
};
|
283
|
+
|
284
|
+
var setMax = function (options, max) {
|
285
|
+
options.yAxis.max = max;
|
286
|
+
};
|
287
|
+
|
288
|
+
var setStacked = function (options) {
|
289
|
+
options.plotOptions.series.stacking = "normal";
|
290
|
+
};
|
291
|
+
|
292
|
+
var jsOptions = jsOptionsFunc(defaultOptions, hideLegend, setMin, setMax, setStacked);
|
293
|
+
|
294
|
+
this.renderLineChart = function (chart, chartType) {
|
295
|
+
chartType = chartType || "spline";
|
296
|
+
var chartOptions = {};
|
297
|
+
if (chartType === "areaspline") {
|
298
|
+
chartOptions = {
|
299
|
+
plotOptions: {
|
300
|
+
areaspline: {
|
301
|
+
stacking: "normal"
|
302
|
+
},
|
303
|
+
series: {
|
304
|
+
marker: {
|
305
|
+
enabled: false
|
306
|
+
}
|
307
|
+
}
|
308
|
+
}
|
309
|
+
};
|
310
|
+
}
|
311
|
+
var options = jsOptions(chart.data, chart.options, chartOptions), data, i, j;
|
312
|
+
options.xAxis.type = chart.options.discrete ? "category" : "datetime";
|
313
|
+
options.chart.type = chartType;
|
314
|
+
options.chart.renderTo = chart.element.id;
|
315
|
+
|
316
|
+
var series = chart.data;
|
317
|
+
for (i = 0; i < series.length; i++) {
|
318
|
+
data = series[i].data;
|
319
|
+
if (!chart.options.discrete) {
|
320
|
+
for (j = 0; j < data.length; j++) {
|
321
|
+
data[j][0] = data[j][0].getTime();
|
322
|
+
}
|
323
|
+
}
|
324
|
+
series[i].marker = {symbol: "circle"};
|
325
|
+
}
|
326
|
+
options.series = series;
|
327
|
+
new Highcharts.Chart(options);
|
328
|
+
};
|
329
|
+
|
330
|
+
this.renderPieChart = function (chart) {
|
331
|
+
var chartOptions = {};
|
332
|
+
if (chart.options.colors) {
|
333
|
+
chartOptions.colors = chart.options.colors;
|
334
|
+
}
|
335
|
+
var options = merge(merge(defaultOptions, chartOptions), chart.options.library || {});
|
336
|
+
options.chart.renderTo = chart.element.id;
|
337
|
+
options.series = [{
|
338
|
+
type: "pie",
|
339
|
+
name: "Value",
|
340
|
+
data: chart.data
|
341
|
+
}];
|
342
|
+
new Highcharts.Chart(options);
|
343
|
+
};
|
344
|
+
|
345
|
+
this.renderColumnChart = function (chart, chartType) {
|
346
|
+
var chartType = chartType || "column";
|
347
|
+
var series = chart.data;
|
348
|
+
var options = jsOptions(series, chart.options), i, j, s, d, rows = [];
|
349
|
+
options.chart.type = chartType;
|
350
|
+
options.chart.renderTo = chart.element.id;
|
351
|
+
|
352
|
+
for (i = 0; i < series.length; i++) {
|
353
|
+
s = series[i];
|
354
|
+
|
355
|
+
for (j = 0; j < s.data.length; j++) {
|
356
|
+
d = s.data[j];
|
357
|
+
if (!rows[d[0]]) {
|
358
|
+
rows[d[0]] = new Array(series.length);
|
359
|
+
}
|
360
|
+
rows[d[0]][i] = d[1];
|
361
|
+
}
|
362
|
+
}
|
363
|
+
|
364
|
+
var categories = [];
|
365
|
+
for (i in rows) {
|
366
|
+
if (rows.hasOwnProperty(i)) {
|
367
|
+
categories.push(i);
|
368
|
+
}
|
369
|
+
}
|
370
|
+
options.xAxis.categories = categories;
|
371
|
+
|
372
|
+
var newSeries = [];
|
373
|
+
for (i = 0; i < series.length; i++) {
|
374
|
+
d = [];
|
375
|
+
for (j = 0; j < categories.length; j++) {
|
376
|
+
d.push(rows[categories[j]][i] || 0);
|
377
|
+
}
|
378
|
+
|
379
|
+
newSeries.push({
|
380
|
+
name: series[i].name,
|
381
|
+
data: d
|
382
|
+
});
|
383
|
+
}
|
384
|
+
options.series = newSeries;
|
385
|
+
|
386
|
+
new Highcharts.Chart(options);
|
387
|
+
};
|
388
|
+
|
389
|
+
var self = this;
|
390
|
+
|
391
|
+
this.renderBarChart = function (chart) {
|
392
|
+
self.renderColumnChart(chart, "bar");
|
393
|
+
};
|
394
|
+
|
395
|
+
this.renderAreaChart = function (chart) {
|
396
|
+
self.renderLineChart(chart, "areaspline");
|
397
|
+
};
|
398
|
+
};
|
399
|
+
adapters.push(HighchartsAdapter);
|
400
|
+
}
|
401
|
+
if (window.google && window.google.setOnLoadCallback) {
|
402
|
+
var GoogleChartsAdapter = new function () {
|
403
|
+
var google = window.google;
|
404
|
+
|
405
|
+
var loaded = {};
|
406
|
+
var callbacks = [];
|
407
|
+
|
408
|
+
var runCallbacks = function () {
|
409
|
+
var cb, call;
|
410
|
+
for (var i = 0; i < callbacks.length; i++) {
|
411
|
+
cb = callbacks[i];
|
412
|
+
call = google.visualization && ((cb.pack == "corechart" && google.visualization.LineChart) || (cb.pack == "timeline" && google.visualization.Timeline))
|
413
|
+
if (call) {
|
414
|
+
cb.callback();
|
415
|
+
callbacks.splice(i, 1);
|
416
|
+
i--;
|
417
|
+
}
|
418
|
+
}
|
419
|
+
};
|
420
|
+
|
421
|
+
var waitForLoaded = function (pack, callback) {
|
422
|
+
if (!callback) {
|
423
|
+
callback = pack;
|
424
|
+
pack = "corechart";
|
425
|
+
}
|
426
|
+
|
427
|
+
callbacks.push({pack: pack, callback: callback});
|
428
|
+
|
429
|
+
if (loaded[pack]) {
|
430
|
+
runCallbacks();
|
431
|
+
} else {
|
432
|
+
loaded[pack] = true;
|
433
|
+
|
434
|
+
// https://groups.google.com/forum/#!topic/google-visualization-api/fMKJcyA2yyI
|
435
|
+
var loadOptions = {
|
436
|
+
packages: [pack],
|
437
|
+
callback: runCallbacks
|
438
|
+
};
|
439
|
+
if (config.language) {
|
440
|
+
loadOptions.language = config.language;
|
441
|
+
}
|
442
|
+
google.load("visualization", "1", loadOptions);
|
443
|
+
}
|
444
|
+
};
|
445
|
+
|
446
|
+
// Set chart options
|
447
|
+
var defaultOptions = {
|
448
|
+
chartArea: {},
|
449
|
+
fontName: "'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Helvetica, sans-serif",
|
450
|
+
pointSize: 6,
|
451
|
+
legend: {
|
452
|
+
textStyle: {
|
453
|
+
fontSize: 12,
|
454
|
+
color: "#444"
|
455
|
+
},
|
456
|
+
alignment: "center",
|
457
|
+
position: "right"
|
458
|
+
},
|
459
|
+
curveType: "function",
|
460
|
+
hAxis: {
|
461
|
+
textStyle: {
|
462
|
+
color: "#666",
|
463
|
+
fontSize: 12
|
464
|
+
},
|
465
|
+
gridlines: {
|
466
|
+
color: "transparent"
|
467
|
+
},
|
468
|
+
baselineColor: "#ccc",
|
469
|
+
viewWindow: {}
|
470
|
+
},
|
471
|
+
vAxis: {
|
472
|
+
textStyle: {
|
473
|
+
color: "#666",
|
474
|
+
fontSize: 12
|
475
|
+
},
|
476
|
+
baselineColor: "#ccc",
|
477
|
+
viewWindow: {}
|
478
|
+
},
|
479
|
+
tooltip: {
|
480
|
+
textStyle: {
|
481
|
+
color: "#666",
|
482
|
+
fontSize: 12
|
483
|
+
}
|
484
|
+
}
|
485
|
+
};
|
486
|
+
|
487
|
+
var hideLegend = function (options) {
|
488
|
+
options.legend.position = "none";
|
489
|
+
};
|
490
|
+
|
491
|
+
var setMin = function (options, min) {
|
492
|
+
options.vAxis.viewWindow.min = min;
|
493
|
+
};
|
494
|
+
|
495
|
+
var setMax = function (options, max) {
|
496
|
+
options.vAxis.viewWindow.max = max;
|
497
|
+
};
|
498
|
+
|
499
|
+
var setBarMin = function (options, min) {
|
500
|
+
options.hAxis.viewWindow.min = min;
|
501
|
+
};
|
502
|
+
|
503
|
+
var setBarMax = function (options, max) {
|
504
|
+
options.hAxis.viewWindow.max = max;
|
505
|
+
};
|
506
|
+
|
507
|
+
var setStacked = function (options) {
|
508
|
+
options.isStacked = true;
|
509
|
+
};
|
510
|
+
|
511
|
+
var jsOptions = jsOptionsFunc(defaultOptions, hideLegend, setMin, setMax, setStacked);
|
512
|
+
|
513
|
+
// cant use object as key
|
514
|
+
var createDataTable = function (series, columnType) {
|
515
|
+
var data = new google.visualization.DataTable();
|
516
|
+
data.addColumn(columnType, "");
|
517
|
+
|
518
|
+
var i, j, s, d, key, rows = [];
|
519
|
+
for (i = 0; i < series.length; i++) {
|
520
|
+
s = series[i];
|
521
|
+
data.addColumn("number", s.name);
|
522
|
+
|
523
|
+
for (j = 0; j < s.data.length; j++) {
|
524
|
+
d = s.data[j];
|
525
|
+
key = (columnType === "datetime") ? d[0].getTime() : d[0];
|
526
|
+
if (!rows[key]) {
|
527
|
+
rows[key] = new Array(series.length);
|
528
|
+
}
|
529
|
+
rows[key][i] = toFloat(d[1]);
|
530
|
+
}
|
531
|
+
}
|
532
|
+
|
533
|
+
var rows2 = [];
|
534
|
+
for (i in rows) {
|
535
|
+
if (rows.hasOwnProperty(i)) {
|
536
|
+
rows2.push([(columnType === "datetime") ? new Date(toFloat(i)) : i].concat(rows[i]));
|
537
|
+
}
|
538
|
+
}
|
539
|
+
if (columnType === "datetime") {
|
540
|
+
rows2.sort(sortByTime);
|
541
|
+
}
|
542
|
+
data.addRows(rows2);
|
543
|
+
|
544
|
+
return data;
|
545
|
+
};
|
546
|
+
|
547
|
+
var resize = function (callback) {
|
548
|
+
if (window.attachEvent) {
|
549
|
+
window.attachEvent("onresize", callback);
|
550
|
+
} else if (window.addEventListener) {
|
551
|
+
window.addEventListener("resize", callback, true);
|
552
|
+
}
|
553
|
+
callback();
|
554
|
+
};
|
555
|
+
|
556
|
+
this.renderLineChart = function (chart) {
|
557
|
+
waitForLoaded(function () {
|
558
|
+
var options = jsOptions(chart.data, chart.options);
|
559
|
+
var data = createDataTable(chart.data, chart.options.discrete ? "string" : "datetime");
|
560
|
+
chart.chart = new google.visualization.LineChart(chart.element);
|
561
|
+
resize(function () {
|
562
|
+
chart.chart.draw(data, options);
|
563
|
+
});
|
564
|
+
});
|
565
|
+
};
|
566
|
+
|
567
|
+
this.renderPieChart = function (chart) {
|
568
|
+
waitForLoaded(function () {
|
569
|
+
var chartOptions = {
|
570
|
+
chartArea: {
|
571
|
+
top: "10%",
|
572
|
+
height: "80%"
|
573
|
+
}
|
574
|
+
};
|
575
|
+
if (chart.options.colors) {
|
576
|
+
chartOptions.colors = chart.options.colors;
|
577
|
+
}
|
578
|
+
var options = merge(merge(defaultOptions, chartOptions), chart.options.library || {});
|
579
|
+
|
580
|
+
var data = new google.visualization.DataTable();
|
581
|
+
data.addColumn("string", "");
|
582
|
+
data.addColumn("number", "Value");
|
583
|
+
data.addRows(chart.data);
|
584
|
+
|
585
|
+
chart.chart = new google.visualization.PieChart(chart.element);
|
586
|
+
resize(function () {
|
587
|
+
chart.chart.draw(data, options);
|
588
|
+
});
|
589
|
+
});
|
590
|
+
};
|
591
|
+
|
592
|
+
this.renderColumnChart = function (chart) {
|
593
|
+
waitForLoaded(function () {
|
594
|
+
var options = jsOptions(chart.data, chart.options);
|
595
|
+
var data = createDataTable(chart.data, "string");
|
596
|
+
chart.chart = new google.visualization.ColumnChart(chart.element);
|
597
|
+
resize(function () {
|
598
|
+
chart.chart.draw(data, options);
|
599
|
+
});
|
600
|
+
});
|
601
|
+
};
|
602
|
+
|
603
|
+
this.renderBarChart = function (chart) {
|
604
|
+
waitForLoaded(function () {
|
605
|
+
var chartOptions = {
|
606
|
+
hAxis: {
|
607
|
+
gridlines: {
|
608
|
+
color: "#ccc"
|
609
|
+
}
|
610
|
+
}
|
611
|
+
};
|
612
|
+
var options = jsOptionsFunc(defaultOptions, hideLegend, setBarMin, setBarMax, setStacked)(chart.data, chart.options, chartOptions);
|
613
|
+
var data = createDataTable(chart.data, "string");
|
614
|
+
chart.chart = new google.visualization.BarChart(chart.element);
|
615
|
+
resize(function () {
|
616
|
+
chart.chart.draw(data, options);
|
617
|
+
});
|
618
|
+
});
|
619
|
+
};
|
620
|
+
|
621
|
+
this.renderAreaChart = function (chart) {
|
622
|
+
waitForLoaded(function () {
|
623
|
+
var chartOptions = {
|
624
|
+
isStacked: true,
|
625
|
+
pointSize: 0,
|
626
|
+
areaOpacity: 0.5
|
627
|
+
};
|
628
|
+
var options = jsOptions(chart.data, chart.options, chartOptions);
|
629
|
+
var data = createDataTable(chart.data, chart.options.discrete ? "string" : "datetime");
|
630
|
+
chart.chart = new google.visualization.AreaChart(chart.element);
|
631
|
+
resize(function () {
|
632
|
+
chart.chart.draw(data, options);
|
633
|
+
});
|
634
|
+
});
|
635
|
+
};
|
636
|
+
|
637
|
+
this.renderGeoChart = function (chart) {
|
638
|
+
waitForLoaded(function () {
|
639
|
+
var chartOptions = {
|
640
|
+
legend: "none",
|
641
|
+
colorAxis: {
|
642
|
+
colors: chart.options.colors || ["#f6c7b6", "#ce502d"]
|
643
|
+
}
|
644
|
+
};
|
645
|
+
var options = merge(merge(defaultOptions, chartOptions), chart.options.library || {});
|
646
|
+
|
647
|
+
var data = new google.visualization.DataTable();
|
648
|
+
data.addColumn("string", "");
|
649
|
+
data.addColumn("number", "Value");
|
650
|
+
data.addRows(chart.data);
|
651
|
+
|
652
|
+
chart.chart = new google.visualization.GeoChart(chart.element);
|
653
|
+
resize(function () {
|
654
|
+
chart.chart.draw(data, options);
|
655
|
+
});
|
656
|
+
});
|
657
|
+
};
|
658
|
+
|
659
|
+
this.renderTimeline = function (chart) {
|
660
|
+
waitForLoaded("timeline", function () {
|
661
|
+
var chartOptions = {
|
662
|
+
legend: "none"
|
663
|
+
};
|
664
|
+
|
665
|
+
if (chart.options.colors) {
|
666
|
+
chartOptions.colorAxis.colors = chart.options.colors;
|
667
|
+
}
|
668
|
+
var options = merge(merge(defaultOptions, chartOptions), chart.options.library || {});
|
669
|
+
|
670
|
+
var data = new google.visualization.DataTable();
|
671
|
+
data.addColumn({type: "string", id: "Name"});
|
672
|
+
data.addColumn({type: "date", id: "Start"});
|
673
|
+
data.addColumn({type: "date", id: "End"});
|
674
|
+
data.addRows(chart.data);
|
675
|
+
|
676
|
+
chart.chart = new google.visualization.Timeline(chart.element);
|
677
|
+
|
678
|
+
resize(function () {
|
679
|
+
chart.chart.draw(data, options);
|
680
|
+
});
|
681
|
+
});
|
682
|
+
};
|
683
|
+
};
|
684
|
+
|
685
|
+
adapters.push(GoogleChartsAdapter);
|
686
|
+
}
|
687
|
+
|
688
|
+
// TODO add adapter option
|
689
|
+
// TODO remove chartType if cross-browser way
|
690
|
+
// to get the name of the chart class
|
691
|
+
function renderChart(chartType, chart) {
|
692
|
+
var i, adapter, fnName;
|
693
|
+
fnName = "render" + chartType;
|
694
|
+
|
695
|
+
for (i = 0; i < adapters.length; i++) {
|
696
|
+
adapter = adapters[i];
|
697
|
+
if (isFunction(adapter[fnName])) {
|
698
|
+
return adapter[fnName](chart);
|
699
|
+
}
|
700
|
+
}
|
701
|
+
throw new Error("No adapter found");
|
702
|
+
}
|
703
|
+
|
704
|
+
// process data
|
705
|
+
|
706
|
+
function processSeries(series, opts, time) {
|
707
|
+
var i, j, data, r, key;
|
708
|
+
|
709
|
+
// see if one series or multiple
|
710
|
+
if (!isArray(series) || typeof series[0] !== "object" || isArray(series[0])) {
|
711
|
+
series = [{name: "Value", data: series}];
|
712
|
+
opts.hideLegend = true;
|
713
|
+
} else {
|
714
|
+
opts.hideLegend = false;
|
715
|
+
}
|
716
|
+
if (opts.discrete) {
|
717
|
+
time = false;
|
718
|
+
}
|
719
|
+
|
720
|
+
// right format
|
721
|
+
for (i = 0; i < series.length; i++) {
|
722
|
+
data = toArr(series[i].data);
|
723
|
+
r = [];
|
724
|
+
for (j = 0; j < data.length; j++) {
|
725
|
+
key = data[j][0];
|
726
|
+
key = time ? toDate(key) : toStr(key);
|
727
|
+
r.push([key, toFloat(data[j][1])]);
|
728
|
+
}
|
729
|
+
if (time) {
|
730
|
+
r.sort(sortByTime);
|
731
|
+
}
|
732
|
+
series[i].data = r;
|
733
|
+
}
|
734
|
+
|
735
|
+
return series;
|
736
|
+
}
|
737
|
+
|
738
|
+
function processSimple(data) {
|
739
|
+
var perfectData = toArr(data), i;
|
740
|
+
for (i = 0; i < perfectData.length; i++) {
|
741
|
+
perfectData[i] = [toStr(perfectData[i][0]), toFloat(perfectData[i][1])];
|
742
|
+
}
|
743
|
+
return perfectData;
|
744
|
+
}
|
745
|
+
|
746
|
+
function processTime(data)
|
747
|
+
{
|
748
|
+
var i;
|
749
|
+
for (i = 0; i < data.length; i++) {
|
750
|
+
data[i][1] = toDate(data[i][1]);
|
751
|
+
data[i][2] = toDate(data[i][2]);
|
752
|
+
}
|
753
|
+
return data;
|
754
|
+
}
|
755
|
+
|
756
|
+
function processLineData(chart) {
|
757
|
+
chart.data = processSeries(chart.data, chart.options, true);
|
758
|
+
renderChart("LineChart", chart);
|
759
|
+
}
|
760
|
+
|
761
|
+
function processColumnData(chart) {
|
762
|
+
chart.data = processSeries(chart.data, chart.options, false);
|
763
|
+
renderChart("ColumnChart", chart);
|
764
|
+
}
|
765
|
+
|
766
|
+
function processPieData(chart) {
|
767
|
+
chart.data = processSimple(chart.data);
|
768
|
+
renderChart("PieChart", chart);
|
769
|
+
}
|
770
|
+
|
771
|
+
function processBarData(chart) {
|
772
|
+
chart.data = processSeries(chart.data, chart.options, false);
|
773
|
+
renderChart("BarChart", chart);
|
774
|
+
}
|
775
|
+
|
776
|
+
function processAreaData(chart) {
|
777
|
+
chart.data = processSeries(chart.data, chart.options, true);
|
778
|
+
renderChart("AreaChart", chart);
|
779
|
+
}
|
780
|
+
|
781
|
+
function processGeoData(chart) {
|
782
|
+
chart.data = processSimple(chart.data);
|
783
|
+
renderChart("GeoChart", chart);
|
784
|
+
}
|
785
|
+
|
786
|
+
function processTimelineData(chart) {
|
787
|
+
chart.data = processTime(chart.data);
|
788
|
+
renderChart("Timeline", chart);
|
789
|
+
}
|
790
|
+
|
791
|
+
function setElement(chart, element, dataSource, opts, callback) {
|
792
|
+
if (typeof element === "string") {
|
793
|
+
element = document.getElementById(element);
|
794
|
+
}
|
795
|
+
chart.element = element;
|
796
|
+
chart.options = opts || {};
|
797
|
+
chart.dataSource = dataSource;
|
798
|
+
Chartkick.charts[element.id] = chart;
|
799
|
+
fetchDataSource(chart, callback);
|
800
|
+
}
|
801
|
+
|
802
|
+
// define classes
|
803
|
+
|
804
|
+
Chartkick = {
|
805
|
+
LineChart: function (element, dataSource, opts) {
|
806
|
+
setElement(this, element, dataSource, opts, processLineData);
|
807
|
+
},
|
808
|
+
PieChart: function (element, dataSource, opts) {
|
809
|
+
setElement(this, element, dataSource, opts, processPieData);
|
810
|
+
},
|
811
|
+
ColumnChart: function (element, dataSource, opts) {
|
812
|
+
setElement(this, element, dataSource, opts, processColumnData);
|
813
|
+
},
|
814
|
+
BarChart: function (element, dataSource, opts) {
|
815
|
+
setElement(this, element, dataSource, opts, processBarData);
|
816
|
+
},
|
817
|
+
AreaChart: function (element, dataSource, opts) {
|
818
|
+
setElement(this, element, dataSource, opts, processAreaData);
|
819
|
+
},
|
820
|
+
GeoChart: function (element, dataSource, opts) {
|
821
|
+
setElement(this, element, dataSource, opts, processGeoData);
|
822
|
+
},
|
823
|
+
Timeline: function (element, dataSource, opts) {
|
824
|
+
setElement(this, element, dataSource, opts, processTimelineData);
|
825
|
+
},
|
826
|
+
charts: {}
|
827
|
+
};
|
828
|
+
|
829
|
+
window.Chartkick = Chartkick;
|
830
|
+
}(window));
|