1 |
8cca41c0
|
Štěpán Červenka
|
import {Graph} from "./graph";
|
2 |
66c042f1
|
Štěpán Červenka
|
import {SingleGraphType} from "./singlegraphtype";
|
3 |
8cca41c0
|
Štěpán Červenka
|
|
4 |
|
|
declare var require: any
|
5 |
|
|
|
6 |
66c042f1
|
Štěpán Červenka
|
export class SingleGraph implements Graph {
|
7 |
8cca41c0
|
Štěpán Červenka
|
isAnalytics: boolean;
|
8 |
|
|
sensorId: number;
|
9 |
|
|
interval: number;
|
10 |
|
|
data: any [];
|
11 |
66c042f1
|
Štěpán Červenka
|
legend: {};
|
12 |
|
|
type: SingleGraphType;
|
13 |
8cca41c0
|
Štěpán Červenka
|
|
14 |
66c042f1
|
Štěpán Červenka
|
constructor(sensorId: number, isAnalytics: boolean, data: any[], legend: {}, interval: number) {
|
15 |
8cca41c0
|
Štěpán Červenka
|
this.sensorId = sensorId;
|
16 |
|
|
this.isAnalytics = isAnalytics;
|
17 |
|
|
this.interval = interval;
|
18 |
|
|
this.data = data;
|
19 |
66c042f1
|
Štěpán Červenka
|
this.legend = this.getLegend(legend);
|
20 |
|
|
this.type = this.getGraphType();
|
21 |
|
|
|
22 |
8cca41c0
|
Štěpán Červenka
|
}
|
23 |
|
|
|
24 |
66c042f1
|
Štěpán Červenka
|
|
25 |
8cca41c0
|
Štěpán Červenka
|
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 |
66c042f1
|
Štěpán Červenka
|
|
31 |
8cca41c0
|
Štěpán Červenka
|
let config2;
|
32 |
66c042f1
|
Štěpán Červenka
|
if (this.isAnalytics) {
|
33 |
8cca41c0
|
Štěpán Červenka
|
config2 = require('/src/vega/config/config-analytics.json');
|
34 |
|
|
} else {
|
35 |
|
|
config2 = require('/src/vega/config/config-observations.json');
|
36 |
|
|
}
|
37 |
|
|
|
38 |
66c042f1
|
Štěpán Červenka
|
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 |
8cca41c0
|
Štěpán Červenka
|
let obj = config2.signals[key];
|
52 |
|
|
rvalue.signals.push(obj);
|
53 |
|
|
}
|
54 |
|
|
|
55 |
66c042f1
|
Štěpán Červenka
|
for (let key in config3.signals) {
|
56 |
|
|
let obj = config3.signals[key];
|
57 |
|
|
rvalue.signals.push(obj);
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
return rvalue;
|
61 |
8cca41c0
|
Štěpán Červenka
|
}
|
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 |
66c042f1
|
Štěpán Červenka
|
const legend = this.getLegendSpec();
|
69 |
8cca41c0
|
Štěpán Červenka
|
const tooltip = this.getTooltipSpec();
|
70 |
66c042f1
|
Štěpán Červenka
|
lodash.merge(rvalue, base, body, legend, tooltip);
|
71 |
8cca41c0
|
Štěpán Červenka
|
|
72 |
|
|
|
73 |
66c042f1
|
Štěpán Červenka
|
this.setData(rvalue, this.data);
|
74 |
|
|
rvalue['data'][0].values = this.legend;
|
75 |
8cca41c0
|
Štěpán Červenka
|
rvalue.signals[0].value = this.interval;
|
76 |
|
|
|
77 |
|
|
return rvalue;
|
78 |
|
|
}
|
79 |
|
|
|
80 |
|
|
|
81 |
66c042f1
|
Štěpán Červenka
|
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 |
8cca41c0
|
Štěpán Červenka
|
if (this.sensorId >= 480000000 && this.sensorId < 490000000) {
|
152 |
66c042f1
|
Štěpán Červenka
|
return SingleGraphType.BARCHART;
|
153 |
8cca41c0
|
Štěpán Červenka
|
} 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 |
66c042f1
|
Štěpán Červenka
|
return SingleGraphType.WINDCHART;
|
156 |
8cca41c0
|
Štěpán Červenka
|
} else {
|
157 |
|
|
//TODO pridat min/max
|
158 |
66c042f1
|
Štěpán Červenka
|
return SingleGraphType.LINECHART;
|
159 |
8cca41c0
|
Štěpán Červenka
|
}
|
160 |
|
|
}
|
161 |
|
|
|
162 |
|
|
|
163 |
|
|
}
|