|
| 1 | +/** |
| 2 | +* Copyright 2012-2016, Plotly, Inc. |
| 3 | +* All rights reserved. |
| 4 | +* |
| 5 | +* This source code is licensed under the MIT license found in the |
| 6 | +* LICENSE file in the root directory of this source tree. |
| 7 | +*/ |
| 8 | + |
| 9 | + |
| 10 | +'use strict'; |
| 11 | + |
| 12 | +var Lib = require('../../lib'); |
| 13 | +var helpers = require('../ohlc/helpers'); |
| 14 | + |
| 15 | +exports.moduleType = 'transform'; |
| 16 | + |
| 17 | +exports.name = 'candlestick'; |
| 18 | + |
| 19 | +exports.attributes = {}; |
| 20 | + |
| 21 | +exports.supplyDefaults = null; |
| 22 | + |
| 23 | +exports.transform = function transform(dataIn, state) { |
| 24 | + var dataOut = []; |
| 25 | + |
| 26 | + for(var i = 0; i < dataIn.length; i++) { |
| 27 | + var traceIn = dataIn[i]; |
| 28 | + |
| 29 | + if(traceIn.type !== 'candlestick') { |
| 30 | + dataOut.push(traceIn); |
| 31 | + continue; |
| 32 | + } |
| 33 | + |
| 34 | + dataOut.push( |
| 35 | + makeTrace(traceIn, state, 'increasing'), |
| 36 | + makeTrace(traceIn, state, 'decreasing') |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + // add a few layout features |
| 41 | + var layout = state.layout; |
| 42 | + helpers.addRangeSlider(layout); |
| 43 | + |
| 44 | + return dataOut; |
| 45 | +}; |
| 46 | + |
| 47 | +function makeTrace(traceIn, state, direction) { |
| 48 | + var directionOpts = traceIn[direction]; |
| 49 | + |
| 50 | + // We need to track which direction ('increasing' or 'decreasing') |
| 51 | + // the generated correspond to for the calcTransform step. |
| 52 | + // |
| 53 | + // To make sure that direction reaches the calcTransform, |
| 54 | + // store it in the transform opts object. |
| 55 | + var _transforms = Lib.extendFlat([], traceIn.transforms); |
| 56 | + _transforms[state.transformIndex] = { |
| 57 | + type: 'candlestick', |
| 58 | + direction: direction |
| 59 | + }; |
| 60 | + |
| 61 | + return { |
| 62 | + type: 'box', |
| 63 | + boxpoints: false, |
| 64 | + |
| 65 | + // TODO could do better |
| 66 | + name: direction, |
| 67 | + |
| 68 | + // to make autotype catch date axes soon!! |
| 69 | + x: traceIn.t || [0], |
| 70 | + |
| 71 | + // concat low and high to get correct autorange |
| 72 | + y: [].concat(traceIn.low).concat(traceIn.high), |
| 73 | + |
| 74 | + visible: directionOpts.visible, |
| 75 | + |
| 76 | + line: { |
| 77 | + color: directionOpts.color, |
| 78 | + width: directionOpts.width |
| 79 | + }, |
| 80 | + fillcolor: directionOpts.fillcolor, |
| 81 | + |
| 82 | + // TODO this doesn't restyle currently |
| 83 | + whiskerwidth: directionOpts.tickwidth, |
| 84 | + |
| 85 | + text: traceIn.text, |
| 86 | + hoverinfo: traceIn.hoverinfo, |
| 87 | + |
| 88 | + opacity: traceIn.opacity, |
| 89 | + showlegend: traceIn.showlegend, |
| 90 | + |
| 91 | + transforms: _transforms |
| 92 | + }; |
| 93 | +} |
| 94 | + |
| 95 | +exports.calcTransform = function calcTransform(gd, trace, opts) { |
| 96 | + var fullInput = trace._fullInput, |
| 97 | + direction = opts.direction; |
| 98 | + |
| 99 | + var filterFn = helpers.getFilterFn(direction); |
| 100 | + |
| 101 | + var open = fullInput.open, |
| 102 | + high = fullInput.high, |
| 103 | + low = fullInput.low, |
| 104 | + close = fullInput.close; |
| 105 | + |
| 106 | + // sliced accordingly in supply-defaults |
| 107 | + var len = open.length; |
| 108 | + |
| 109 | + // clear generated trace x / y |
| 110 | + trace.x = []; |
| 111 | + trace.y = []; |
| 112 | + |
| 113 | + var appendX = fullInput.t ? |
| 114 | + function(i) { |
| 115 | + var t = fullInput.t[i]; |
| 116 | + trace.x.push(t, t, t, t, t, t); |
| 117 | + } : |
| 118 | + function(i) { |
| 119 | + trace.x.push(i, i, i, i, i, i); |
| 120 | + }; |
| 121 | + |
| 122 | + var appendY = function(o, h, l, c) { |
| 123 | + trace.y.push(l, o, c, c, c, h); |
| 124 | + }; |
| 125 | + |
| 126 | + for(var i = 0; i < len; i++) { |
| 127 | + if(filterFn(open[i], close[i])) { |
| 128 | + appendX(i); |
| 129 | + appendY(open[i], high[i], low[i], close[i]); |
| 130 | + } |
| 131 | + } |
| 132 | +}; |
0 commit comments