UNPKG

2.61 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 custom 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 Dygraph.DataHandlers.BarsHandler
20 */
21var CustomBarsHandler = function() {
22};
23
24CustomBarsHandler.prototype = new BarsHandler();
25
26/** @inheritDoc */
27CustomBarsHandler.prototype.extractSeries = function(rawData, i, options) {
28 // TODO(danvk): pre-allocate series here.
29 var series = [];
30 var x, y, point;
31 const seriesLabel = options.get("labels")[i];
32 const logScale = options.getForSeries("logscale", seriesLabel);
33 for ( var j = 0; j < rawData.length; j++) {
34 x = rawData[j][0];
35 point = rawData[j][i];
36 if (logScale && point !== null) {
37 // On the log scale, points less than zero do not exist.
38 // This will create a gap in the chart.
39 if (point[0] <= 0 || point[1] <= 0 || point[2] <= 0) {
40 point = null;
41 }
42 }
43 // Extract to the unified data format.
44 if (point !== null) {
45 y = point[1];
46 if (y !== null && !isNaN(y)) {
47 series.push([ x, y, [ point[0], point[2] ] ]);
48 } else {
49 series.push([ x, y, [ y, y ] ]);
50 }
51 } else {
52 series.push([ x, null, [ null, null ] ]);
53 }
54 }
55 return series;
56};
57
58/** @inheritDoc */
59CustomBarsHandler.prototype.rollingAverage =
60 function(originalData, rollPeriod, options, i) {
61 rollPeriod = Math.min(rollPeriod, originalData.length);
62 var rollingData = [];
63 var y, low, high, mid,count, i, extremes;
64
65 low = 0;
66 mid = 0;
67 high = 0;
68 count = 0;
69 for (i = 0; i < originalData.length; i++) {
70 y = originalData[i][1];
71 extremes = originalData[i][2];
72 rollingData[i] = originalData[i];
73
74 if (y !== null && !isNaN(y)) {
75 low += extremes[0];
76 mid += y;
77 high += extremes[1];
78 count += 1;
79 }
80 if (i - rollPeriod >= 0) {
81 var prev = originalData[i - rollPeriod];
82 if (prev[1] !== null && !isNaN(prev[1])) {
83 low -= prev[2][0];
84 mid -= prev[1];
85 high -= prev[2][1];
86 count -= 1;
87 }
88 }
89 if (count) {
90 rollingData[i] = [
91 originalData[i][0],
92 1.0 * mid / count,
93 [ 1.0 * low / count,
94 1.0 * high / count ] ];
95 } else {
96 rollingData[i] = [ originalData[i][0], null, [ null, null ] ];
97 }
98 }
99
100 return rollingData;
101};
102
103export default CustomBarsHandler;