Skip to content

Commit ac8097e

Browse files
committed
first pass ohlc trace type
1 parent 5b5a5a0 commit ac8097e

File tree

7 files changed

+403
-0
lines changed

7 files changed

+403
-0
lines changed

lib/ohlc.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/ohlc');

src/traces/ohlc/attributes.js

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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 scatterAttrs = require('../scatter/attributes');
14+
15+
var lineAttrs = scatterAttrs.line;
16+
17+
var directionAttrs = {
18+
visible: {
19+
valType: 'enumerated',
20+
values: [true, false, 'legendonly'],
21+
role: 'info',
22+
dflt: true,
23+
description: [
24+
25+
].join(' ')
26+
},
27+
28+
color: Lib.extendFlat({}, lineAttrs.color),
29+
width: Lib.extendFlat({}, lineAttrs.width),
30+
dash: Lib.extendFlat({}, lineAttrs.dash),
31+
32+
tickwidth: {
33+
valType: 'number',
34+
min: 0,
35+
max: 1,
36+
dflt: 0.1,
37+
role: 'style',
38+
description: [
39+
'Sets the width of the open/close tick marks',
40+
'relative to the *t* minimal interval.'
41+
].join(' ')
42+
}
43+
};
44+
45+
module.exports = {
46+
47+
// or should this be 'x'
48+
//
49+
//
50+
// should we add the option for ohlc along y-axis ??
51+
t: {
52+
valType: 'data_array',
53+
description: [
54+
'Sets the time coordinate.',
55+
'If absent, linear coordinate will be generated.'
56+
].join(' ')
57+
},
58+
59+
open: {
60+
valType: 'data_array',
61+
dflt: [],
62+
description: 'Sets the open values.'
63+
},
64+
65+
high: {
66+
valType: 'data_array',
67+
dflt: [],
68+
description: 'Sets the high values.'
69+
},
70+
71+
low: {
72+
valType: 'data_array',
73+
dflt: [],
74+
description: 'Sets the low values.'
75+
},
76+
77+
close: {
78+
valType: 'data_array',
79+
dflt: [],
80+
description: 'Sets the close values.'
81+
},
82+
83+
// TODO find better colors
84+
increasing: Lib.extendDeep({}, directionAttrs, {
85+
color: { dflt: 'green' }
86+
}),
87+
88+
decreasing: Lib.extendDeep({}, directionAttrs, {
89+
color: { dflt: 'red' }
90+
}),
91+
92+
text: {
93+
valType: 'string',
94+
role: 'info',
95+
dflt: '',
96+
arrayOk: true,
97+
description: [
98+
'Sets hover text elements associated with each sample point.',
99+
'If a single string, the same string appears over',
100+
'all the data points.',
101+
'If an array of string, the items are mapped in order to the',
102+
'this trace\'s sample points.'
103+
].join(' ')
104+
}
105+
};

src/traces/ohlc/defaults.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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_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+
// TODO .. HAVE TO TEST FILTER with 't', 'high', 'open', 'low' ...
23+
24+
var transformOpts = { type: 'ohlc' };
25+
if(Array.isArray(traceOut.transforms)) traceOut.transforms.push(transformOpts);
26+
else traceOut.transforms = [transformOpts];
27+
28+
var len = handleOHLC(traceIn, traceOut, coerce);
29+
30+
if(len === 0) {
31+
traceOut.visible = false;
32+
return;
33+
}
34+
35+
coerce('text');
36+
37+
handleDirection(traceOut, coerce, 'increasing');
38+
handleDirection(traceOut, coerce, 'decreasing');
39+
};
40+
41+
function handleDirection(traceOut, coerce, direction) {
42+
var dirVisible = coerce(direction + '.visible', traceOut.visible);
43+
44+
if(dirVisible) {
45+
coerce(direction + '.color');
46+
coerce(direction + '.width');
47+
coerce(direction + '.dash');
48+
coerce(direction + '.tickwidth');
49+
}
50+
}

src/traces/ohlc/helpers.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
13+
exports.getFilterFn = function getFilterFn(direction) {
14+
switch(direction) {
15+
case 'increasing':
16+
return function(o, c) { return o <= c; };
17+
18+
case 'decreasing':
19+
return function(o, c) { return o > c; };
20+
}
21+
};
22+
23+
exports.addRangeSlider = function addRangeSlider(layout) {
24+
if(!layout.xaxis) layout.xaxis = {};
25+
if(!layout.xaxis.rangeslider) layout.xaxis.rangeslider = {};
26+
};

src/traces/ohlc/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: 'ohlc',
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('../scatter'));
30+
register(require('./transform'));

src/traces/ohlc/ohlc_defaults.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
module.exports = function handleOHLC(traceIn, traceOut, coerce) {
13+
var len;
14+
15+
var t = coerce('t'),
16+
open = coerce('open'),
17+
high = coerce('high'),
18+
low = coerce('low'),
19+
close = coerce('close');
20+
21+
len = Math.min(open.length, high.length, low.length, close.length);
22+
23+
if(t) {
24+
len = Math.min(len, t.length);
25+
t.slice(0, len);
26+
}
27+
28+
open = open.slice(0, len);
29+
high = high.slice(0, len);
30+
low = low.slice(0, len);
31+
close = close.slice(0, len);
32+
33+
return len;
34+
};

0 commit comments

Comments
 (0)