-
Notifications
You must be signed in to change notification settings - Fork 1
/
table_chart.js
428 lines (390 loc) · 13.7 KB
/
table_chart.js
1
2
3
4
5
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
37
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
66
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
96
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
141
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
180
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
220
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
263
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
(function($) {
Drupal.behaviors.table_chart = {
attach: function(context, settings) {
var count = 0;
$('.table-chart').each(function(){
count++;
$(this).once('.table-chart').each(function(){
var wrapper = $(this);
var table = wrapper.find('table');
var chartLibrary = table.data('charting-library');
// Check if the charting library and based on that formulate the table from the json object to how we can formulate it.
switch(chartLibrary) {
case 'chartist':
chartist(wrapper, table, count);
break;
case 'morrisjs':
morrisJs(wrapper, table, count);
break;
}
});
});
}
};
// Chartist data formulate data.
function chartist(wrapper, table, count) {
var options = {};
var headings = [];
var data_labels = [];
var data_series = [];
$(table).find("thead tr th").each(function(){
headings.push($(this).text());
});
var table_data = table.tableToJSON();
// Fist element in the headings will the labels and the 2 and plus items will the series.
$(table_data).each(function() {
$(this).each(function(i) {
$.each($(this)[i], function(key, value) {
// Check if its first heading element.
var first_element = $(headings).first();
if (first_element[0] == key) {
// first heading element so make it a label element.
data_labels.push(value);
}
else {
var item_array = $.inArray(key, headings);
// negate 1 to remote the first table label element.
item_array = item_array - 1;
if (typeof data_series[item_array] === 'undefined') {
data_series[item_array] = [];
data_series[item_array].push(value);
}
else {
data_series[item_array].push(value);
}
}
});
});
});
// Create the chartis json object to create the markup.
var data = {
labels: data_labels,
series: data_series,
};
var optKeys = [
['seriesBarDistance', 'int', 15],
['low', 'int', undefined],
['high', 'int', undefined],
['width', 'str', undefined],
['height', 'str', undefined],
['referenceValue', 'int', 0],
['stackBars', 'bool', false],
['stackMode', 'str', 'accumulate'],
['horizontalBars', 'bool', false],
['distributeSeries', 'bool', false],
['reverseData', 'bool', false],
['showGridBackground', 'bool', false],
['showLine', 'bool', true],
['showPoint', 'bool', true],
['showArea', 'bool', false],
['areaBase', 'int', 0],
['lineSmooth', 'bool', true],
['showGridBackground', 'bool', false],
['fullWidth', 'bool', false],
['reverseData', 'false', false],
['chartPadding', 'int', 5],
['startAngle', 'int', 0],
['total', 'int', undefined],
['donut', 'bool', false],
['donutSolid', 'bool', false],
['donutWidth', 'bool', 60],
['showLabel', 'bool', true],
['labelOffset', 'int', 0],
['labelPosition', 'str', 'inside'],
['labelDirection', 'str', 'neutral'],
['reverseData', 'bool', false],
['ignoreEmptyValues', 'bool', false],
];
options = generateOptionsList(optKeys, table, 'chartist');
options.element = 'chartist-chart-' + count;
// Prepare display
table.addClass('hidden');
wrapper.append('<div id="' + options.element + '" class="table-chart-chartist-div"></div>');
// Create a new line chart object where as first parameter we pass in a selector
// that is resolving to our chart container element. The Second parameter
// is the actual data object.
var chartType = table.data('chartist-type');
// If the variable was not set the default to line chart type.
if (typeof chartType == 'undefined') {
chartType = 'line';
}
switch(chartType.toLowerCase()) {
case 'bar':
new Chartist.Bar('#' + options.element, data, options);
break;
case 'line':
new Chartist.Line('#' + options.element, data, options);
break;
case 'pie':
// The pie data is formulated differently.
var pie_data = {
series: data_series[0],
};
new Chartist.Pie('#' + options.element, pie_data, options);
break;
}
}
// Morris js data formulating.
function morrisJs(wrapper, table, count) {
// Chart settings.
var options = {};
// List of settings along with their type and default value.
var optKeys = [
['colors', 'array', null],
['stacked', 'bool', false],
['resize', 'bool', false],
['type', 'str', 'line'],
['lineWidth', 'str', null],
['pointSize', 'str',null],
['pointFillColors', 'str', null],
['pointStrokeColors', 'str', null],
['ymax', 'str', null],
['ymin', 'str', null],
['smooth', 'bool', true],
['hideHover', 'bool', false],
['hoverCallback', 'str', null],
['parseTime', 'bool', true],
['postUnits', 'str', null],
['preUnits', 'str', null],
['dateFormat', 'str', null],
['xLabels', 'str', null],
['xLabelFormat', 'str', null],
['yLabelFormat', 'str', null],
['goals', 'array', null],
['goalStrokeWidth', 'str', null],
['goalLineColors', 'array', null],
['events', 'str', null],
['eventStrokeWidth', 'str', null],
['eventLineColors', 'array', null],
['continuousLine', 'str', null],
['axes', 'bool', true],
['grid', 'bool', true],
['gridTextColor', 'str', null],
['gridTextSize', 'int', null],
['gridTextFamily', 'str', null],
['gridTextWeight', 'str', null],
['fillOpacity', 'float', null],
['behaveLikeLine', 'bool', null],
['xkey', 'str', null],
['ykeys', 'array', []],
['labels', 'array', null]
];
options = generateOptionsList(optKeys, table, 'morris');
options.element = 'morris-chart-' + count;
// Data settings.
// Ignoring the first column (i.e. 0) has some odd behavior. Zero (0)
// being misinterpreted as the emtpy array value. So we treat this
// case separately.
if (table.data('tabletojson-ignoreColumns') == 0) {
options.ignoreColumns = [0];
}
else {
options.ignoreColumns = table.data('tabletojson-ignoreColumns') ? String(table.data('tabletojson-ignoreColumns')).split(',') : [];
}
// Same issue as above
if (table.data('tabletojson-onlyColumns') == 0) {
options.onlyColumns = [0];
}
else {
options.onlyColumns = table.data('tabletojson-onlyColumns') ? table.data('tabletojson-onlyColumns').split(',') : null;
}
options.ignoreHiddenRows = table.data('tabletojson-ignoreHiddenRows') ? true == table.data('tabletojson-ignoreHiddenRows') : true;
options.headings = table.data('tabletojson-headings') ? table.data('tabletojson-headings').split(',') : null;
options.keys = [];
// Data preperation
// Ensure all the values are true integers and not string numbers
options.ignoreColumns = options.ignoreColumns.map(function (x) {
return parseInt(x, 10);
});
// Read the table data with the given configuration
options.table_data = table.tableToJSON({
ignoreColumns: options.ignoreColumns,
onlyColumns: options.onlyColumns,
ignoreHiddenRows: options.ignoreHiddenRows,
headings: options.headings
});
// Get the list of keys
for (var i in options.table_data) {
for (var j in options.table_data[i]) {
options.keys.push(j);
}
break;
}
// Prepare display
table.addClass('hidden');
wrapper.append('<div id="' + options.element + '" class="morris-chart" style="height: 250px;"></div> <a href="#" class="button toggle-table" title="'+ Drupal.t("Toggle Data Table") +'">' + Drupal.t("Show data") + '</a>');
// Draw the chart with the given options
drawChart(options);
wrapper.find('.button.toggle-table').click(function(event){
table.toggleClass('element-invisible');
$(this).toggleClass('down');
if (table.hasClass('element-invisible')) {
$(this).text(Drupal.t('Show Data'));
}
else {
$(this).text(Drupal.t('Hide Data'));
}
event.preventDefault();
});
}
function generateOptionsList(optKeys, table, charting_type) {
var options = [];
// Extract settings from data attributes if provided.
$.each(optKeys, function(idx, optData) {
var key = optData[0],
type = optData[1],
defaultValue = optData[2];
// Check for original camel case and lowercase data attribute names, as some browsers lowercase the attribute names.
var attrKey = null
if (table.data(charting_type + '-' + key)) {
attrKey = key;
}
else if (table.data(charting_type + '-' + key.toLowerCase())) {
attrKey = key.toLowerCase();
}
if (attrKey !== null) {
if (type == 'str') {
options[key] = table.data(charting_type + '-' + attrKey);
}
else if (type == 'bool') {
options[key] = !!table.data(charting_type + '-' + attrKey);
}
else if (type == 'int') {
options[key] = parseInt(table.data(charting_type + '-' + attrKey), 10);
}
else if (type == 'float') {
options[key] = parseFloat(table.data(charting_type + '-' + attrKey));
}
else if (type == 'array') {
options[key] = table.data(charting_type + '-' + attrKey).split(',');
}
}
else {
options[key] = defaultValue;
}
});
return options;
}
/**
* Generate the chart with the given defaults
*/
function drawChart(options) {
// Set global settings
var settings = {
element: options.element,
data: options.table_data,
resize: options.resize
};
// Set per-type settings
switch (options.type) {
case 'bar':
// Add optional settings
if (null !== options.colors && options.colors != undefined) {
settings.barColors = options.colors;
}
if (options.stacked) {
settings.stacked = options.stacked;
}
else {
settings.stacked = settings.stacked;
}
if (null !== options.hideHover) {
settings.hideHover = options.hideHover;
}
if (null !== options.hoverCallback) {
settings.hoverCallback = options.hoverCallback;
}
if (null !== options.axes) {
settings.axes = options.axes;
}
if (null !== options.grid) {
settings.grid = options.grid;
}
if (null !== options.gridTextColor) {
settings.gridTextColor = options.gridTextColor;
}
if (null !== options.gridTextSize) {
settings.gridTextSize = options.gridTextSize;
}
if (null !== options.gridTextFamily) {
settings.gridTextFamily = options.gridTextFamily;
}
if (null !== options.gridTextWeight) {
settings.gridTextWeight = options.gridTextWeight;
}
if (null !== options.hideHover) {
settings.hideHover = options.hideHover;
}
// Add required settings
if (null != options.xkey) {
settings.xkey = options.xkey;
}
else {
settings.xkey = options.keys.shift();
}
if (options.ykeys.length > 0) {
settings.ykeys = options.ykeys;
}
else {
settings.ykeys = options.keys;
}
if (null != options.labels) {
settings.labels = options.labels;
}
else {
settings.labels = settings.ykeys;
}
new Morris.Bar(settings);
break;
case 'donut':
if (null !== options.colors && options.colors != undefined) {
settings.colors = options.colors;
}
if (null !== options.formatter) {
settings.formatter = options.formatter;
}
new Morris.Donut(settings);
break;
case 'area':
// Add optional settings
if (null !== options.behaveLikeLine) {
settings.behaveLikeLine = options.behaveLikeLine;
}
// Area and Line share all the same configuration except the one option
// So we fall into the default case and check at the end which type to
// generate.
case 'line':
default:
// Add optional settings
if (null !== options.colors && options.colors != undefined) {
settings.lineColors = options.colors;
}
// Add required settings
if (null != options.xkey) {
settings.xkey = options.xkey;
}
else {
settings.xkey = options.keys.shift();
}
if (options.ykeys.length > 0) {
settings.ykeys = options.ykeys;
}
else {
settings.ykeys = options.keys;
}
if (null != options.labels) {
settings.labels = options.labels;
}
else {
settings.labels = settings.ykeys;
}
// @todo adjust line settings
if (options.type == 'area') {
new Morris.Area(settings);
}
else {
new Morris.Line(settings);
}
}
}
}(jQuery));