Skip to content

Commit 2b1918c

Browse files
committed
finance: improve inc / dec 'name' / 'showlegend' logic
- remove `increasing/decreasing.visible` attribute - add increasing/decreasing 'name' and 'showlegend'
1 parent 7b19b74 commit 2b1918c

File tree

8 files changed

+170
-76
lines changed

8 files changed

+170
-76
lines changed

src/traces/candlestick/attributes.js

+2-9
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,8 @@ var OHLCattrs = require('../ohlc/attributes');
1414
var boxAttrs = require('../box/attributes');
1515

1616
var directionAttrs = {
17-
visible: {
18-
valType: 'enumerated',
19-
values: [true, false, 'legendonly'],
20-
role: 'info',
21-
dflt: true,
22-
description: [
23-
24-
].join(' ')
25-
},
17+
name: OHLCattrs.increasing.name,
18+
showlegend: OHLCattrs.increasing.showlegend,
2619

2720
color: Lib.extendFlat({}, boxAttrs.line.color),
2821
width: Lib.extendFlat({}, boxAttrs.line.width),

src/traces/candlestick/defaults.js

+8-9
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 handleDirectionDefaults = require('../ohlc/direction_defaults');
1415
var helpers = require('../ohlc/helpers');
1516
var attributes = require('./attributes');
1617

@@ -30,16 +31,14 @@ module.exports = function supplyDefaults(traceIn, traceOut) {
3031
coerce('text');
3132
coerce('whiskerwidth');
3233

33-
handleDirection(traceOut, coerce, 'increasing');
34-
handleDirection(traceOut, coerce, 'decreasing');
34+
handleDirection(traceIn, traceOut, coerce, 'increasing');
35+
handleDirection(traceIn, traceOut, coerce, 'decreasing');
3536
};
3637

37-
function handleDirection(traceOut, coerce, direction) {
38-
var dirVisible = coerce(direction + '.visible', traceOut.visible);
38+
function handleDirection(traceIn, traceOut, coerce, direction) {
39+
handleDirectionDefaults(traceIn, traceOut, coerce, direction);
3940

40-
if(dirVisible) {
41-
coerce(direction + '.color');
42-
coerce(direction + '.width');
43-
coerce(direction + '.fillcolor');
44-
}
41+
coerce(direction + '.color');
42+
coerce(direction + '.width');
43+
coerce(direction + '.fillcolor');
4544
}

src/traces/candlestick/transform.js

+18-19
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
'use strict';
1111

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

1415
exports.moduleType = 'transform';
@@ -52,17 +53,9 @@ function makeTrace(traceIn, state, direction) {
5253
type: 'box',
5354
boxpoints: false,
5455

55-
// TODO could do better
56-
name: direction,
57-
58-
// TODO this doesn't restyle currently
59-
whiskerwidth: traceIn.whiskerwidth,
60-
61-
text: traceIn.text,
56+
visible: traceIn.visible,
6257
hoverinfo: traceIn.hoverinfo,
63-
6458
opacity: traceIn.opacity,
65-
showlegend: traceIn.showlegend,
6659

6760
transforms: helpers.makeTransform(traceIn, state, direction)
6861
};
@@ -72,21 +65,27 @@ function makeTrace(traceIn, state, direction) {
7265
var directionOpts = traceIn[direction];
7366

7467
if(directionOpts) {
68+
Lib.extendFlat(traceOut, {
7569

76-
// to make autotype catch date axes soon!!
77-
traceOut.x = traceIn.x || [0];
70+
// to make autotype catch date axes soon!!
71+
x: traceIn.x || [0],
7872

79-
// concat low and high to get correct autorange
80-
traceOut.y = [].concat(traceIn.low).concat(traceIn.high);
73+
// concat low and high to get correct autorange
74+
y: [].concat(traceIn.low).concat(traceIn.high),
8175

82-
traceOut.visible = directionOpts.visible;
76+
whiskerwidth: traceIn.whiskerwidth,
77+
text: traceIn.text,
8378

84-
traceOut.line = {
85-
color: directionOpts.color,
86-
width: directionOpts.width
87-
};
79+
name: directionOpts.name,
80+
showlegend: directionOpts.showlegend,
81+
82+
line: {
83+
color: directionOpts.color,
84+
width: directionOpts.width
85+
},
8886

89-
traceOut.fillcolor = directionOpts.fillcolor;
87+
fillcolor: directionOpts.fillcolor
88+
});
9089
}
9190

9291
return traceOut;

src/traces/ohlc/attributes.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,22 @@ var DECREASING_COLOR = '#FF4136';
1818
var lineAttrs = scatterAttrs.line;
1919

2020
var directionAttrs = {
21-
visible: {
22-
valType: 'enumerated',
23-
values: [true, false, 'legendonly'],
21+
name: {
22+
valType: 'string',
2423
role: 'info',
25-
dflt: true,
2624
description: [
25+
'Sets the segment name.',
26+
'The segment name appear as the legend item and on hover.'
27+
].join(' ')
28+
},
2729

30+
showlegend: {
31+
valType: 'boolean',
32+
role: 'info',
33+
dflt: true,
34+
description: [
35+
'Determines whether or not an item corresponding to this',
36+
'segment is shown in the legend.'
2837
].join(' ')
2938
},
3039

src/traces/ohlc/defaults.js

+8-9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

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

@@ -30,16 +31,14 @@ module.exports = function supplyDefaults(traceIn, traceOut) {
3031
coerce('text');
3132
coerce('tickwidth');
3233

33-
handleDirection(traceOut, coerce, 'increasing');
34-
handleDirection(traceOut, coerce, 'decreasing');
34+
handleDirection(traceIn, traceOut, coerce, 'increasing');
35+
handleDirection(traceIn, traceOut, coerce, 'decreasing');
3536
};
3637

37-
function handleDirection(traceOut, coerce, direction) {
38-
var dirVisible = coerce(direction + '.visible', traceOut.visible);
38+
function handleDirection(traceIn, traceOut, coerce, direction) {
39+
handleDirectionDefaults(traceIn, traceOut, coerce, direction);
3940

40-
if(dirVisible) {
41-
coerce(direction + '.color');
42-
coerce(direction + '.width');
43-
coerce(direction + '.dash');
44-
}
41+
coerce(direction + '.color');
42+
coerce(direction + '.width');
43+
coerce(direction + '.dash');
4544
}

src/traces/ohlc/direction_defaults.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
module.exports = function handleDirectionDefaults(traceIn, traceOut, coerce, direction) {
14+
coerce(direction + '.showlegend');
15+
16+
// trace-wide *showlegend* overrides direction *showlegend*
17+
if(traceIn.showlegend === false) {
18+
traceOut[direction].showlegend = false;
19+
}
20+
21+
var nameDflt = traceOut.name + ' - ' + direction;
22+
23+
coerce(direction + '.name', nameDflt);
24+
};

src/traces/ohlc/transform.js

+17-16
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
'use strict';
1111

12+
var Lib = require('../../lib');
1213
var helpers = require('./helpers');
1314
var axisIds = require('../../plots/cartesian/axis_ids');
1415

@@ -55,14 +56,9 @@ function makeTrace(traceIn, state, direction) {
5556
mode: 'lines',
5657
connectgaps: false,
5758

58-
// TODO could do better
59-
name: direction,
60-
61-
text: traceIn.text,
59+
visible: traceIn.visible,
6260
hoverinfo: traceIn.hoverinfo,
63-
6461
opacity: traceIn.opacity,
65-
showlegend: traceIn.showlegend,
6662

6763
transforms: helpers.makeTransform(traceIn, state, direction)
6864
};
@@ -72,20 +68,25 @@ function makeTrace(traceIn, state, direction) {
7268
var directionOpts = traceIn[direction];
7369

7470
if(directionOpts) {
71+
Lib.extendFlat(traceOut, {
7572

76-
// to make autotype catch date axes soon!!
77-
traceOut.x = traceIn.x || [0];
73+
// to make autotype catch date axes soon!!
74+
x: traceIn.x || [0],
7875

79-
// concat low and high to get correct autorange
80-
traceOut.y = [].concat(traceIn.low).concat(traceIn.high);
76+
// concat low and high to get correct autorange
77+
y: [].concat(traceIn.low).concat(traceIn.high),
8178

82-
traceOut.visible = directionOpts.visible;
79+
text: traceIn.text,
8380

84-
traceOut.line = {
85-
color: directionOpts.color,
86-
width: directionOpts.width,
87-
dash: directionOpts.dash
88-
};
81+
name: directionOpts.name,
82+
showlegend: directionOpts.showlegend,
83+
84+
line: {
85+
color: directionOpts.color,
86+
width: directionOpts.width,
87+
dash: directionOpts.dash
88+
}
89+
});
8990
}
9091

9192
return traceOut;

0 commit comments

Comments
 (0)