-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathtransform.js
129 lines (96 loc) · 3.03 KB
/
transform.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* Copyright 2012-2018, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var isNumeric = require('fast-isnumeric');
var Lib = require('../../lib');
var helpers = require('../ohlc/helpers');
exports.moduleType = 'transform';
exports.name = 'candlestick';
exports.attributes = {};
exports.supplyDefaults = function(transformIn, traceOut, layout, traceIn) {
helpers.clearEphemeralTransformOpts(traceIn);
helpers.copyOHLC(transformIn, traceOut);
return transformIn;
};
exports.transform = function transform(dataIn, state) {
var dataOut = [];
for(var i = 0; i < dataIn.length; i++) {
var traceIn = dataIn[i];
if(traceIn.type !== 'candlestick') {
dataOut.push(traceIn);
continue;
}
dataOut.push(
makeTrace(traceIn, state, 'increasing'),
makeTrace(traceIn, state, 'decreasing')
);
}
helpers.addRangeSlider(dataOut, state.layout);
return dataOut;
};
function makeTrace(traceIn, state, direction) {
var traceOut = {
type: 'box',
boxpoints: false,
visible: traceIn.visible,
hoverinfo: traceIn.hoverinfo,
opacity: traceIn.opacity,
xaxis: traceIn.xaxis,
yaxis: traceIn.yaxis,
transforms: helpers.makeTransform(traceIn, state, direction),
_inputLength: traceIn._inputLength
};
// the rest of below may not have been coerced
var directionOpts = traceIn[direction];
if(directionOpts) {
Lib.extendFlat(traceOut, {
// to make autotype catch date axes soon!!
x: traceIn.x || [0],
xcalendar: traceIn.xcalendar,
// concat low and high to get correct autorange
y: [].concat(traceIn.low).concat(traceIn.high),
whiskerwidth: traceIn.whiskerwidth,
text: traceIn.text,
name: directionOpts.name,
showlegend: directionOpts.showlegend,
line: directionOpts.line,
fillcolor: directionOpts.fillcolor
});
}
return traceOut;
}
exports.calcTransform = function calcTransform(gd, trace, opts) {
var direction = opts.direction,
filterFn = helpers.getFilterFn(direction);
var open = trace.open,
high = trace.high,
low = trace.low,
close = trace.close;
var len = trace._inputLength,
x = [],
y = [];
var appendX = trace._fullInput.x ?
function(i) {
var v = trace.x[i];
x.push(v, v, v, v, v, v);
} :
function(i) {
x.push(i, i, i, i, i, i);
};
var appendY = function(o, h, l, c) {
y.push(l, o, c, c, c, h);
};
for(var i = 0; i < len; i++) {
if(filterFn(open[i], close[i]) && isNumeric(high[i]) && isNumeric(low[i])) {
appendX(i);
appendY(open[i], high[i], low[i], close[i]);
}
}
trace.x = x;
trace.y = y;
};