1
|
import {Graph} from "./graph";
|
2
|
import {SingleGraphType} from "./singlegraphtype";
|
3
|
|
4
|
declare var require: any
|
5
|
|
6
|
export class SingleGraph implements Graph {
|
7
|
isAnalytics: boolean;
|
8
|
sensorId: number;
|
9
|
interval: number;
|
10
|
data: any [];
|
11
|
legend: {};
|
12
|
type: SingleGraphType;
|
13
|
|
14
|
constructor(sensorId: number, isAnalytics: boolean, data: any[], legend: {}, interval: number) {
|
15
|
this.sensorId = sensorId;
|
16
|
this.isAnalytics = isAnalytics;
|
17
|
this.interval = interval;
|
18
|
this.data = data;
|
19
|
this.legend = this.getLegend(legend);
|
20
|
this.type = this.getGraphType();
|
21
|
|
22
|
}
|
23
|
|
24
|
|
25
|
getConfig(): {} {
|
26
|
const lodash = require('lodash/object');
|
27
|
const rvalue: any = {};
|
28
|
let config1 = require('/src/vega/config/config.json');
|
29
|
lodash.merge(rvalue, config1);
|
30
|
|
31
|
let config2;
|
32
|
if (this.isAnalytics) {
|
33
|
config2 = require('/src/vega/config/config-analytics.json');
|
34
|
} else {
|
35
|
config2 = require('/src/vega/config/config-observations.json');
|
36
|
}
|
37
|
|
38
|
let config3;
|
39
|
switch (this.type) {
|
40
|
case SingleGraphType.LINECHART:
|
41
|
case SingleGraphType.BARCHART:
|
42
|
config3 = require('/src/vega/config/config-barline.json');
|
43
|
break;
|
44
|
case SingleGraphType.WINDCHART:
|
45
|
config3 = require('/src/vega/config/config-wind.json');
|
46
|
break;
|
47
|
}
|
48
|
|
49
|
|
50
|
for (let key in config2.signals) {
|
51
|
let obj = config2.signals[key];
|
52
|
rvalue.signals.push(obj);
|
53
|
}
|
54
|
|
55
|
for (let key in config3.signals) {
|
56
|
let obj = config3.signals[key];
|
57
|
rvalue.signals.push(obj);
|
58
|
}
|
59
|
|
60
|
return rvalue;
|
61
|
}
|
62
|
|
63
|
getSpec(): {} {
|
64
|
const lodash = require('lodash/object');
|
65
|
const rvalue: any = {};
|
66
|
const base = require('/src/vega/base/default.json');
|
67
|
const body = this.getBodySpec();
|
68
|
const legend = this.getLegendSpec();
|
69
|
const tooltip = this.getTooltipSpec();
|
70
|
lodash.merge(rvalue, base, body, legend, tooltip);
|
71
|
|
72
|
|
73
|
this.setData(rvalue, this.data);
|
74
|
rvalue['data'][0].values = this.legend;
|
75
|
rvalue.signals[0].value = this.interval;
|
76
|
|
77
|
return rvalue;
|
78
|
}
|
79
|
|
80
|
|
81
|
private getLegendSpec() {
|
82
|
switch (this.type) {
|
83
|
case SingleGraphType.BARCHART:
|
84
|
case SingleGraphType.LINECHART:
|
85
|
return require('/src/vega/miscs/legend.json');
|
86
|
case SingleGraphType.WINDCHART:
|
87
|
return {};
|
88
|
}
|
89
|
}
|
90
|
|
91
|
private getBodySpec() {
|
92
|
switch (this.type) {
|
93
|
case SingleGraphType.BARCHART:
|
94
|
return require('/src/vega/body/barchart.json');
|
95
|
case SingleGraphType.WINDCHART:
|
96
|
return require('/src/vega/body/windchart.json');
|
97
|
case SingleGraphType.LINECHART:
|
98
|
//TODO pridat min/max
|
99
|
return require('/src/vega/body/linechart.json');
|
100
|
}
|
101
|
}
|
102
|
|
103
|
|
104
|
private getTooltipSpec() {
|
105
|
let lodash = require('lodash');
|
106
|
let tooltipMessage;
|
107
|
let rvalue: any = {};
|
108
|
if (this.isAnalytics) {
|
109
|
lodash.merge(rvalue, require('/src/vega/miscs/samm-tooltip.json'));
|
110
|
tooltipMessage = this.getSammTooltip();
|
111
|
} else {
|
112
|
lodash.merge(rvalue, require('/src/vega/miscs/value-tooltip.json'));
|
113
|
tooltipMessage = this.getValueTooltip();
|
114
|
}
|
115
|
|
116
|
rvalue.marks[0].marks[0].marks[0].encode.enter.tooltip.signal = tooltipMessage;
|
117
|
return rvalue;
|
118
|
}
|
119
|
|
120
|
|
121
|
private getValueTooltip() {
|
122
|
return "{title: timeFormat(datum.dateTime, '%A, %e. %B %Y, %X') , 'value': datum.value + ' " + this.legend['phenomenon']['unit'] + "', 'gid': datum.gid }"
|
123
|
}
|
124
|
|
125
|
private getSammTooltip() {
|
126
|
return "{title: timeFormat(datum.dateTime, '%A, %e. %B %Y, %X') " +
|
127
|
" , 'avg': datum.avg + ' " + this.legend['phenomenon']['unit'] +
|
128
|
"', 'sum': datum.sum + ' " + this.legend['phenomenon']['unit'] +
|
129
|
"', 'max': datum.max + ' " + this.legend['phenomenon']['unit'] +
|
130
|
"', 'min': datum.min + ' " + this.legend['phenomenon']['unit'] + "'}"
|
131
|
}
|
132
|
|
133
|
|
134
|
private setData(spec: any, data: any) {
|
135
|
if(data.length > 0 && 'data' in data[0]) {
|
136
|
spec['data'][1].values = data[0].data;
|
137
|
} else {
|
138
|
spec['data'][1].values = data;
|
139
|
}
|
140
|
}
|
141
|
|
142
|
private getLegend(legend: any) {
|
143
|
if (Array.isArray(legend)) {
|
144
|
return legend[0];
|
145
|
} else {
|
146
|
return legend;
|
147
|
}
|
148
|
}
|
149
|
|
150
|
private getGraphType() {
|
151
|
if (this.sensorId >= 480000000 && this.sensorId < 490000000) {
|
152
|
return SingleGraphType.BARCHART;
|
153
|
} else if ((this.sensorId >= 470020000 && this.sensorId < 470030000) || (this.sensorId >= 470060000 && this.sensorId < 470090000) ||
|
154
|
(this.sensorId >= 470130000 && this.sensorId < 470140000) || (this.sensorId >= 470180000 && this.sensorId < 470190000)) {
|
155
|
return SingleGraphType.WINDCHART;
|
156
|
} else {
|
157
|
//TODO pridat min/max
|
158
|
return SingleGraphType.LINECHART;
|
159
|
}
|
160
|
}
|
161
|
|
162
|
|
163
|
}
|