-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdweetProChartNV.js
347 lines (308 loc) · 10.5 KB
/
dweetProChartNV.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
// #vimocChart.js Freeboard Widget
//
// ## A Freeboard plugin that uses D3.js and NVD3 to visualise historical data coming from a Vimoc Datasource.
//
//
(function()
{
var push_apply = Function.apply.bind([].push);
var slice_call = Function.call.bind([].slice);
Object.defineProperty(Array.prototype, "pushArrayMembers", {
value: function() {
for (var i = 0; i < arguments.length; i++) {
var to_add = arguments[i];
for (var n = 0; n < to_add.length; n+=300) {
push_apply(this, slice_call(to_add, n, n+300));
}
}
}
});
// freeboard.addStyle(".tooltip","position: absolute; text-align: left; width:auto; height: auto; padding: 8px; margin-top: -20px; font: 10px sans-serif; background: #d3d4d4; pointer-events: none; z-index:3000; ");
// freeboard.addStyle(".axis path","fill: none; stroke: #FFF;shape-rendering: crispEdges;");
// freeboard.addStyle(".axis line","fill: none; stroke: #FFF;shape-rendering: crispEdges;");
// freeboard.addStyle(".chart","color:#fff;");
// freeboard.addStyle(".axis text","font-family: sans-serif; font-size:10px; font-weight:lighter; fill:none; stroke:#d3d4d4;shape-rendering: crispEdges;");
// freeboard.addStyle(".overlay","fill: none; pointer-events: all;");
// freeboard.addStyle(".focus circle","fill:none; stroke:white");
freeboard.addStyle(".tick line","display: none;");
freeboard.addStyle(".nv-axis","fill: white;");
freeboard.addStyle(".nvd3 .nv-axis path.domain","stroke: #ffffff;");
freeboard.addStyle(".nvd3 .nv-y .nv-axis .tick.zero line","stroke: #fff;");
freeboard.addStyle(".nvd3 text","fill:white;");
freeboard.addStyle(".highlight td","background-color: rgba(136, 110, 70, 0.3)");
freeboard.loadWidgetPlugin({
"type_name": "dPChartWidget",
"display_name": "Dweet Historical Chart",
"description": "Creates a multi-series line chart from DweetPro Datasource data.",
"external_scripts": [
"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js",
"https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.6/nv.d3.min.js",
"https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.6/nv.d3.min.css"
],
"fill_size": false,
"settings": [
{
"name": "type",
"display_name": "Chart Type",
"type": "option",
"options": [
"Line"
],
"default_value": "Line"
},
{
"name": "data",
"display_name": "JSON Data Array",
"type": "calculated"
},
{
"name": "target",
"display_name": "Target Data Field",
"type": "calculated"
},
{
"name": "timeframe",
"display_name":"Time Frame",
"type":"option",
"options":[
"All",
"Hour",
"Day",
"Week",
"Month"
],
"default_value":"All"
},
{
"name":"ymin",
"display_name":"Y-Min",
"type":"text"
},
{
"name":"ymax",
"display_name":"Y-Max",
"type":"text"
},
{
"name": "size",
"display_name": "Size",
"description": "Small: 300x180, Medium: 600x420, Large: 900x600.<br><strong>NOTE: You must also set the containing Pane's COLUMNS value to 1 (Small), 2 (Medium) or 3 (Large)</strong>",
"type": "option",
"options": [
"Small",
"Medium",
"Large",
"X-Wide"
],
"default_value": "Small"
},
{
"name": "include_axis",
"display_name": "Include Axis?",
"type": "boolean",
"default_value": false
}
],
newInstance : function(settings, newInstanceCallback)
{
newInstanceCallback(new dPChartWidget(settings));
}
});
var dPChartWidget = function(settings)
{
var self = this;
var currentSettings = settings;
var container;
var chartElement;
var styleSheet = null;
this.render = function(containerElement)
{
// Here we append our text element to the widget container element.
container = containerElement;
// Load the settings:
this.onSettingsChanged(currentSettings);
}
this.getHeight = function()
{
switch (currentSettings.size) {
case 'Small':
return 3;
case 'Medium':
return 7;
case 'Large':
return 10;
case 'X-Wide':
return 7;
}
return 5;
}
this.onSettingsChanged = function(newSettings)
{
currentSettings = newSettings;
$(container).empty();
chartElement = d3.select(container).append('svg').attr("class","chart").attr("style","height:"+currentSettings.height+"px").attr("height",currentSettings.height);
switch (currentSettings.size) {
case 'Small':
currentSettings.width = 300;
currentSettings.height = 180;
break;
case 'Medium':
currentSettings.width = 600;
currentSettings.height = 420;
break;
case 'Large':
currentSettings.width = 900;
currentSettings.height = 600;
break;
case 'X-Wide':
currentSettings.width = 1200;
currentSettings.height = 420;
break;
default:
currentSettings.width = 300;
currentSettings.height = 180;
}
}
this.onCalculatedValueChanged = function(settingName, newValue)
{
if(settingName == "data")
{
// Set the data:
var data = processDweetData(newValue.reverse());
// Placeholder for future support of different chart types
switch (currentSettings.type) {
// case "Bar":
// createBarChart(data, targetArray, currentSettings.include_axis);
// break;
case "Line":
createLineChart(data, currentSettings.include_axis, currentSettings.ymin, currentSettings.ymax);
break;
default:
createLineChart(data, currentSettings.include_axis);
}
}
}
function processDweetData(rawdata) {
var newData = [];
var format = {};
rawdata.forEach(function(datapoint) {
for (var k in datapoint["content"]) {
if (_.isUndefined(format[k])) {
format[k] = [];
}
format[k].push({'x':datapoint["created"],'y':datapoint["content"][k]});
}
});
for (var key in format) {
var obj = {};
obj["key"] = key;
obj["values"] = format[key];
newData.push(obj);
}
console.log(newData);
return newData;
}
function processDataNVD3(vimocdata) {
var newData = [];
vimocdata["sensors"].forEach(function(sensor) {
var inobj = {"values":[]};
var outobj = {"values":[]};
inobj["key"] = sensor["info"]["Name"] + "-In";
inobj["sensor"] = sensor["guid"].substring(9) + "-in";
for (var a=0; a<sensor["data"]["history"].length; a++) {
inobj["values"].push({"x":sensor["data"]["history"][a]["time"],"y":sensor["data"]["history"][a]["in"]});
}
inobj["orient"] = sensor["info"]["in"];
inobj["dir"] = "in";
newData.push(inobj);
outobj["key"] = sensor["info"]["Name"] + "-Out";
outobj["sensor"] = sensor["guid"].substring(9) + "-out";
for (var a=0; a<sensor["data"]["history"].length; a++) {
outobj["values"].push({"x":sensor["data"]["history"][a]["time"],"y":sensor["data"]["history"][a]["out"]});
}
outobj["orient"] = sensor["info"]["out"];
outobj["dir"] = "out";
newData.push(outobj);
});
return newData;
}
function findValueByPrefix(object, prefix) {
for (var property in object) {
if (object.hasOwnProperty(property) && property.toString().startsWith(prefix)) {
return object[property];
}
}
}
function findValueBySuffix(object, suffix) {
for (var property in object) {
if (object.hasOwnProperty(property) && property.toString().endsWith(suffix)) {
return object[property];
}
}
}
function getKey(object,value) {
for(var key in object){
if(object[key]==value) return key;
}
}
function getInOut(val) {
if (val.endsWith("_in")) {
return "In";
} else {
return "Out";
}
}
function createLineChart(data,axis,ymin,ymax) {
window.data = data;
nv.addGraph(function() {
window.chart = nv.models.lineChart()
.x(function (d) { return new Date(d.x) })
.y(function (d) { return +d.y })
.xScale(d3.time.scale())
.showLegend(true)
.showXAxis(true)
.useInteractiveGuideline(true);
var x = d3.time.scale()
.domain(d3.extent(window.data[0], function(d){return new Date(d.x)}))
.range([0,currentSettings.width]);
var format = d3.time.format.multi([
[".%L", function(d) { return d.getMilliseconds(); }],
[":%S", function(d) { return d.getSeconds(); }],
["%I:%M", function(d) { return d.getMinutes(); }],
["%I %p", function(d) { return d.getHours(); }],
["%a %d", function(d) { return d.getDay() && d.getDate() != 1; }],
["%b %d", function(d) { return d.getDate() != 1; }],
["%B", function(d) { return d.getMonth(); }],
["%Y", function() { return true; }]
]);
window.chart.xAxis
.axisLabel('Time')
.showMaxMin(false)
.tickSize(10)
.tickFormat(function (d) { return format(new Date(d)); })
.ticks(10);
window.chart.yAxis
.axisLabel('Value');
window.chart.legend
.maxKeyLength(33);
chartElement
.datum(window.data)
.transition().duration(750)
.call(window.chart);
nv.utils.windowResize(window.chart.update);
// set up the tooltip to display full dates
var tsFormat = d3.time.format('%b %-d, %Y %I:%M%p');
var contentGenerator = window.chart.interactiveLayer.tooltip.contentGenerator();
var tooltip = window.chart.interactiveLayer.tooltip;
tooltip.contentGenerator(function (d) { d.value = d.series[0].data.x; return contentGenerator(d); });
tooltip.headerFormatter(function (d) { return tsFormat(new Date(d)); });
return window.chart;
});
}
this.onDispose = function()
{
window.data = null;
window.chart = null;
}
}
}());