UNPKG

3.06 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright 2013 David Eberlein (david.eberlein@ch.sauter-bc.com)
4 * MIT-licenced: https://opensource.org/licenses/MIT
5 */
6
7/**
8 * @fileoverview DataHandler implementation for the error bars option.
9 * @author David Eberlein (david.eberlein@ch.sauter-bc.com)
10 */
11
12/*global Dygraph:false */
13"use strict";
14
15import BarsHandler from './bars';
16
17/**
18 * @constructor
19 * @extends BarsHandler
20 */
21var ErrorBarsHandler = function() {
22};
23
24ErrorBarsHandler.prototype = new BarsHandler();
25
26/** @inheritDoc */
27ErrorBarsHandler.prototype.extractSeries = function(rawData, i, options) {
28 // TODO(danvk): pre-allocate series here.
29 var series = [];
30 var x, y, variance, point;
31 const seriesLabel = options.get("labels")[i];
32 const logScale = options.getForSeries("logscale", seriesLabel);
33 const sigma = options.getForSeries("sigma", seriesLabel);
34 for ( var j = 0; j < rawData.length; j++) {
35 x = rawData[j][0];
36 point = rawData[j][i];
37 if (logScale && point !== null) {
38 // On the log scale, points less than zero do not exist.
39 // This will create a gap in the chart.
40 if (point[0] <= 0 || point[0] - sigma * point[1] <= 0) {
41 point = null;
42 }
43 }
44 // Extract to the unified data format.
45 if (point !== null) {
46 y = point[0];
47 if (y !== null && !isNaN(y)) {
48 variance = sigma * point[1];
49 // preserve original error value in extras for further
50 // filtering
51 series.push([ x, y, [ y - variance, y + variance, point[1] ] ]);
52 } else {
53 series.push([ x, y, [ y, y, y ] ]);
54 }
55 } else {
56 series.push([ x, null, [ null, null, null ] ]);
57 }
58 }
59 return series;
60};
61
62/** @inheritDoc */
63ErrorBarsHandler.prototype.rollingAverage =
64 function(originalData, rollPeriod, options, i) {
65 rollPeriod = Math.min(rollPeriod, originalData.length);
66 var rollingData = [];
67 const seriesLabel = options.get("labels")[i];
68 const sigma = options.getForSeries("sigma", seriesLabel);
69
70 var i, j, y, v, sum, num_ok, stddev, variance, value;
71
72 // Calculate the rolling average for the first rollPeriod - 1 points
73 // where there is not enough data to roll over the full number of points
74 for (i = 0; i < originalData.length; i++) {
75 sum = 0;
76 variance = 0;
77 num_ok = 0;
78 for (j = Math.max(0, i - rollPeriod + 1); j < i + 1; j++) {
79 y = originalData[j][1];
80 if (y === null || isNaN(y))
81 continue;
82 num_ok++;
83 sum += y;
84 variance += Math.pow(originalData[j][2][2], 2);
85 }
86 if (num_ok) {
87 stddev = Math.sqrt(variance) / num_ok;
88 value = sum / num_ok;
89 rollingData[i] = [ originalData[i][0], value,
90 [value - sigma * stddev, value + sigma * stddev] ];
91 } else {
92 // This explicitly preserves NaNs to aid with "independent
93 // series".
94 // See testRollingAveragePreservesNaNs.
95 v = (rollPeriod == 1) ? originalData[i][1] : null;
96 rollingData[i] = [ originalData[i][0], v, [ v, v ] ];
97 }
98 }
99
100 return rollingData;
101};
102
103export default ErrorBarsHandler;