-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcharts.js
382 lines (358 loc) · 12.7 KB
/
charts.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
'use strict';
var c3 = require('c3');
var m = require('mithril');
var $ = require('jquery');
var widgetUtils = require('js/search_dashboard/widgetUtils');
require('c3/c3.css');
require('./css/search-widget.css');
//This module contains a bunch of generic charts and parsers for formating the correct data for them
var COLORBREWER_COLORS = [[166, 206, 227], [31, 120, 180], [178, 223, 138], [51, 160, 44], [251, 154, 153], [227, 26, 28], [253, 191, 111], [255, 127, 0], [202, 178, 214], [106, 61, 154], [255, 255, 153], [177, 89, 40]]
var charts = {};
function calculateDistanceBetweenColors(color1, color2) {
return [Math.floor((color1[0] + color2[0]) / 2),
Math.floor((color1[1] + color2[1]) / 2),
Math.floor((color1[2] + color2[2]) / 2)];
}
function rgbToHex(rgb) {
var rgb = rgb[2] + (rgb[1] << 8) + (rgb[0] << 16);
return '#' + (0x1000000 + rgb).toString(16).substring(1);
}
/**
* Returns a colors in the middle of current colors
*
* @param {Array} colorsUsed: colors used
* @return {Array} new colors to use
*/
function getNewColors (colorsUsed) {
var newColors = [];
for (var i=0; i < colorsUsed.length-1; i++) {
newColors.push(calculateDistanceBetweenColors(colorsUsed[i], colorsUsed[i + 1]));
}
return newColors;
};
/**
* Returns a requested number of unique complementary colors
*
* @param {integer} numColors: number of colors to return
* @return {Array} Array of Hex color values
*/
charts.generateColors = function(numColors) {
var colorsToGenerate = COLORBREWER_COLORS.slice();
var colorsUsed = [];
var colorsOut = [];
while (colorsOut.length < numColors) {
var color = colorsToGenerate.shift();
if (typeof color === 'undefined'){
colorsToGenerate = getNewColors(colorsUsed);
colorsUsed = [];
} else {
colorsUsed.push(color);
colorsOut.push(rgbToHex(color));
}
}
return colorsOut;
};
/**
* Finds the first range filter and applies its bounds to the timegraphs zoom.
* This is useful when instantiating a page from URL
*
* @param {Object} request: The request to get filter bounds from
* @return {Array} Zoom bounds in int format (time since epoch in MS)
*/
charts.getZoomFromTimeRangeFilter = function(request){
var zoom = null;
request.userDefinedANDFilters.some(function(filterString) {
var filterParts = filterString.split('='); //remove lock qualifier if it exists
if (filterParts[1] !== undefined) {return; } //there is a lock, so do nothing with this filter
var parts = filterParts[0].split(':');
var type = parts[0];
if (type === 'range') { //TODO this assumes all range filters work on dates, better would be to check if field matches the 'date_created'
zoom = [parseInt(parts[2]), parseInt(parts[3])]; //also this will be the last range filter that is returned...
}
});
return zoom;
};
/**
* Mithril component for the timeseries object
*/
charts.timeSeries = {
view: function(ctrl, params){
var vm = params.vm;
var widget = params.widget;
var parsedData = widget.display.parser(vm.requests[widget.display.reqRequests[0]].data, widget.levelNames, vm, widget);
parsedData.zoom = charts.getZoomFromTimeRangeFilter(vm.requests[widget.display.reqRequests[0]]);
var chartSetup = charts.timeSeriesChart(parsedData, vm, widget);
return charts.updateC3(vm, chartSetup, widget.id);
}
};
/**
* Mithril component for the chart object
*/
charts.donut = {
view: function(ctrl, params){
var vm = params.vm;
var widget = params.widget;
var parsedData = widget.display.parser(vm.requests[widget.display.reqRequests[0]].data, widget.levelNames, vm, widget);
var chartSetup = charts.donutChart(parsedData, vm, widget);
return charts.updateC3(vm, chartSetup, widget.id);
}
};
/**
* Wraps and returns a c3 chart in a component, or updates already created chart
* Only updates when an update to this widget has been requested
*
* @param {Object} c3ChartSetup: A fully setup c3 chart object
* @param {Object} vm: vm of the searchDashboard
* @param {Object} divID: id of the chart (name of widget)
* @return {Object} c3 chart wrapped in component
*/
charts.updateC3 = function(vm, c3ChartSetup, divID) {
return m('div.c3-chart-padding', {id: divID,
config: function(element, isInit, context){
if (!isInit) {
vm.widgets[divID].handle = c3.generate(c3ChartSetup);
return vm.widgets[divID].handle;
}
if (!widgetUtils.updateTriggered(divID,vm)) {return; }
vm.widgets[divID].handle.load({
columns: c3ChartSetup.data.columns,
unload: true
});
}
});
};
charts.getChangedColumns = function(oldCols, newCols){
var changedCols = [];
$.each(newCols, function(i, col){
if($.inArray(col, oldCols) === -1) changedCols.push(col);
});
};
/**
* Creates a c3 donut chart component
*
* @param {Object} vm: vm of the searchDashboard
* @param {Object} widget: params of the widget that chart is being created for
* @return {Object} c3 chart wrapped in component
*/
charts.donutChart = function (data, vm, widget) {
data.onclick = widget.display.callbacks.onclick ? widget.display.callbacks.onclick.bind({
vm: vm,
widget: widget
}) : undefined;
data.type = 'donut';
return {
bindto: '#' + widget.id,
size: {
height: widget.size[1]
},
data: data,
donut: {
title: widget.display.title ? widget.display.title : data.title,
label: {format: widget.display.labelFormat || undefined}
},
legend: {
show: true,
position: 'right',
item : {onclick: data.onclick}
}
};
};
/**
* Creates a c3 histogram chart component //NOT USED, UNTESTED!
*
* @param {Object} vm: vm of the searchDashboard
* @param {Object} widget: params of the widget that chart is being created for
* @return {Object} c3 chart wrapped in component
*/
charts.barChart = function (data, vm, widget) {
data.onclick = widget.display.callbacks.onclick ? widget.display.callbacks.onclick.bind({
vm: vm,
widget: widget
}) : undefined;
data.type = 'bar';
return {
bindto: '#' + widget.id,
size: {
height: widget.size[1],
},
data: data,
tooltip: {
grouped: false
},
legend: {
position: 'right'
},
axis: {
x: {
tick: {
format: function (d) {return ''; },
},
label: {
text: widget.display.xLabel ? widget.display.xLabel : '',
position: 'outer-center'
}
},
y: {
label: {
text: widget.display.yLabel ? widget.display.yLabel : '',
position: 'outer-middle'
},
tick: {
format: function (x) {
if (x !== Math.floor(x)) {
return '';
}
return x;
}
}
},
rotated: true
}
};
};
/**
* Creates a c3 timeseries chart component after parsing raw data
*
* @param {Object} vm: vm of the searchDashboard
* @param {Object} widget: params of the widget that chart is being created for
* @return {Object} c3 chart wrapped in component
*/
charts.timeSeriesChart = function (data, vm, widget) {
data.type = widget.display.type ? widget.display.type : 'area-spline';
if (!data.zoom && widget.handle) {widget.handle.unzoom(); }
return {
bindto: '#' + widget.id,
size: {
height: widget.size[1]
},
data: data,
subchart: {
show: true,
size: {
height: 30
},
onbrush: widget.display.callbacks.onbrushOfSubgraph ? widget.display.callbacks.onbrushOfSubgraph.bind({
vm: vm,
widget: widget,
bounds: data.bounds,
}) : undefined
},
axis: {
x: {
label: {
text: widget.display.xLabel ? widget.display.xLabel : '',
position: 'outer-center'
},
extent: data.zoom,
type: 'timeseries',
tick: {
format: function (d) {return widgetUtils.timeSinceEpochInMsToMMYY(d); }
}
},
y: {
label: {
text: widget.display.yLabel ? widget.display.yLabel : '',
position: 'outer-middle'
},
tick: {
format: function (x) {
if (x !== Math.floor(x)) {
return '';
}
return x;
}
}
}
},
legend: {
position: 'inset',
item: {
onclick: widget.display.callbacks.onclickOfLegend ? widget.display.callbacks.onclickOfLegend.bind({
vm: vm,
widget: widget
}) : undefined
}
},
padding:{
right: 15
},
tooltip: {
grouped: false
}
};
};
/**
* Parses a single level of elastic search data for a c3 single level chart (such as a donut)
*
* @param {Object} data: raw elastic aggregation Data to parse
* @param {Object} levelNames: names of each level (one in this case)
* @return {Object} parsed data
*/
charts.singleLevelAggParser = function (data, levelNames, vm, widget) {
var chartData = {};
chartData.name = levelNames[0];
chartData.columns = [];
chartData.rawX = [];
chartData.colors = {};
chartData.type = 'donut';
var count = 0;
var hexColors = widget.display.customColors ?
widget.display.customColors : charts.generateColors(data.aggregations[levelNames[0]].buckets.length);
var i = 0;
data.aggregations[levelNames[0]].buckets.forEach(
function (bucket) {
chartData.columns.push([bucket.key, bucket.doc_count]);
count = count + (bucket.doc_count ? 1 : 0);
chartData.colors[bucket.key] = hexColors[i];
i = i + 1;
}
);
chartData.title = count.toString() + ' ' + (count !== 1 ? levelNames[0] : levelNames[0].slice(0,-1));
$('.c3-chart-arcs-title').text(chartData.title); //dynamically update chart title
return chartData;
};
/**
* Parses a single level of elastic search data for a c3 two level chart (such as a timeseries or histogram)
*
* @param {Object} data: raw elastic aggregation Data to parse
* @param {Object} levelNames: names of each level (two in this case)
* @return {Object} parsed data
*/
charts.twoLevelAggParser = function (data, levelNames, vm, widget) {
var chartData = {};
chartData.name = levelNames[0];
chartData.columns = [];
chartData.colors = {};
chartData.x = 'x';
chartData.groups = [];
var grouping = [];
grouping.push('x');
var hexColors = widget.display.customColors ?
widget.display.customColors : charts.generateColors(data.aggregations[levelNames[0]].buckets.length);
var i = 0;
var xCol = [];
if(data.aggregations[levelNames[0]]) {
data.aggregations[levelNames[0]].buckets.forEach(
function (levelOneItem) {
chartData.colors[levelOneItem.key] = hexColors[i];
var column = [levelOneItem.key];
grouping.push(levelOneItem.key);
levelOneItem[levelNames[1]].buckets.forEach(function (levelTwoItem) {
column.push(levelTwoItem.doc_count);
if (i === 0) {
xCol.push(levelTwoItem.key);
}
});
chartData.columns.push(column);
i = i + 1;
}
);
}
chartData.groups.push(grouping);
xCol.unshift('x');
chartData.columns.unshift(xCol);
chartData.bounds = [chartData.columns[0][1], chartData.columns[0][chartData.columns[0].length-1]]; //get bounds of chart
chartData.zoom = charts.getZoomFromTimeRangeFilter(vm.requests.mainRequest, chartData.bounds); //TODO the request name should not be here...
return chartData;
};
module.exports = charts;