Skip to content

Commit 57a0e5c

Browse files
authored
Added stddev to timeline (#627)
1 parent c3f7663 commit 57a0e5c

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

frontend/js/helpers/charts.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
const calculateStatistics = (data) => {
2+
const mean = data.reduce((sum, value) => sum + value.value, 0) / data.length;
3+
const stddev = Math.sqrt(data.reduce((sum, value) => sum + Math.pow(value.value - mean, 2), 0) / data.length);
4+
return { mean, stddev };
5+
}
6+
17
const getCompareChartOptions = (legend, series, chart_type='line', x_axis='time', y_axis_name, mark_area=null, graphic=null) => {
28
let tooltip_trigger = (chart_type=='line') ? 'axis' : 'item';
39

@@ -106,7 +112,7 @@ const calculateMA = (series, factor) => {
106112
return result;
107113
}
108114

109-
const getLineBarChartOptions = (legend, labels, series, x_axis_name=null, y_axis_name='', x_axis='time', mark_area=null, no_toolbox=false, graphic=null, moving_average=false, show_x_axis_label=true) => {
115+
const getLineBarChartOptions = (legend, labels, series, x_axis_name=null, y_axis_name='', x_axis='time', mark_area=null, no_toolbox=false, graphic=null, moving_average=false, show_x_axis_label=true, stddev=false) => {
110116

111117
if(Object.keys(series).length == 0) {
112118
return {graphic: getChartGraphic("No energy reporter active")};
@@ -128,6 +134,31 @@ const getLineBarChartOptions = (legend, labels, series, x_axis_name=null, y_axis
128134
})
129135
}
130136

137+
if(stddev) {
138+
const { mean, stddev } = calculateStatistics(series[0].data);
139+
140+
legend.push('Stddev')
141+
series.push({
142+
name: 'Stddev',
143+
type: 'line',
144+
markArea: {
145+
label: {
146+
show: true,
147+
name: "MarkArea",
148+
position: 'top'
149+
},
150+
data: [
151+
[
152+
{ yAxis: mean + stddev, name: `StdDev: ${(stddev/mean * 100).toFixed(2)} %`},
153+
{ yAxis: mean - stddev},
154+
]
155+
156+
],
157+
}
158+
});
159+
}
160+
161+
131162
let options = {
132163
tooltip: { trigger: tooltip_trigger },
133164
grid: {

frontend/js/timeline.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,12 @@ const loadCharts = async () => {
218218
}
219219
}]
220220

221-
let options = getLineBarChartOptions([], series[my_series].labels, data_series, 'Time', series[my_series].unit, 'category', null, false, null, true, false);
221+
let options = getLineBarChartOptions([], series[my_series].labels, data_series, 'Time', series[my_series].unit, 'category', null, false, null, true, false, true);
222222

223223
options.tooltip = {
224224
trigger: 'item',
225225
formatter: function (params, ticket, callback) {
226-
if(params.componentType != 'series') return; // no notes for the MovingAverage
226+
if(series[params.seriesName]?.notes == null) return; // no notes for the MovingAverage
227227
return `<strong>${series[params.seriesName].notes[params.dataIndex].run_name}</strong><br>
228228
date: ${series[params.seriesName].notes[params.dataIndex].created_at}<br>
229229
metric_name: ${params.seriesName}<br>
@@ -243,9 +243,31 @@ const loadCharts = async () => {
243243

244244
});
245245

246+
options.dataZoom = {
247+
show: false,
248+
start: 0,
249+
end: 100,
250+
};
251+
246252

247253
chart_instance.setOption(options);
248254
chart_instances.push(chart_instance);
255+
chart_instance.on('datazoom', function(e, f) {
256+
const data = chart_instance.getOption().series[0].data
257+
const dataZoomOption = chart_instance.getOption().dataZoom[0];
258+
const startPercent = dataZoomOption.start;
259+
const endPercent = dataZoomOption.end;
260+
const totalDataPoints = data.length;
261+
const startIndex = Math.floor(startPercent / 100 * totalDataPoints);
262+
const endIndex = Math.ceil(endPercent / 100 * totalDataPoints) - 1;
263+
const { mean, stddev } = calculateStatistics(data.slice(startIndex, endIndex+1));
264+
265+
let options = chart_instance.getOption()
266+
options.series[2].markArea.data[0][0].name = `StdDev: ${(stddev/mean * 100).toFixed(2)} %`
267+
options.series[2].markArea.data[0][0].yAxis = mean + stddev
268+
options.series[2].markArea.data[0][1].yAxis = mean - stddev;
269+
chart_instance.setOption(options)
270+
});
249271

250272
}
251273

0 commit comments

Comments
 (0)