Skip to content

Commit f6c33f2

Browse files
committed
finance: 2nd iteration
- factor out hacky moves in ohlc/helpers.js - pass open, high, low, close array in transform opts, then pass then to traceOut in transform supplyDefaults - use trace (not trace._fullInput) to filter -> this makes ohlc and candlestick compatible with filters & groupby
1 parent ede8ee2 commit f6c33f2

File tree

5 files changed

+148
-119
lines changed

5 files changed

+148
-119
lines changed

src/traces/candlestick/defaults.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
var Lib = require('../../lib');
1313
var handleOHLC = require('../ohlc/ohlc_defaults');
14+
var helpers = require('../ohlc/helpers');
1415
var attributes = require('./attributes');
1516

1617
module.exports = function supplyDefaults(traceIn, traceOut) {
@@ -19,13 +20,10 @@ module.exports = function supplyDefaults(traceIn, traceOut) {
1920
return Lib.coerce(traceIn, traceOut, attributes, attr, dflt);
2021
}
2122

22-
// add comment about this hack block
2323
var transformOpts = { type: 'candlestick' };
24-
if(Array.isArray(traceOut.transforms)) traceOut.transforms.push(transformOpts);
25-
else traceOut.transforms = [transformOpts];
24+
helpers.prependTransformOpts(traceIn, traceOut, transformOpts);
2625

2726
var len = handleOHLC(traceIn, traceOut, coerce);
28-
2927
if(len === 0) {
3028
traceOut.visible = false;
3129
return;

src/traces/candlestick/transform.js

Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
'use strict';
1111

12-
var Lib = require('../../lib');
1312
var helpers = require('../ohlc/helpers');
1413

1514
exports.moduleType = 'transform';
@@ -18,7 +17,11 @@ exports.name = 'candlestick';
1817

1918
exports.attributes = {};
2019

21-
exports.supplyDefaults = null;
20+
exports.supplyDefaults = function(transformIn, traceOut) {
21+
helpers.copyOHLC(transformIn, traceOut);
22+
23+
return transformIn;
24+
};
2225

2326
exports.transform = function transform(dataIn, state) {
2427
var dataOut = [];
@@ -45,82 +48,74 @@ exports.transform = function transform(dataIn, state) {
4548
};
4649

4750
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 {
51+
var traceOut = {
6252
type: 'box',
6353
boxpoints: false,
6454

6555
// TODO could do better
6656
name: direction,
6757

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-
8258
// TODO this doesn't restyle currently
83-
whiskerwidth: directionOpts.tickwidth,
59+
whiskerwidth: traceIn.whiskerwidth,
8460

8561
text: traceIn.text,
8662
hoverinfo: traceIn.hoverinfo,
8763

8864
opacity: traceIn.opacity,
8965
showlegend: traceIn.showlegend,
9066

91-
transforms: _transforms
67+
transforms: helpers.makeTransform(traceIn, state, direction)
9268
};
93-
}
9469

95-
exports.calcTransform = function calcTransform(gd, trace, opts) {
96-
var fullInput = trace._fullInput,
97-
direction = opts.direction;
70+
// the rest of below may not have been coerced
9871

99-
var filterFn = helpers.getFilterFn(direction);
72+
var directionOpts = traceIn[direction];
10073

101-
var open = fullInput.open,
102-
high = fullInput.high,
103-
low = fullInput.low,
104-
close = fullInput.close;
74+
if(directionOpts) {
10575

106-
// sliced accordingly in supply-defaults
107-
var len = open.length;
76+
// to make autotype catch date axes soon!!
77+
traceOut.x = traceIn.x || [0];
78+
79+
// concat low and high to get correct autorange
80+
traceOut.y = [].concat(traceIn.low).concat(traceIn.high);
81+
82+
traceOut.visible = directionOpts.visible;
83+
84+
traceOut.line = {
85+
color: directionOpts.color,
86+
width: directionOpts.width
87+
};
10888

109-
// clear generated trace x / y
110-
trace.x = [];
111-
trace.y = [];
89+
traceOut.fillcolor = directionOpts.fillcolor;
90+
}
91+
92+
return traceOut;
93+
}
94+
95+
exports.calcTransform = function calcTransform(gd, trace, opts) {
96+
var direction = opts.direction,
97+
filterFn = helpers.getFilterFn(direction);
98+
99+
var open = trace.open,
100+
high = trace.high,
101+
low = trace.low,
102+
close = trace.close;
112103

113-
var appendX = fullInput.t ?
104+
var len = open.length,
105+
x = [],
106+
y = [];
107+
108+
var appendX = trace._fullInput.x ?
114109
function(i) {
115-
var t = fullInput.t[i];
116-
trace.x.push(t, t, t, t, t, t);
110+
var v = trace.x[i];
111+
x.push(v, v, v, v, v, v);
117112
} :
118113
function(i) {
119-
trace.x.push(i, i, i, i, i, i);
114+
x.push(i, i, i, i, i, i);
120115
};
121116

122117
var appendY = function(o, h, l, c) {
123-
trace.y.push(l, o, c, c, c, h);
118+
y.push(l, o, c, c, c, h);
124119
};
125120

126121
for(var i = 0; i < len; i++) {
@@ -129,4 +124,7 @@ exports.calcTransform = function calcTransform(gd, trace, opts) {
129124
appendY(open[i], high[i], low[i], close[i]);
130125
}
131126
}
127+
128+
trace.x = x;
129+
trace.y = y;
132130
};

src/traces/ohlc/defaults.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,18 @@
1212
var Lib = require('../../lib');
1313
var handleOHLC = require('./ohlc_defaults');
1414
var attributes = require('./attributes');
15+
var helpers = require('./helpers');
1516

1617
module.exports = function supplyDefaults(traceIn, traceOut) {
1718

1819
function coerce(attr, dflt) {
1920
return Lib.coerce(traceIn, traceOut, attributes, attr, dflt);
2021
}
2122

22-
// TODO .. HAVE TO TEST FILTER with 't', 'high', 'open', 'low' ...
23-
2423
var transformOpts = { type: 'ohlc' };
25-
if(Array.isArray(traceOut.transforms)) traceOut.transforms.push(transformOpts);
26-
else traceOut.transforms = [transformOpts];
24+
helpers.prependTransformOpts(traceIn, traceOut, transformOpts);
2725

2826
var len = handleOHLC(traceIn, traceOut, coerce);
29-
3027
if(len === 0) {
3128
traceOut.visible = false;
3229
return;

src/traces/ohlc/helpers.js

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,49 @@
99

1010
'use strict';
1111

12+
var Lib = require('../../lib');
1213

13-
exports.getFilterFn = function getFilterFn(direction) {
14+
// TODO add comment
15+
exports.prependTransformOpts = function(traceIn, traceOut, transformOpts) {
16+
if(Array.isArray(traceIn.transforms)) {
17+
traceIn.transforms.push(transformOpts);
18+
}
19+
else {
20+
traceIn.transforms = [transformOpts];
21+
}
22+
};
23+
24+
// TODO add comment
25+
exports.copyOHLC = function(container, traceOut) {
26+
if(container.open) traceOut.open = container.open;
27+
if(container.high) traceOut.high = container.high;
28+
if(container.low) traceOut.low = container.low;
29+
if(container.close) traceOut.close = container.close;
30+
};
31+
32+
// We need to track which direction ('increasing' or 'decreasing')
33+
// the generated correspond to for the calcTransform step.
34+
//
35+
// To make sure that direction reaches the calcTransform,
36+
// store it in the transform opts object.
37+
exports.makeTransform = function(traceIn, state, direction) {
38+
var out = Lib.extendFlat([], traceIn.transforms);
39+
40+
out[state.transformIndex] = {
41+
type: traceIn.type,
42+
direction: direction,
43+
44+
// ...
45+
open: traceIn.open,
46+
high: traceIn.high,
47+
low: traceIn.low,
48+
close: traceIn.close
49+
};
50+
51+
return out;
52+
};
53+
54+
exports.getFilterFn = function(direction) {
1455
switch(direction) {
1556
case 'increasing':
1657
return function(o, c) { return o <= c; };
@@ -20,7 +61,7 @@ exports.getFilterFn = function getFilterFn(direction) {
2061
}
2162
};
2263

23-
exports.addRangeSlider = function addRangeSlider(layout) {
64+
exports.addRangeSlider = function(layout) {
2465
if(!layout.xaxis) layout.xaxis = {};
2566
if(!layout.xaxis.rangeslider) layout.xaxis.rangeslider = {};
2667
};

0 commit comments

Comments
 (0)