-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathplugin_highcharts.js
458 lines (418 loc) · 11.5 KB
/
plugin_highcharts.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
// ┌────────────────────────────────────────────────────────────────────┐ \\
// │ freeboard-dynamic-highcharts-plugin │ \\
// ├────────────────────────────────────────────────────────────────────┤ \\
// │ http://blog.onlinux.fr/?tag=freeboard │ \\
// ├────────────────────────────────────────────────────────────────────┤ \\
// │ Licensed under the MIT license. │ \\
// ├────────────────────────────────────────────────────────────────────┤ \\
// │ Freeboard widget plugin for Highcharts. │ \\
// └────────────────────────────────────────────────────────────────────┘ \\
(function() {
//
// DECLARATIONS
//
var HIGHCHARTS_ID = 0;
var ONE_SECOND_IN_MILIS = 1000;
var MAX_NUM_SERIES = 3;
//
// HELPERS
//
// Get coordinates of point
function xy(obj, x, y) {
return [obj[x], obj[y]]
}
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
//
// TIME SERIES CHARTS
//
var highchartsLineWidgetSettings = [{
"name": "timeframe",
"display_name": "Timeframe (s)",
"type": "number",
"description": "Specify the last number of seconds you want to see.",
"default_value": 60
}, {
"name": "blocks",
"display_name": "Height (No. Blocks)",
"type": "number",
"default_value": 4
}, {
"name": "chartType",
"display_name": "Chart Type",
"type": "option",
"options": [{
"name": "Area",
"value": "area"
}, {
"name": "Spline",
"value": "spline"
}]
}, {
"name": "title",
"display_name": "Title",
"type": "text"
}, {
"name": "xaxis",
"display_name": "X-Axis",
"type": "calculated",
"default_value": "{\"title\":{\"text\" : \"Time\"}, \"type\": \"datetime\", \"floor\":0}"
}, {
"name": "yaxis",
"display_name": "Y-Axis",
"type": "calculated",
"default_value": "{\"title\":{\"text\" : \"Values\"}, \"minorTickInterval\":\"auto\", \"floor\":0}"
}];
for (i = 1; i <= MAX_NUM_SERIES; i++) {
var dataSource = {
"name": "series" + i,
"display_name": "Series " + i + " - Datasource",
"type": "calculated"
};
var xField = {
"name": "series" + i + "label",
"display_name": "Series " + i + " - Label",
"type": "text",
};
highchartsLineWidgetSettings.push(dataSource);
highchartsLineWidgetSettings.push(xField);
}
freeboard
.loadWidgetPlugin({
"type_name": "highcharts-timeseries",
"display_name": "Time series (Highcharts)",
"description": "Time series line chart.",
"external_scripts": [
"https://code.highcharts.com/8.0.0/highcharts.js",
"https://code.highcharts.com/8.0.0/modules/exporting.js"
],
"fill_size": true,
"settings": highchartsLineWidgetSettings,
newInstance: function(settings, newInstanceCallback) {
newInstanceCallback(new highchartsTimeseriesWidgetPlugin(
settings));
}
});
var highchartsTimeseriesWidgetPlugin = function(settings) {
var self = this;
var currentSettings = settings;
var thisWidgetId = "highcharts-widget-timeseries-" + HIGHCHARTS_ID++;
var thisWidgetContainer = $('<div class="highcharts-widget" id="' + thisWidgetId + '"></div>');
function createWidget() {
Highcharts.theme = {
global: {
useUTC: false
},
colors: ["#2b908f", "#90ee7e", "#f45b5b", "#7798BF", "#aaeeee",
"#ff0066", "#eeaaee", "#55BF3B", "#DF5353", "#7798BF", "#aaeeee"
],
chart: {
backgroundColor: null,
style: {
fontFamily: "'Open Sans', sans-serif"
},
plotBorderColor: '#606063'
},
plotShadow: false,
title: {
style: {
color: '#E0E0E3',
fontSize: '20px'
}
},
subtitle: {
style: {
color: '#E0E0E3',
textTransform: 'uppercase'
}
},
xAxis: {
gridLineColor: '#707073',
labels: {
style: {
color: '#E0E0E3'
}
},
lineColor: '#707073',
minorGridLineColor: '#505053',
tickColor: '#707073',
title: {
style: {
color: '#A0A0A3'
}
}
},
yAxis: {
gridLineColor: '#707073',
labels: {
style: {
color: '#E0E0E3'
}
},
lineColor: '#707073',
minorGridLineColor: '#505053',
tickColor: '#707073',
tickWidth: 1,
title: {
style: {
color: '#A0A0A3'
}
}
},
tooltip: {
backgroundColor: 'rgba(0, 0, 0, 0.85)',
style: {
color: '#F0F0F0'
}
},
plotOptions: {
series: {
dataLabels: {
color: '#B0B0B3'
},
marker: {
lineColor: '#333'
}
},
boxplot: {
fillColor: '#505053'
},
candlestick: {
lineColor: 'white'
},
errorbar: {
color: 'white'
}
},
legend: {
itemStyle: {
color: '#E0E0E3'
},
itemHoverStyle: {
color: '#FFF'
},
itemHiddenStyle: {
color: '#606063'
}
},
credits: {
enabled: false,
style: {
color: '#666'
}
},
labels: {
style: {
color: '#707073'
}
},
drilldown: {
activeAxisLabelStyle: {
color: '#F0F0F3'
},
activeDataLabelStyle: {
color: '#F0F0F3'
}
},
navigation: {
buttonOptions: {
symbolStroke: '#DDDDDD',
theme: {
fill: '#505053'
}
}
},
// scroll charts
rangeSelector: {
buttonTheme: {
fill: '#505053',
stroke: '#000000',
style: {
color: '#CCC'
},
states: {
hover: {
fill: '#707073',
stroke: '#000000',
style: {
color: 'white'
}
},
select: {
fill: '#000003',
stroke: '#000000',
style: {
color: 'white'
}
}
}
},
inputBoxBorderColor: '#505053',
inputStyle: {
backgroundColor: '#333',
color: 'silver'
},
labelStyle: {
color: 'silver'
}
},
navigator: {
handles: {
backgroundColor: '#666',
borderColor: '#AAA'
},
outlineColor: '#CCC',
maskFill: 'rgba(255,255,255,0.1)',
series: {
color: '#7798BF',
lineColor: '#A6C7ED'
},
xAxis: {
gridLineColor: '#505053'
}
},
scrollbar: {
barBackgroundColor: '#808083',
barBorderColor: '#808083',
buttonArrowColor: '#CCC',
buttonBackgroundColor: '#606063',
buttonBorderColor: '#606063',
rifleColor: '#FFF',
trackBackgroundColor: '#404043',
trackBorderColor: '#404043'
},
// special colors for some of the
legendBackgroundColor: 'rgba(0, 0, 0, 0.5)',
background2: '#505053',
dataLabelsColor: '#B0B0B3',
textColor: '#C0C0C0',
contrastTextColor: '#F0F0F3',
maskColor: 'rgba(255,255,255,0.3)'
};
Highcharts.setOptions(Highcharts.theme);
// Get widget configurations
var thisWidgetXAxis = JSON.parse(currentSettings.xaxis);
var thisWidgetYAxis = JSON.parse(currentSettings.yaxis);
var thisWidgetTitle = currentSettings.title;
var thisWidgetChartType = currentSettings.chartType;
//console.log('chartType:' + currentSettings.chartType + ' ' + thisWidgetChartType);
var thisWidgetSeries = [];
for (i = 1; i <= MAX_NUM_SERIES; i++) {
var datasource = currentSettings['series' + i];
if (datasource) {
var serieno = "series" + i + "label";
var label = currentSettings[serieno];
console.log('label: ', label);
var newSeries = {
id: 'series' + i,
name: label,
fillColor: {
linearGradient: {
x1: 0,
y1: 0,
x2: 0,
y2: 1
},
stops: [
[0, Highcharts.getOptions().colors[i - 1]],
//[1, 'rgba(2,0,0,0)']
[1, Highcharts.Color(Highcharts.getOptions().colors[i - 1]).setOpacity(0).get('rgba')]
]
},
data: [],
connectNulls: true
};
thisWidgetSeries.push(newSeries);
}
}
// Create widget
thisWidgetContainer
.css('height', 60 * self.getHeight() - 10 + 'px');
thisWidgetContainer.css('width', '100%');
thisWidgetContainer.highcharts({
chart: {
type: thisWidgetChartType,
animation: Highcharts.svg,
marginRight: 20
},
title: {
text: thisWidgetTitle
},
xAxis: thisWidgetXAxis,
yAxis: thisWidgetYAxis,
plotOptions: {
area: {
marker: {
enabled: false,
symbol: 'circle',
radius: 2,
hover: {
enabled: true
}
},
lineWidth: 2,
states: {
hover: {
lineWidth: 2
}
},
threshold: null
}
},
tooltip: {
formatter: function() {
return '<b>' + this.series.name + '</b><br/>' + Highcharts.dateFormat('%Y-%m-%d %H:%M:%S',
this.x) + '<br/>' + Highcharts.numberFormat(this.y, 1);
}
},
series: thisWidgetSeries
});
}
self.render = function(containerElement) {
$(containerElement).append(thisWidgetContainer);
createWidget();
}
self.getHeight = function() {
return currentSettings.blocks;
}
self.onSettingsChanged = function(newSettings) {
currentSettings = newSettings;
createWidget();
}
self.onCalculatedValueChanged = function(settingName, newValue) {
// console.log(settingName, 'newValue:', newValue);
var chart = thisWidgetContainer.highcharts();
var series = chart.get(settingName);
if (series) {
var timeframeMS = currentSettings.timeframe * ONE_SECOND_IN_MILIS;
var seriesno = settingName;
var len = series.data.length;
var shift = false;
// Check if it should shift the series
if (series.data.length > 1) {
var first = series.data[0].x;
//var last = series.data[series.data.length-1].x;
var last = new Date().getTime();
// Check if time frame is complete
var diff = last - first;
// console.log('last :', last);
// console.log('first:', first);
// console.log('diff :', diff);
if (last - first > timeframeMS) {
shift = true;
}
}
if (isNumber(newValue)) { //check if it is a real number and not text
var x = (new Date()).getTime();
// console.log('addPoint:', x,currentSettings[seriesno], Number(newValue));
var plotMqtt = [x, Number(newValue)]; //create the array+ "Y"
series.addPoint(plotMqtt, true, shift);
};
}
}
self.onDispose = function() {
return;
}
}
}());