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