1
|
import {Graph} from "./graph";
|
2
|
|
3
|
declare var require: any
|
4
|
|
5
|
/**
|
6
|
* Graph with multiple sensors. (Only linechart)
|
7
|
*/
|
8
|
export class MultiGraph implements Graph{
|
9
|
|
10
|
|
11
|
isAnalytics: boolean;
|
12
|
data: any [];
|
13
|
interval: number;
|
14
|
legend: {};
|
15
|
|
16
|
/**
|
17
|
* Instantiates new multigraph
|
18
|
* @param isAnalytics true/false analytics/observations
|
19
|
* @param data source of values for vega graph
|
20
|
* @param legend source of legend for graph (e.g. sensors phenomenon)
|
21
|
* @param interval default graph interval used for different purposes (in milliseconds) (see timeWindow and maxTimeDifference in vega specification)
|
22
|
*/
|
23
|
constructor(isAnalytics: boolean, data: any [], legend: {}, interval: number) {
|
24
|
this.isAnalytics = isAnalytics;
|
25
|
this.data = data;
|
26
|
this.interval = interval;
|
27
|
this.legend = this.getLegend(legend);
|
28
|
|
29
|
}
|
30
|
|
31
|
/**
|
32
|
* Returns vega configuration (see readme.txt in vega folder)
|
33
|
*/
|
34
|
getConfig(): {} {
|
35
|
//from folder vega/config merge corresponding files
|
36
|
|
37
|
const lodash = require('lodash/object');
|
38
|
const rvalue: any = {};
|
39
|
let config1 = require('/src/vega/config/config.json');
|
40
|
lodash.merge(rvalue, config1);
|
41
|
|
42
|
let config2;
|
43
|
if (this.isAnalytics) {
|
44
|
config2 = require('/src/vega/config/config-multiline-analytics.json');
|
45
|
} else {
|
46
|
config2 = require('/src/vega/config/config-multiline-observations.json');
|
47
|
}
|
48
|
|
49
|
let config3 = require('/src/vega/config/config-barline.json');
|
50
|
|
51
|
|
52
|
for(let key in config2.signals) {
|
53
|
let obj = config2.signals[key];
|
54
|
rvalue.signals.push(obj);
|
55
|
}
|
56
|
|
57
|
for(let key in config3.signals) {
|
58
|
let obj = config3.signals[key];
|
59
|
rvalue.signals.push(obj);
|
60
|
}
|
61
|
|
62
|
return rvalue;
|
63
|
}
|
64
|
|
65
|
|
66
|
/**
|
67
|
* returns vega specification (see readme.txt in vega folder)
|
68
|
*/
|
69
|
getSpec(): {} {
|
70
|
const lodash = require('lodash/object');
|
71
|
const rvalue: any = {};
|
72
|
const base = require('/src/vega/base/default.json');
|
73
|
const body = require('/src/vega/body/multilinechart.json');
|
74
|
const legend = require('/src/vega/miscs/legend.json');
|
75
|
const tooltip = require('/src/vega/miscs/multiline-tooltip.json');
|
76
|
|
77
|
lodash.merge(rvalue, base, body, legend, tooltip);
|
78
|
|
79
|
//setting legend to result
|
80
|
rvalue.data[0].values = this.legend;
|
81
|
|
82
|
//setting data to result
|
83
|
rvalue.data[1].values = this.data;
|
84
|
|
85
|
//setting interval to json
|
86
|
//interval used for
|
87
|
// 1) appending graph domain
|
88
|
// 2) 5 * interval is max difference for connecting the dots
|
89
|
// 3) setting zoom in windcharts
|
90
|
rvalue.signals[0].value = this.interval;
|
91
|
|
92
|
//setting tooltip message
|
93
|
rvalue.marks[0].marks[0].marks[0].marks[2].encode.enter.tooltip.signal = this.getTooltipMessage();
|
94
|
|
95
|
return rvalue;
|
96
|
}
|
97
|
|
98
|
/**
|
99
|
* Generates the tooltip message
|
100
|
*/
|
101
|
private getTooltipMessage() {
|
102
|
let message = "{title: timeFormat(datum.dateTime, '%A, %B %e, %Y %X')";
|
103
|
this.data.forEach(function (e) {
|
104
|
message += (", '" + e.sensor.sensorName + "': datum['" + e.sensor.sensorName + "'] + ' " + e.sensor.phenomenon.unit +"' ");
|
105
|
})
|
106
|
|
107
|
message += "}";
|
108
|
return message;
|
109
|
}
|
110
|
|
111
|
/**
|
112
|
* If legend is array of phenomenons, it selects the first element
|
113
|
* @param legend info for displaying legend
|
114
|
*/
|
115
|
private getLegend(legend: any) {
|
116
|
if(Array.isArray(legend)) {
|
117
|
return legend[0];
|
118
|
} else {
|
119
|
return legend;
|
120
|
}
|
121
|
}
|
122
|
|
123
|
|
124
|
}
|