-
Notifications
You must be signed in to change notification settings - Fork 4
/
visu.js
146 lines (133 loc) · 5.25 KB
/
visu.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
// ----------------------------------------------------------
//
// SmartVISU widget for database plots with highcharts
// (c) Tobias Geier 2015
//
// Version: 0.3
// License: GNU General Public License v2.0
//
// Manual: https://github.com/ToGe3688/db_plot_widget
//
// ----------------------------------------------------------
$(document).delegate('div[data-widget="dbPlot.linePlot"]', {
// Get the data via POST Request from the php DB Handler
'init': function (event) {
// Set unit for y Axis
var unit = $(this).attr('data-unit-y-axis');
var containerId = $(this).attr('id');
// Check if y Axis Options are set. If not use default setting with values from widget data fields
var yAxisOptions = JSON.parse($(this).attr('data-y-axis-options'));
if (yAxisOptions !== '') {
// Set custom yAxisData
var yAxisData = yAxisOptions;
} else {
// Set default yAxisData
var yAxisData = {
title: {
text: $(this).attr('data-text-y-axis')
},
labels: {
format: '{value} ' + $(this).attr('data-unit-y-axis')
}
};
}
// Check if y Axis Options are set. If not use default setting with values from widget data fields
var legendOptions = JSON.parse($(this).attr('data-legend-options'));
if (legendOptions !== '') {
// Set custom legendOptions
var legendData = legendOptions;
} else {
// Set default legendOptions
var legendData = {
align: 'right',
verticalAlign: 'top',
borderWidth: 0
};
}
var options = {
// Options for chart
chart: {
renderTo: $(this).attr('id'),
type: $(this).attr('data-type'),
zoomType: 'x'
},
// Options for plot title
title: {
text: $(this).attr('data-title'),
align: 'left'
},
// Options for xAxis
xAxis: {
type: 'datetime'
},
// Options for yAxis
yAxis: yAxisData,
// Options for legend
legend: legendData,
series: [],
// Tooltip for SmartVISU Style
tooltip: {
formatter: function () {
if (this.series.options.unit !== null) {
// If unit is set in the series data use this
return this.series.name + ' <br/>' + this.y + ' ' + this.series.options.unit + '</b>';
} else {
// If unit is not set use the unit value from data fields
return this.series.name + ' <br/>' + this.y + ' ' + unit + '</b>';
}
}
}
};
// Get Query for POST-Request
var postData = {
query: $(this).attr('data-query'),
maxRows: $(this).attr('data-max-rows'),
timeRangeStart: Math.round((Date.now()/1000)-($(this).attr('data-range')*60)),
timeRangeEnd: Math.round(Date.now()/1000)
};
// Make POST-Request to get the data series for the plot
$.post("widgets/widget_dbplot.php", postData).done(function (data) {
if (!data.error) {
options.series = data;
new Highcharts.Chart(options);
} else {
console.log(data.error);
$('#' + containerId ).hide();
$('#' + containerId + '-error-container').show().html('<h3>dbPlot Error (Initial)</h3>' + data.error);
}
});
$(this).attr('data-last-update', Date.now());
},
// Update event is called when a GAD changes
'update': function (event, response) {
// The chart which will be updated
var chart = $('#' + this.id).highcharts();
var containerId = $(this).attr('id');
// Get Query for POST-Request
var postData = {
query: $(this).attr('data-query'),
maxRows: $(this).attr('data-max-rows'),
timeRangeStart: Math.round((Date.now()/1000)-($(this).attr('data-range')*60)),
timeRangeEnd: Math.round(Date.now()/1000)
};
// Make POST-Request and update the Plot
$.post("widgets/widget_dbplot.php", postData).done(function (data) {
if (!data.error) {
for (i = 0; i < data.length; i++) {
if (data[i].data.length !== 0) {
chart.series[i].setData(data[i].data, false);
$('#' + containerId ).attr('data-last-update', Date.now());
}
}
// Redraw chart
chart.redraw();
} else {
console.log(data.error);
$('#' + containerId ).hide();
$('#' + containerId + '-error-container').show().html('<h3>dbPlot Error (Update)</h3>' + data.error);
}
});
// Fix for display problems with too wide Chart container
$(window).resize();
}
});