-
Notifications
You must be signed in to change notification settings - Fork 50
/
chart.html
506 lines (448 loc) · 15.7 KB
/
chart.html
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
<!DOCTYPE html>
<html style="height:100%">
<head>
<title>HABmin Charting</title>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/highcharts.js"></script>
<script type="text/javascript">
var HABminBaseURL = "/services/habmin";
var persistenceService = "";
var supportedCharts = [
'spline',
'line',
'area',
'areaspline',
'bar',
'column'
];
var supportedMarkers = [
'circle',
'square',
'diamond',
'triangle',
'triangle-down'
];
var chartConfig = [];
var chartOptions = {
chart: {
renderTo: 'HABmin',
animation: false,
type: 'spline',
zoomType: 'x',
events: {
selection: function (event) {
event.preventDefault();
updateChart(chartConfig, Math.floor(event.xAxis[0].min), Math.ceil(event.xAxis[0].max));
}
}
},
credits: {
enabled: false
},
title: {
text: null
},
xAxis: {
type: 'datetime',
dateTimeLabelFormats: { // don't display the dummy year
month: '%e. %b',
year: '%b'
}
},
plotOptions: {
spline: {
lineWidth: 3,
states: {
hover: {
lineWidth: 5
}
},
marker: {
states: {
hover: {
enabled: true,
symbol: 'circle',
radius: 5,
lineWidth: 1
}
}
}
},
series: {
marker: {
enabled: false
}
}
},
legend: {
enabled: true
},
tooltip: {
enabled: true,
crosshairs: true,
shared: false,
formatter: function () {
return '<b>' + this.series.name + '</b><br/>' +
Highcharts.dateFormat('%H:%M:%S %a %d %b %Y', this.x) + ': ' +
Highcharts.numberFormat(this.y, 2);
}
}
};
var graphName;
var graphId;
var graphPeriod;
var graphUpdate = 0;
var configGraph;
var configChan;
var initState;
var nightsDisplay;
var rawData;
var initList = [
{variable: "configGraph", url: HABminBaseURL + "/persistence/charts"}
];
function getQueryString() {
// This function is anonymous, is executed immediately and
// the return value is assigned to QueryString!
var query_string = {};
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split("=");
// If first entry with this name
if (typeof query_string[pair[0]] === "undefined") {
query_string[pair[0]] = pair[1];
// If second entry with this name
} else if (typeof query_string[pair[0]] === "string") {
var arr = [ query_string[pair[0]], pair[1] ];
query_string[pair[0]] = arr;
// If third or later entry with this name
} else {
query_string[pair[0]].push(pair[1]);
}
}
return query_string;
}
var periods = [
{name: 'h', period: 3600},
{name: '4h', period: 14400},
{name: '8h', period: 28800},
{name: '12h', period: 43200},
{name: 'd', period: 86400},
{name: '2d', period: 172800},
{name: '3d', period: 259200},
{name: '4d', period: 345600},
{name: 'w', period: 604800},
{name: '2w', period: 1209600},
{name: '3w', period: 1814400},
{name: 'm', period: 2592000},
{name: '2m', period: 5184000},
{name: '3m', period: 7776000},
{name: '4m', period: 10368000},
{name: '6m', period: 15552000},
{name: 'y', period: 31536000}
];
window.onload = function () {
var params = getQueryString();
graphPeriod = undefined;
graphName = params['chart'];
if (params['period'] != undefined) {
params['period'] = params['period'].toLowerCase();
for(var c = 0; c < periods.length; c++) {
if(periods[c].name == params['period']) {
graphPeriod = periods[c].period;
break;
}
}
if(graphPeriod == undefined)
graphPeriod = parseFloat(params['period']);
}
if (graphPeriod == NaN)
graphPeriod = undefined;
if (params['refresh'] != undefined)
graphUpdate = parseFloat(params['refresh']);
if (graphUpdate == NaN)
graphPeriod = 0;
persistenceService = params['service'];
initState = 0;
loadNextConfig();
}
function loadNextConfig() {
$.ajax({
url: initList[initState].url,
dataType: 'json',
success: function (json, state, xhr) {
if (state == "success") {
window[initList[initState].variable] = json;
initState++;
}
if (initState < initList.length) {
loadNextConfig();
return;
}
// All configs loaded
if (graphUpdate == 0)
doGraph();
else
mainLoop();
},
error: function () {
displayError("Error communicating with HABmin!");
}
});
}
function mainLoop() {
window.setTimeout(mainLoop, (graphUpdate * 60000))
doGraph();
}
function doGraph() {
// Default to local time
Highcharts.setOptions({global: {useUTC: false}});
if(configGraph == null || configGraph.chart == null) {
return;
}
var chartList = [].concat(configGraph.chart);
graphName = decodeURI(graphName.toLowerCase());
for(var c = 0; c < chartList.length; c++) {
if(chartList[c].name.toLowerCase() == graphName)
graphId = chartList[c].id;
}
if(graphId == null) {
displayError("Unable to find chart definition for \"" + graphName + "\"")
return;
}
jQuery.getJSON(
HABminBaseURL + '/persistence/charts/' + graphId,
"",
function (json, state, xhr) {
if (state == "success") {
var now = (new Date()).getTime();
updateChart(json, now - (graphPeriod * 1000), now);
}
});
}
/**
* Process incoming data responses.
* This is called when we receive a response to a graph data request.
* It correlates the data and waits until all requests are complete
* before drawing the graph
* @param item
* @param json
*/
function addGraphData(item, json) {
// Find this item in rawData
for (var chan = 0; chan < rawData.length; chan++) {
if (rawData[chan].item == item) {
// Mark that we've received this item
rawData[chan].received = true;
// If we have data, process it into the right format for highcharts
// This will probably run in parallel with the next channel being received
if (json != null) {
if (json.datapoints < 2) {
}
else {
// Convert the format. Hopefully the openHAB json can be changed to make this unnecessary
var newSeries = [];
var subPoints = parseInt(json.datapoints / 1000);
var subCount = 0;
var outCount = 0;
for (var i = 0; i < json.datapoints; i++) {
if (subCount++ < subPoints)
continue;
subCount = 0;
newSeries[outCount] = [];
newSeries[outCount][0] = parseInt(json.data[i].time);
newSeries[outCount][1] = parseFloat(json.data[i].state);
outCount++;
}
rawData[chan].data = newSeries;
}
}
}
}
// Check if all requests have completed
for (var chan = 0; chan < rawData.length; chan++) {
if (rawData[chan].received == false)
return;
}
// All requests have completed! Process and display the data!
drawChart();
}
/**
* Draws the chart. This is called after the data has been received.
*/
function drawChart() {
var options = chartOptions;
var errors = "";
options.series = [];
var cnt, tot;
cnt = 0;
tot = 0;
// Process all the data
var series = -1;
for (var chan = 0; chan < rawData.length; chan++) {
// Check if this channel has data
if (rawData[chan].data == null)
continue;
series++;
options.series[series] = {};
// Configure the series styles...
options.series[series].data = rawData[chan].data;
options.series[series].name = rawData[chan].label;
options.series[series].yAxis = rawData[chan].axis;
if (supportedCharts.indexOf(rawData[chan].chart) > -1)
options.series[series].type = rawData[chan].chart;
else
options.series[series].type = "spline";
if (rawData[chan].lineColor != null && rawData[chan].lineColor.length != 0)
options.series[series].color = rawData[chan].lineColor;
if (rawData[chan].lineWidth != null && parseInt(rawData[chan].lineWidth) != NaN)
options.series[series].lineWidth = rawData[chan].lineWidth;
if (typeof rawData[chan].legend == 'string' || rawData[chan].legend instanceof String)
options.series[series].showInLegend = (rawData[chan].legend.toLowerCase() == 'true') ? true : false;
if (rawData[chan].lineStyle != null && rawData[chan].lineStyle.length != 0)
options.series[series].dashStyle = rawData[chan].lineStyle;
if (supportedMarkers.indexOf(rawData[chan].markerSymbol) > -1) {
options.series[series].marker = {};
options.series[series].marker.enabled = true;
options.series[series].marker.symbol = rawData[chan].markerSymbol;
if (rawData[chan].markerColor != null && rawData[chan].markerColor.length != 0) {
options.series[series].marker.fillColor = rawData[chan].markerColor;
options.series[series].marker.lineColor = rawData[chan].markerColor;
}
}
}
$("#splashscreen").fadeOut(10);
$("#HABmin").fadeIn(10);
var h1 = $('#HABmin').innerHeight();
var h2 = $('#HABmin').outerHeight();
h1 = $(window).height();
var w1 = $(window).width();
options.chart.height = h1-2;
options.chart.width = w1-2;
this.chartObject = new Highcharts.Chart(options);
}
/**
* Update the graph.
* Requests data from the server and processes the responses.
* @param newConfig Config object for the chart
* @param start Start time
* @param stop Stop time
*/
function updateChart(newConfig, start, stop) {
// A bit of sanity checking before we start...
if (newConfig == null || newConfig.items == null || newConfig.items.length == 0)
return;
// Keep track of the current configuration
chartConfig = newConfig;
// Change to arrays
if (newConfig.axis != null)
newConfig.axis = [].concat(newConfig.axis);
if (newConfig.items != null)
newConfig.items = [].concat(newConfig.items);
if (isNaN(start))
start = 0;
if (isNaN(stop))
stop = 0;
if (start == 0 || stop == 0) {
var ts = Math.round((new Date()).getTime());
start = ts - (2 * 86400000);
stop = ts;
}
// Make sure we're not asking for data from the future!
if (stop > Math.round((new Date()).getTime()))
stop = Math.round((new Date()).getTime());
var parms = {};
parms.starttime = Math.floor(start);
parms.endtime = Math.ceil(stop);
// Remove the categories from the yAxis
chartOptions.yAxis = [];
for (var cnt = 0; cnt < 4; cnt++) {
chartOptions.yAxis[cnt] = {};
chartOptions.yAxis[cnt].title = {};
chartOptions.yAxis[cnt].title.text = "";
}
// Configure the axis
if (newConfig.axis != null) {
for (var cnt = 0; cnt < newConfig.axis.length; cnt++) {
var axis = newConfig.axis[cnt].axis - 1;
if(newConfig.axis[cnt].label != null && newConfig.axis[cnt].label.length != 0) {
chartOptions.yAxis[axis].title.text = newConfig.axis[cnt].label;
}
if(newConfig.axis[cnt].minimum != null) {
chartOptions.yAxis[axis].min = newConfig.axis[cnt].minimum;
chartOptions.yAxis[axis].startOnTick = false;
}
if(newConfig.axis[cnt].maximum != null) {
chartOptions.yAxis[axis].max = newConfig.axis[cnt].maximum;
chartOptions.yAxis[axis].endOnTick = false;
}
if(newConfig.axis[cnt].position == 'right') {
chartOptions.yAxis[axis].opposite = true;
}
if(newConfig.axis[cnt].format != null && newConfig.axis[cnt].format.length != 0) {
chartOptions.yAxis[axis].labels = {};
chartOptions.yAxis[axis].labels.format = newConfig.axis[cnt].format;
}
if(newConfig.axis[cnt].color != null && newConfig.axis[cnt].color.length != 0) {
if(chartOptions.yAxis[axis].labels == null)
chartOptions.yAxis[axis].labels = {};
chartOptions.yAxis[axis].labels.style = {};
chartOptions.yAxis[axis].labels.style.color = newConfig.axis[cnt].color;
if(chartOptions.yAxis[axis].title != null) {
chartOptions.yAxis[axis].title.style = {};
chartOptions.yAxis[axis].title.style.color = newConfig.axis[cnt].color;
}
}
}
}
// Clear the raw data
rawData = [];
// Loop through all items and request data via Ajax
for (var chan = 0; chan < chartConfig.items.length; chan++) {
rawData[chan] = {};
rawData[chan].received = false;
rawData[chan].item = chartConfig.items[chan].item;
rawData[chan].axis = chartConfig.items[chan].axis - 1;
rawData[chan].label = chartConfig.items[chan].label;
rawData[chan].chart = chartConfig.items[chan].chart;
rawData[chan].legend = chartConfig.items[chan].legend;
rawData[chan].lineWidth = chartConfig.items[chan].lineWidth;
rawData[chan].lineColor = chartConfig.items[chan].lineColor;
rawData[chan].lineStyle = chartConfig.items[chan].lineStyle;
rawData[chan].markerColor = chartConfig.items[chan].markerColor;
rawData[chan].markerSymbol = chartConfig.items[chan].markerSymbol;
jQuery.getJSON(
HABminBaseURL + '/persistence/services/' + persistenceService + '/' +
chartConfig.items[chan].item,
parms,
function (json, state, xhr) {
if (state == "success") {
addGraphData(json.name, json);
}
});
}
}
;
function displayError(error) {
$("#warningText").text(error);
$("#loadingSpinner").fadeOut(10);
$("#startWarning").fadeIn(10);
}
</script>
</head>
<body style="margin:0px;">
<div id="HABmin" style="width:100%;height:100%">
</div>
<div id="splashscreen" class="splashscreen">
<div style="display:block;position:absolute;width:99%;top:40%; text-align: center;">
<p id="loadingSpinner"><img src="images/loading.gif" alt="Loading..."/></p>
</div>
<div style="display:block;position:absolute;width:99%;top:45%; text-align: center;">
<p id="startWarning" style="display:none;font-family:Verdana,Helvetica,sans-serif;font-size: 10px;">
<span><img style="margin-top:-4px;" src="images/exclamation.png" alt="Error..."/></span><span
id="warningText" style="margin-left: 4px; vertical-align: top;"></span>
</p>
</div>
</div>
</body>
</html>