Skip to content

Commit 7a17e09

Browse files
committed
first pass candlestick trace type
1 parent ac8097e commit 7a17e09

File tree

5 files changed

+269
-0
lines changed

5 files changed

+269
-0
lines changed

lib/candlestick.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
module.exports = require('../src/traces/candlestick');

src/traces/candlestick/attributes.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 OHLCattrs = require('../ohlc/attributes');
14+
var boxAttrs = require('../box/attributes');
15+
16+
var directionAttrs = {
17+
visible: {
18+
valType: 'enumerated',
19+
values: [true, false, 'legendonly'],
20+
role: 'info',
21+
dflt: true,
22+
description: [
23+
24+
].join(' ')
25+
},
26+
27+
color: Lib.extendFlat({}, boxAttrs.line.color),
28+
width: Lib.extendFlat({}, boxAttrs.line.width),
29+
fillcolor: Lib.extendFlat({}, boxAttrs.fillcolor),
30+
tickwidth: Lib.extendFlat({}, boxAttrs.whiskerwidth, { dflt: 0 }),
31+
};
32+
33+
module.exports = {
34+
t: OHLCattrs.t,
35+
open: OHLCattrs.open,
36+
high: OHLCattrs.high,
37+
low: OHLCattrs.low,
38+
close: OHLCattrs.close,
39+
40+
increasing: Lib.extendDeep({}, directionAttrs, {
41+
color: { dflt: 'green' }
42+
}),
43+
44+
decreasing: Lib.extendDeep({}, directionAttrs, {
45+
color: { dflt: 'red' }
46+
}),
47+
48+
text: OHLCattrs.text
49+
};

src/traces/candlestick/defaults.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 handleOHLC = require('../ohlc/ohlc_defaults');
14+
var attributes = require('./attributes');
15+
16+
module.exports = function supplyDefaults(traceIn, traceOut) {
17+
18+
function coerce(attr, dflt) {
19+
return Lib.coerce(traceIn, traceOut, attributes, attr, dflt);
20+
}
21+
22+
// add comment about this hack block
23+
var transformOpts = { type: 'candlestick' };
24+
if(Array.isArray(traceOut.transforms)) traceOut.transforms.push(transformOpts);
25+
else traceOut.transforms = [transformOpts];
26+
27+
var len = handleOHLC(traceIn, traceOut, coerce);
28+
29+
if(len === 0) {
30+
traceOut.visible = false;
31+
return;
32+
}
33+
34+
coerce('text');
35+
36+
handleDirection(traceOut, coerce, 'increasing');
37+
handleDirection(traceOut, coerce, 'decreasing');
38+
};
39+
40+
function handleDirection(traceOut, coerce, direction) {
41+
var dirVisible = coerce(direction + '.visible', traceOut.visible);
42+
43+
if(dirVisible) {
44+
coerce(direction + '.color');
45+
coerce(direction + '.width');
46+
coerce(direction + '.fillcolor');
47+
coerce(direction + '.tickwidth');
48+
}
49+
}

src/traces/candlestick/index.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 register = require('../../plot_api/register');
13+
14+
module.exports = {
15+
moduleType: 'trace',
16+
name: 'candlestick',
17+
basePlotModule: require('../../plots/cartesian'),
18+
categories: ['cartesian', 'showLegend'],
19+
meta: {
20+
description: [
21+
// ...
22+
].join(' ')
23+
},
24+
25+
attributes: require('./attributes'),
26+
supplyDefaults: require('./defaults'),
27+
};
28+
29+
register(require('../box'));
30+
register(require('./transform'));

src/traces/candlestick/transform.js

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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

Comments
 (0)