-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-graph.js.txt
366 lines (322 loc) · 12.9 KB
/
create-graph.js.txt
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
var flag =0 ; /* flag notes if some other graphs are already present on the page */
var query ;
var refreshIntervalId;
function showGraph(filename,flr) {
document.getElementById("selected").innerHTML="You Selected : " + filename ;
if(flag){ /* clear the previously set update function so that old file doesn’t overlap with new.*/
clearInterval(refreshIntervalId);
}
flag = 1 ;
var pav=[]; /* Arrays to store received json data in the form required for canvasjs to plot*/
var pbv=[];
var pcv=[];
var pac = [];
var pbc=[];
var pcc=[];
var paap = [];
var pbap= [];
var pcap = [];
var avgV= [];
var avgC=[];
var totap = [];
var linefreq= [];
var cumKWH = [];
var power= [];
var totpf=[];
var present_time = '0' ;
query= { 'present_time': present_time , 'filename': filename } ; /* Parameters sent to php file*/
$.getJSON('update_data.php',query, function(result){
console.log(result);
for(var i in result){
/* data points should be in the form of {x:x_value , y:y_value} format for canvasjs to plot*/
pav.push({x:Number(result[i]['time']), y:Number(result[i]['pav']) });
pbv.push({x:Number(result[i]['time']), y:Number(result[i]['pbv']) });
pcv.push({x:Number(result[i]['time']), y:Number(result[i]['pcv']) });
pac.push({x:Number(result[i]['time']), y:Number(result[i]['pac']) });
pbc.push({x:Number(result[i]['time']), y:Number(result[i]['pbc']) });
pcc.push({x:Number(result[i]['time']), y:Number(result[i]['pcc']) });
paap.push({x:Number(result[i]['time']), y:Number(result[i]['paap']) });
pbap.push({x:Number(result[i]['time']), y:Number(result[i]['pbap']) });
pcap.push({x:Number(result[i]['time']), y:Number(result[i]['pcap']) });
avgV.push({x:Number(result[i]['time']), y:Number(result[i]['avgV']) });
avgC.push({x:Number(result[i]['time']), y:Number(result[i]['avgC']) });
totap.push({x:Number(result[i]['time']), y:Number(result[i]['totap']) });
linefreq.push({x:Number(result[i]['time']), y:Number(result[i]['linefreq']) });
cumKWH.push({x:Number(result[i]['time']), y:Number(result[i]['cumKWH']) });
power.push({x:Number(result[i]['time']), y:Number(result[i]['power']) });
totpf.push({x:Number(result[i]['time']), y:Number(result[i]['totpf']) });
/* Update present_time as time of last datapoint */
present_time = result[i]['time'] ;
// Sending mail whenever threshold crosses
if(result[i]['pcv']>244.4 ){
var form_data = {'quantity':'PhaseC voltage','value':result[i]['pcv'],'flr':flr,'tim':present_time,'lim':'244.4' };
$.getJSON("testmailphp.php",form_data);
}
if(result[i]['avgV']>243.8 ){
var form_data = {'quantity':'AvgVoltage','value':result[i]['avgV'],'flr':flr,'tim':present_time,'lim':'243.8' };
$.getJSON("testmailphp.php",form_data);
}
if(result[i]['totap']>36.79 ){
var form_data = {'quantity':'Total ActPower','value':result[i]['totap'],'flr':flr,'tim':present_time,'lim':'36.79' };
$.getJSON("testmailphp.php",form_data);
}
}
/*Actual graph plotting starts below
The first parameter specifies the id of that html block to which the graph should stick to */
var chart1 = new CanvasJS.Chart("id1",{
title :{
text: "Phase Voltages",
fontSize: 25
},
theme: "dark1",
toolTip:{
cornerRadius: 8,
},
axisX: {
title: "Epoch Time",
labelAngle: -85,
labelFontSize: 8.5
},
axisY: [{
includeZero: false,
suffix: " V" }],
data: [{
type: "line",
showInLegend: true,
name: "PhaseA Voltage",
markerSize: 5,
dataPoints : pav},
{
type: "line",
showInLegend: true,
name: "PhaseB Voltage",
markerSize: 5,
dataPoints : pbv},
{
type: "line",
showInLegend: true,
name: "PhaseC Voltage",
markerSize: 5,
dataPoints : pcv},
{
type: "line",
lineDashType: "dash",
showInLegend: true,
name: "Avg Voltage",
markerSize: 5,
dataPoints : avgV}]
});
var chart2 = new CanvasJS.Chart("id2",{
title :{
text: "Phase Currents",
fontSize: 25
},
theme: "dark1",
toolTip:{
cornerRadius: 8,
},
axisX: {
title: "Epoch Time",
labelAngle: -85,
labelFontSize: 8.5
},
axisY: [{
includeZero: false,
suffix: " Amp" }],
data: [{
type: "line",
showInLegend: true,
name: "PhaseA Current",
markerSize: 5,
dataPoints : pac},
{
type: "line",
showInLegend: true,
name: "PhaseB Current",
markerSize: 5,
dataPoints : pbc},
{
type: "line",
showInLegend: true,
name: "PhaseC Currentt",
markerSize: 5,
dataPoints : pcc},
{
type: "line",
lineDashType: "dash",
showInLegend: true,
name: "Avg Current",
markerSize: 5,
dataPoints : avgC}]
});
var chart3 = new CanvasJS.Chart("id3",{
title :{
text: "Phase Active Powers",
fontSize: 25
},
theme: "dark1",
toolTip:{
cornerRadius: 8,
},
axisX: {
title: "Epoch Time",
labelAngle: -85,
labelFontSize: 8.5
},
axisY:{
includeZero: false
},
data: [{
type: "line",
showInLegend: true,
name: "PhaseA Act.power",
markerSize: 5,
dataPoints : paap},
{
type: "line",
showInLegend: true,
name: "PhaseB Act.Power",
markerSize: 5,
dataPoints : pbap},
{
type: "line",
showInLegend: true,
name: "PhaseC Act.Power",
markerSize: 5,
dataPoints : pcap}]
});
var chart4 = new CanvasJS.Chart("id4",{
title :{
text: "Line Frequency",
fontSize: 25
},
theme: "dark1",
toolTip:{
cornerRadius: 8,
},
axisX: {
title: "Epoch Time",
labelAngle: -85,
labelFontSize: 8.5
},
axisY:{
includeZero: false
},
data: [{
type: "line",
dataPoints : linefreq }]
});
var chart5 = new CanvasJS.Chart("id5",{
theme: "dark1",
toolTip:{
cornerRadius: 8,
},
axisX: {
title: "Epoch Time",
labelAngle: -85,
labelFontSize: 8.5
},
axisY:{
includeZero: false
},
data: [{
type: "line",
showInLegend: true,
name: "Total Active Power",
markerSize: 5,
dataPoints : totap},
{
type: "line",
showInLegend: true,
name: "Power",
markerSize: 5,
dataPoints : power},
{
type: "line",
showInLegend: true,
name: "Total Power Factor",
markerSize: 5,
dataPoints : totpf}]
});
var chart6 = new CanvasJS.Chart("id6",{
title :{
text: "Cumulative KWH Energy",
fontSize: 25
},
theme: "dark1",
toolTip:{
cornerRadius: 8,
},
axisX: {
title: "Epoch Time",
labelAngle: -85,
labelFontSize: 8.5
},
axisY:{
includeZero: false
},
data: [{
type: "line",
dataPoints : cumKWH }]
});
chart1.render(); /* Renders the charts to plot graphs with the given parameters*/
chart2.render();
chart3.render();
chart4.render();
chart5.render();
chart6.render();
var updateInterval = 5000; /* Time period at which plots are updated in milliseconds */
function updateChart() {
/* This function gets updated data by sending request to database through php
present_time specifies the time entry till which previous graph was plotted
filename tells from which sql table you need data from */
query= { 'present_time': present_time , 'filename': filename } ;
$.getJSON('update_data.php', query ,function(result){
/* New,updated data is added to the array at the end and present_time is updated*/
for(var i in result){
pav.push({x:Number(result[i]['time']), y:Number(result[i]['pav']) });
pbv.push({x:Number(result[i]['time']), y:Number(result[i]['pbv']) });
pcv.push({x:Number(result[i]['time']), y:Number(result[i]['pcv']) });
pac.push({x:Number(result[i]['time']), y:Number(result[i]['pac']) });
pbc.push({x:Number(result[i]['time']), y:Number(result[i]['pbc']) });
pcc.push({x:Number(result[i]['time']), y:Number(result[i]['pcc']) });
paap.push({x:Number(result[i]['time']), y:Number(result[i]['paap']) });
pbap.push({x:Number(result[i]['time']), y:Number(result[i]['pbap']) });
pcap.push({x:Number(result[i]['time']), y:Number(result[i]['pcap']) });
avgV.push({x:Number(result[i]['time']), y:Number(result[i]['avgV']) });
avgC.push({x:Number(result[i]['time']), y:Number(result[i]['avgC']) });
totap.push({x:Number(result[i]['time']), y:Number(result[i]['totap']) });
linefreq.push({x:Number(result[i]['time']), y:Number(result[i]['linefreq']) });
cumKWH.push({x:Number(result[i]['time']), y:Number(result[i]['cumKWH']) });
power.push({x:Number(result[i]['time']), y:Number(result[i]['power']) });
totpf.push({x:Number(result[i]['time']), y:Number(result[i]['totpf']) });
present_time = result[i]['time'] ;
query= { 'present_time': present_time , 'filename': filename } ;
// Sending mail if threshold crosses
if(result[i]['pcv']>244.4 ){
var form_data = {'quantity':'PhaseC voltage','value':result[i]['pcv'],'flr':flr,'time':present_time,'lim':'244.4' };
$.getJSON("testmailphp.php",form_data);
}
if(result[i]['avgV']>243.8 ){
var form_data = {'quantity':'Average Voltage','value':result[i]['avgV'],'flr':flr,'time':present_time,'lim':'243.8' };
$.getJSON("testmailphp.php",form_data);
}
if(result[i]['totap']>36.79 ){
var form_data = {'quantity':'Total ActivePower','value':result[i]['totap'],'flr':flr,'time':present_time,'lim':'36.79' };
$.getJSON("testmailphp.php",form_data);
}
/* Arrays are shifted one value to the right each time since they become longer and longer as time progresses and graph loading might become slow and difficult to understand shrinked plots */
pav.shift();pbv.shift();pcv.shift();pac.shift();pbc.shift();pcc.shift();paap.shift();pbap.shift();pcap.shift();avgV.shift();avgC.shift();totap.shift();linefreq.shift();cumKWH.shift();power.shift();totpf.shift();
}
chart1.render(); /* All the charts are rendered again to plot the updated data*/
chart2.render();
chart3.render();
chart4.render();
chart5.render();
chart6.render();
});
}
/* Setting the updateChart() function to repeat at regular intervals*/
refreshIntervalId =setInterval(function(){updateChart()}, updateInterval);
});
}