-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathtransform.js
240 lines (180 loc) · 6.26 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/**
* Copyright 2012-2016, 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 Lib = require('../../lib');
var helpers = require('./helpers');
var Axes = require('../../plots/cartesian/axes');
var axisIds = require('../../plots/cartesian/axis_ids');
exports.moduleType = 'transform';
exports.name = 'ohlc';
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 !== 'ohlc') {
dataOut.push(traceIn);
continue;
}
dataOut.push(
makeTrace(traceIn, state, 'increasing'),
makeTrace(traceIn, state, 'decreasing')
);
}
helpers.addRangeSlider(state.layout);
return dataOut;
};
function makeTrace(traceIn, state, direction) {
var traceOut = {
type: 'scatter',
mode: 'lines',
connectgaps: false,
visible: traceIn.visible,
opacity: traceIn.opacity,
xaxis: traceIn.xaxis,
yaxis: traceIn.yaxis,
hoverinfo: makeHoverInfo(traceIn),
transforms: helpers.makeTransform(traceIn, state, direction)
};
// 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],
// concat low and high to get correct autorange
y: [].concat(traceIn.low).concat(traceIn.high),
text: traceIn.text,
name: directionOpts.name,
showlegend: directionOpts.showlegend,
line: directionOpts.line
});
}
return traceOut;
}
// Let scatter hoverPoint format 'x' coordinates, if desired.
//
// Note that, this solution isn't perfect: it shows open and close
// values at slightly different 'x' coordinates then the rest of the
// segments, but is for more robust than calling `Axes.tickText` during
// calcTransform.
//
// A future iteration should perhaps try to add a hook for transforms in
// the hoverPoints handlers.
function makeHoverInfo(traceIn) {
var hoverinfo = traceIn.hoverinfo;
if(hoverinfo === 'all') return 'x+text+name';
var parts = hoverinfo.split('+'),
indexOfY = parts.indexOf('y'),
indexOfText = parts.indexOf('text');
if(indexOfY !== -1) {
parts.splice(indexOfY, 1);
if(indexOfText === -1) parts.push('text');
}
return parts.join('+');
}
exports.calcTransform = function calcTransform(gd, trace, opts) {
var direction = opts.direction,
filterFn = helpers.getFilterFn(direction);
var xa = axisIds.getFromTrace(gd, trace, 'x'),
ya = axisIds.getFromTrace(gd, trace, 'y'),
tickWidth = convertTickWidth(gd, xa, trace);
var open = trace.open,
high = trace.high,
low = trace.low,
close = trace.close,
textIn = trace.text;
var len = open.length,
x = [],
y = [],
textOut = [];
var getXItem = trace._fullInput.x ?
function(i) { return xa.d2c(trace.x[i]); } :
function(i) { return i; };
var getTextItem = Array.isArray(textIn) ?
function(i) { return textIn[i] || ''; } :
function() { return textIn; };
var appendX = function(i) {
var v = getXItem(i);
x.push(v - tickWidth, v, v, v, v, v + tickWidth, null);
};
var appendY = function(o, h, l, c) {
y.push(o, o, h, l, c, c, null);
};
var format = function(ax, val) {
return Axes.tickText(ax, ax.c2l(val), 'hover').text;
};
var hoverinfo = trace._fullInput.hoverinfo,
hoverParts = hoverinfo.split('+'),
hasAll = hoverinfo === 'all',
hasY = hasAll || hoverParts.indexOf('y') !== -1,
hasText = hasAll || hoverParts.indexOf('text') !== -1;
var appendText = function(i, o, h, l, c) {
var t = [];
if(hasY) {
t.push('Open: ' + format(ya, o));
t.push('High: ' + format(ya, h));
t.push('Low: ' + format(ya, l));
t.push('Close: ' + format(ya, c));
}
if(hasText) t.push(getTextItem(i));
var _t = t.join('<br>');
textOut.push(_t, _t, _t, _t, _t, _t, null);
};
for(var i = 0; i < len; i++) {
if(filterFn(open[i], close[i])) {
appendX(i);
appendY(open[i], high[i], low[i], close[i]);
appendText(i, open[i], high[i], low[i], close[i]);
}
}
trace.x = x;
trace.y = y;
trace.text = textOut;
};
function convertTickWidth(gd, xa, trace) {
var fullInput = trace._fullInput,
tickWidth = fullInput.tickwidth,
minDiff = fullInput._minDiff;
if(!minDiff) {
var fullData = gd._fullData,
ohlcTracesOnThisXaxis = [];
minDiff = Infinity;
// find min x-coordinates difference of all traces
// attached to this x-axis and stash the result
var i;
for(i = 0; i < fullData.length; i++) {
var _trace = fullData[i]._fullInput;
if(_trace.type === 'ohlc' &&
_trace.visible === true &&
_trace.xaxis === xa._id
) {
ohlcTracesOnThisXaxis.push(_trace);
// - _trace.x may be undefined here,
// it is filled later in calcTransform
//
// - handle trace of length 1 separately.
if(_trace.x && _trace.x.length > 1) {
var _minDiff = Lib.distinctVals(_trace.x.map(xa.d2c)).minDiff;
minDiff = Math.min(minDiff, _minDiff);
}
}
}
// if minDiff is still Infinity here, set it to 1
if(minDiff === Infinity) minDiff = 1;
for(i = 0; i < ohlcTracesOnThisXaxis.length; i++) {
ohlcTracesOnThisXaxis[i]._minDiff = minDiff;
}
}
return minDiff * tickWidth;
}