-
Notifications
You must be signed in to change notification settings - Fork 1
/
wdataview.dev.js
142 lines (121 loc) · 3.72 KB
/
wdataview.dev.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
/*!
* WViewPlot 1.0
*
* Copyright 2014-2015 Henrik Enquist
* Released under the GPLv3 license
*/
function plotJson(series,placeholder,clickplaceholder) {
//basic plot options
if (series.message === 'ok') {
var options = {
lines: {
show: true
},
//points: {
// show: false
//},
xaxis: {
mode: "time",
timezone: "browser"
},
crosshair: {
mode: "xy"
},
grid: {
hoverable: true,
clickable: true,
autoHighlight: false
},
yaxes: []
};
var data=[];
var unitarray = [];
for ( var sensor in series.units ){
if (sensor != 'dateTime') {
unitarray.push( series.units[ sensor ] );
}
}
var plotunits = $.unique(unitarray);
var nbr_axes = plotunits.length;
for (var axis = 1; axis <= nbr_axes; axis++) {
if (axis == 2) {
temppos = "right";
} else {
temppos = "left";
}
options.yaxes.push( { position: temppos, axisLabel: plotunits[axis-1], alignTicksWithAxis: axis > 1 ? 1 : null });
}
for (var sens = 1; sens < series.sensors.length; sens++) {
seriesdata=[];
var serieslabel = series.sensors[sens];
var seriesunit = series.units[serieslabel];
var seriesaxis = plotunits.indexOf(seriesunit)+1;
//loop through all values
for (var tpoint = 0; tpoint < series.data.dateTime.length; tpoint++) {
seriesdata.push([series.data.dateTime[tpoint]*1000, series.data[series.sensors[sens]][tpoint]]);
}
// build dataset for Flot
var tempdata = {data: seriesdata, yaxis: seriesaxis, label: serieslabel + ', ' + seriesunit };
//special treatment of rain (bar plot)
if (serieslabel === "rain") {
//x-axis step needed to set bar width (only used for rain)
var timestep = (series.data.dateTime[1]-series.data.dateTime[0])*1000;
tempdata["bars"] = { show: true , barWidth: timestep};
tempdata["lines"] = { show: false };
}
//push new series into plot dataset
data.push(tempdata);
}
//plot it!
$.plot(placeholder, data, options);
//bind plotclick
$(placeholder).bind("plotclick", function (event, pos, item) {
if (item) {
var x = new Date(item.datapoint[0]),
y = item.datapoint[1].toFixed(2);
$(clickplaceholder).text( x.toString() + " - " + item.series.label + ": " + y );
//plot.highlight(item.series, item.datapoint);
}
});
} else {
$(clickplaceholder).text(series.message);
}
}
function updateGraph() //run to update plot
{
var datesRel, datesAbs, values, index, dAbs;
// Get the parameters as an array
values = $("#formParams").serializeArray();
datesRel = $("#formDatesRel").serializeArray();
datesAbs = $("#formDatesAbs").serializeArray();
var activeTab = $("ul#dateTabs li.active").attr('id');
if (activeTab == "absolute") {
values.push({name: "dAbs", value: 1});
} else {
values.push({name: "dAbs", value: 0});
}
//plot button pressed, get form
var str1 = jQuery.param(values);
var str2 = jQuery.param(datesRel);
var str3 = jQuery.param(datesAbs);
//var str = $("form").serialize();
var dataurl = './getjson.dev.php?' + str1 + '&' + str2 + '&' + str3;
//request data
$.ajax({
url: dataurl,
type: "GET",
dataType: "json",
success: function(json) {
plotJson(json, "#placeholder","#clickplaceholder");
}
});
}
// ----------------------------- main program ------------------------------------
//$("#placeholder").bind("plotclick", function (event, pos, item) {
// if (item) {
// var x = new Date(item.datapoint[0]),
// y = item.datapoint[1].toFixed(2);
// $("#clickdata").text( x.toString() + " - " + item.series.label + ": " + y );
// //plot.highlight(item.series, item.datapoint);
// }
//});