1
|
import {Graph} from "./graph";
|
2
|
import {SingleGraphType} from "./singlegraphtype";
|
3
|
|
4
|
declare var require: any
|
5
|
|
6
|
/**
|
7
|
* Graph with single sensors (linechart, barchart, windchart)
|
8
|
*/
|
9
|
export class SingleGraph implements Graph {
|
10
|
isAnalytics: boolean;
|
11
|
sensorId: number;
|
12
|
interval: number;
|
13
|
data: any [];
|
14
|
legend: {};
|
15
|
type: SingleGraphType;
|
16
|
|
17
|
/**
|
18
|
*
|
19
|
* @param sensorId id of sensor
|
20
|
* @param isAnalytics true for analytics, false for observations
|
21
|
* @param data source of values for vega graph
|
22
|
* @param legend source of legend info for graph (e.g. sensor phenomenon)
|
23
|
* @param interval default graph interval used for different purposes (in milliseconds) (see timeWindow and maxTimeDifference in vega specification)
|
24
|
*/
|
25
|
constructor(sensorId: number, isAnalytics: boolean, data: any[], legend: {}, interval: number) {
|
26
|
this.sensorId = sensorId;
|
27
|
this.isAnalytics = isAnalytics;
|
28
|
this.interval = interval;
|
29
|
this.data = data;
|
30
|
this.legend = this.getLegend(legend);
|
31
|
this.type = this.getGraphType();
|
32
|
|
33
|
}
|
34
|
|
35
|
/**
|
36
|
* Returns vega configuration (see readme.txt in vega folder)
|
37
|
*/
|
38
|
getConfig(): {} {
|
39
|
const lodash = require('lodash/object');
|
40
|
const rvalue: any = {};
|
41
|
let config1 = require('/src/vega/config/config.json');
|
42
|
lodash.merge(rvalue, config1);
|
43
|
|
44
|
let config2;
|
45
|
if (this.isAnalytics) {
|
46
|
config2 = require('/src/vega/config/config-analytics.json');
|
47
|
} else {
|
48
|
config2 = require('/src/vega/config/config-observations.json');
|
49
|
}
|
50
|
|
51
|
let config3;
|
52
|
switch (this.type) {
|
53
|
case SingleGraphType.LINECHART:
|
54
|
case SingleGraphType.BARCHART:
|
55
|
config3 = require('/src/vega/config/config-barline.json');
|
56
|
break;
|
57
|
case SingleGraphType.WINDCHART:
|
58
|
config3 = require('/src/vega/config/config-wind.json');
|
59
|
break;
|
60
|
}
|
61
|
|
62
|
|
63
|
for (let key in config2.signals) {
|
64
|
let obj = config2.signals[key];
|
65
|
rvalue.signals.push(obj);
|
66
|
}
|
67
|
|
68
|
for (let key in config3.signals) {
|
69
|
let obj = config3.signals[key];
|
70
|
rvalue.signals.push(obj);
|
71
|
}
|
72
|
|
73
|
return rvalue;
|
74
|
}
|
75
|
|
76
|
/**
|
77
|
* Gets vega specification (see readme.txt in vega folder)
|
78
|
*/
|
79
|
getSpec(): {} {
|
80
|
const lodash = require('lodash/object');
|
81
|
const rvalue: any = {};
|
82
|
const base = require('/src/vega/base/default.json');
|
83
|
const body = this.getBodySpec();
|
84
|
const legend = this.getLegendSpec();
|
85
|
const tooltip = this.getTooltipSpec();
|
86
|
lodash.merge(rvalue, base, body, legend, tooltip);
|
87
|
|
88
|
//setting data to result
|
89
|
this.setData(rvalue, this.data);
|
90
|
|
91
|
//setting legend to result
|
92
|
rvalue['data'][0].values = this.legend;
|
93
|
|
94
|
//setting interval to result
|
95
|
rvalue.signals[0].value = this.interval;
|
96
|
|
97
|
return rvalue;
|
98
|
}
|
99
|
|
100
|
/**
|
101
|
* Gets legend specification (windchart does not have a legend)
|
102
|
*/
|
103
|
private getLegendSpec() {
|
104
|
switch (this.type) {
|
105
|
case SingleGraphType.BARCHART:
|
106
|
case SingleGraphType.LINECHART:
|
107
|
return require('/src/vega/miscs/legend.json');
|
108
|
case SingleGraphType.WINDCHART:
|
109
|
return {};
|
110
|
}
|
111
|
}
|
112
|
|
113
|
/**
|
114
|
* Gets body specification
|
115
|
*/
|
116
|
private getBodySpec() {
|
117
|
switch (this.type) {
|
118
|
case SingleGraphType.BARCHART:
|
119
|
return require('/src/vega/body/barchart.json');
|
120
|
case SingleGraphType.WINDCHART:
|
121
|
return require('/src/vega/body/windchart.json');
|
122
|
case SingleGraphType.LINECHART:
|
123
|
return require('/src/vega/body/linechart.json');
|
124
|
}
|
125
|
}
|
126
|
|
127
|
|
128
|
/**
|
129
|
* Get tooltip specification
|
130
|
*/
|
131
|
private getTooltipSpec() {
|
132
|
let lodash = require('lodash');
|
133
|
let tooltipMessage;
|
134
|
let rvalue: any = {};
|
135
|
if (this.isAnalytics) {
|
136
|
lodash.merge(rvalue, require('/src/vega/miscs/samm-tooltip.json'));
|
137
|
tooltipMessage = this.getSammTooltip();
|
138
|
} else {
|
139
|
lodash.merge(rvalue, require('/src/vega/miscs/value-tooltip.json'));
|
140
|
tooltipMessage = this.getValueTooltip();
|
141
|
}
|
142
|
|
143
|
//sets the tooltip message
|
144
|
rvalue.marks[0].marks[0].marks[0].encode.enter.tooltip.signal = tooltipMessage;
|
145
|
return rvalue;
|
146
|
}
|
147
|
|
148
|
/**
|
149
|
*
|
150
|
*/
|
151
|
private getValueTooltip() {
|
152
|
return "{title: timeFormat(datum.dateTime, '%A, %B %e, %Y %X') , 'value': datum.value + ' " + this.legend['phenomenon']['unit'] + "'}"
|
153
|
}
|
154
|
|
155
|
private getSammTooltip() {
|
156
|
return "{title: timeFormat(datum.dateTime, '%A, %B %e, %Y %X') " +
|
157
|
" , 'avg': datum.avg + ' " + this.legend['phenomenon']['unit'] +
|
158
|
"', 'sum': datum.sum + ' " + this.legend['phenomenon']['unit'] +
|
159
|
"', 'max': datum.max + ' " + this.legend['phenomenon']['unit'] +
|
160
|
"', 'min': datum.min + ' " + this.legend['phenomenon']['unit'] + "'}"
|
161
|
}
|
162
|
|
163
|
|
164
|
/**
|
165
|
* Sets data to specification. If data is array, it selects only the first element
|
166
|
* @param spec specification to insert into
|
167
|
* @param data data to insert
|
168
|
*/
|
169
|
private setData(spec: any, data: any) {
|
170
|
if(data.length > 0 && 'data' in data[0]) {
|
171
|
spec['data'][1].values = data[0].data;
|
172
|
} else {
|
173
|
spec['data'][1].values = data;
|
174
|
}
|
175
|
}
|
176
|
|
177
|
/**
|
178
|
* If legend is array of phenomenons, it selects the first element
|
179
|
* @param legend info for displaying legend
|
180
|
*/
|
181
|
private getLegend(legend: any) {
|
182
|
if (Array.isArray(legend)) {
|
183
|
return legend[0];
|
184
|
} else {
|
185
|
return legend;
|
186
|
}
|
187
|
}
|
188
|
|
189
|
/**
|
190
|
* Select the graph type from id
|
191
|
*/
|
192
|
private getGraphType() {
|
193
|
if (this.sensorId >= 480000000 && this.sensorId < 490000000) {
|
194
|
return SingleGraphType.BARCHART;
|
195
|
} else if ((this.sensorId >= 470020000 && this.sensorId < 470030000) || (this.sensorId >= 470060000 && this.sensorId < 470090000) ||
|
196
|
(this.sensorId >= 470130000 && this.sensorId < 470140000) || (this.sensorId >= 470180000 && this.sensorId < 470190000)) {
|
197
|
return SingleGraphType.WINDCHART;
|
198
|
} else {
|
199
|
return SingleGraphType.LINECHART;
|
200
|
}
|
201
|
}
|
202
|
|
203
|
|
204
|
}
|