Skip to content

Commit 6f93045

Browse files
authored
Merge pull request #5963 from plotly/prefix-suffix-defaults
Separate file to handle prefix and suffix defaults
2 parents 2a3b38e + 5bf1014 commit 6f93045

File tree

9 files changed

+64
-54
lines changed

9 files changed

+64
-54
lines changed

src/components/colorbar/defaults.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var Template = require('../../plot_api/plot_template');
66
var handleTickValueDefaults = require('../../plots/cartesian/tick_value_defaults');
77
var handleTickMarkDefaults = require('../../plots/cartesian/tick_mark_defaults');
88
var handleTickLabelDefaults = require('../../plots/cartesian/tick_label_defaults');
9+
var handlePrefixSuffixDefaults = require('../../plots/cartesian/prefix_suffix_defaults');
910

1011
var attributes = require('./attributes');
1112

@@ -53,6 +54,7 @@ module.exports = function colorbarDefaults(containerIn, containerOut, layout) {
5354
if(ticklabelposition.indexOf('inside') !== -1) {
5455
opts.bgColor = 'black'; // could we instead use the average of colors in the scale?
5556
}
57+
handlePrefixSuffixDefaults(colorbarIn, colorbarOut, coerce, 'linear', opts);
5658
handleTickLabelDefaults(colorbarIn, colorbarOut, coerce, 'linear', opts);
5759
handleTickMarkDefaults(colorbarIn, colorbarOut, coerce, 'linear', opts);
5860

src/plots/cartesian/axis_defaults.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var layoutAttributes = require('./layout_attributes');
1111
var handleTickValueDefaults = require('./tick_value_defaults');
1212
var handleTickMarkDefaults = require('./tick_mark_defaults');
1313
var handleTickLabelDefaults = require('./tick_label_defaults');
14+
var handlePrefixSuffixDefaults = require('./prefix_suffix_defaults');
1415
var handleCategoryOrderDefaults = require('./category_order_defaults');
1516
var handleLineGridDefaults = require('./line_grid_defaults');
1617
var setConvert = require('./set_convert');
@@ -110,7 +111,7 @@ module.exports = function handleAxisDefaults(containerIn, containerOut, coerce,
110111
// try to get default title from splom trace, fallback to graph-wide value
111112
var dfltTitle = splomStash.label || layoutOut._dfltTitle[letter];
112113

113-
handleTickLabelDefaults(containerIn, containerOut, coerce, axType, options, {pass: 1});
114+
handlePrefixSuffixDefaults(containerIn, containerOut, coerce, axType, options);
114115
if(!visible) return containerOut;
115116

116117
coerce('title.text', dfltTitle);
@@ -121,7 +122,7 @@ module.exports = function handleAxisDefaults(containerIn, containerOut, coerce,
121122
});
122123

123124
handleTickValueDefaults(containerIn, containerOut, coerce, axType);
124-
handleTickLabelDefaults(containerIn, containerOut, coerce, axType, options, {pass: 2});
125+
handleTickLabelDefaults(containerIn, containerOut, coerce, axType, options);
125126
handleTickMarkDefaults(containerIn, containerOut, coerce, options);
126127
handleLineGridDefaults(containerIn, containerOut, coerce, {
127128
dfltColor: dfltColor,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
var getShowAttrDflt = require('./show_dflt');
4+
5+
module.exports = function handlePrefixSuffixDefaults(containerIn, containerOut, coerce, axType, options) {
6+
if(!options) options = {};
7+
var tickSuffixDflt = options.tickSuffixDflt;
8+
9+
var showAttrDflt = getShowAttrDflt(containerIn);
10+
11+
var tickPrefix = coerce('tickprefix');
12+
if(tickPrefix) coerce('showtickprefix', showAttrDflt);
13+
14+
var tickSuffix = coerce('ticksuffix', tickSuffixDflt);
15+
if(tickSuffix) coerce('showticksuffix', showAttrDflt);
16+
};

src/plots/cartesian/show_dflt.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
/*
4+
* Attributes 'showexponent', 'showtickprefix' and 'showticksuffix'
5+
* share values.
6+
*
7+
* If only 1 attribute is set,
8+
* the remaining attributes inherit that value.
9+
*
10+
* If 2 attributes are set to the same value,
11+
* the remaining attribute inherits that value.
12+
*
13+
* If 2 attributes are set to different values,
14+
* the remaining is set to its dflt value.
15+
*
16+
*/
17+
module.exports = function getShowAttrDflt(containerIn) {
18+
var showAttrsAll = ['showexponent', 'showtickprefix', 'showticksuffix'];
19+
var showAttrs = showAttrsAll.filter(function(a) {
20+
return containerIn[a] !== undefined;
21+
});
22+
var sameVal = function(a) {
23+
return containerIn[a] === containerIn[showAttrs[0]];
24+
};
25+
26+
if(showAttrs.every(sameVal) || showAttrs.length === 1) {
27+
return containerIn[showAttrs[0]];
28+
}
29+
};

src/plots/cartesian/tick_label_defaults.js

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,11 @@
33
var Lib = require('../../lib');
44
var contrast = require('../../components/color').contrast;
55
var layoutAttributes = require('./layout_attributes');
6+
var getShowAttrDflt = require('./show_dflt');
67
var handleArrayContainerDefaults = require('../array_container_defaults');
78

8-
module.exports = function handleTickLabelDefaults(containerIn, containerOut, coerce, axType, options, config) {
9-
if(!config || config.pass === 1) {
10-
handlePrefixSuffix(containerIn, containerOut, coerce, axType, options);
11-
}
12-
13-
if(!config || config.pass === 2) {
14-
handleOtherDefaults(containerIn, containerOut, coerce, axType, options);
15-
}
16-
};
17-
18-
function handlePrefixSuffix(containerIn, containerOut, coerce, axType, options) {
19-
var showAttrDflt = getShowAttrDflt(containerIn);
20-
21-
var tickPrefix = coerce('tickprefix');
22-
if(tickPrefix) coerce('showtickprefix', showAttrDflt);
23-
24-
var tickSuffix = coerce('ticksuffix', options.tickSuffixDflt);
25-
if(tickSuffix) coerce('showticksuffix', showAttrDflt);
26-
}
27-
28-
function handleOtherDefaults(containerIn, containerOut, coerce, axType, options) {
9+
module.exports = function handleTickLabelDefaults(containerIn, containerOut, coerce, axType, options) {
10+
if(!options) options = {};
2911
var showAttrDflt = getShowAttrDflt(containerIn);
3012

3113
var showTickLabels = coerce('showticklabels');
@@ -67,35 +49,7 @@ function handleOtherDefaults(containerIn, containerOut, coerce, axType, options)
6749
}
6850
}
6951
}
70-
}
71-
72-
/*
73-
* Attributes 'showexponent', 'showtickprefix' and 'showticksuffix'
74-
* share values.
75-
*
76-
* If only 1 attribute is set,
77-
* the remaining attributes inherit that value.
78-
*
79-
* If 2 attributes are set to the same value,
80-
* the remaining attribute inherits that value.
81-
*
82-
* If 2 attributes are set to different values,
83-
* the remaining is set to its dflt value.
84-
*
85-
*/
86-
function getShowAttrDflt(containerIn) {
87-
var showAttrsAll = ['showexponent', 'showtickprefix', 'showticksuffix'];
88-
var showAttrs = showAttrsAll.filter(function(a) {
89-
return containerIn[a] !== undefined;
90-
});
91-
var sameVal = function(a) {
92-
return containerIn[a] === containerIn[showAttrs[0]];
93-
};
94-
95-
if(showAttrs.every(sameVal) || showAttrs.length === 1) {
96-
return containerIn[showAttrs[0]];
97-
}
98-
}
52+
};
9953

10054
function tickformatstopDefaults(valueIn, valueOut) {
10155
function coerce(attr, dflt) {

src/plots/polar/layout_defaults.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var getSubplotData = require('../get_data').getSubplotData;
1010
var handleTickValueDefaults = require('../cartesian/tick_value_defaults');
1111
var handleTickMarkDefaults = require('../cartesian/tick_mark_defaults');
1212
var handleTickLabelDefaults = require('../cartesian/tick_label_defaults');
13+
var handlePrefixSuffixDefaults = require('../cartesian/prefix_suffix_defaults');
1314
var handleCategoryOrderDefaults = require('../cartesian/category_order_defaults');
1415
var handleLineGridDefaults = require('../cartesian/line_grid_defaults');
1516
var autoType = require('../cartesian/axis_autotype');
@@ -138,9 +139,10 @@ function handleDefaults(contIn, contOut, coerce, opts) {
138139

139140
if(visible) {
140141
handleTickValueDefaults(axIn, axOut, coerceAxis, axOut.type);
141-
handleTickLabelDefaults(axIn, axOut, coerceAxis, axOut.type, {
142+
handlePrefixSuffixDefaults(axIn, axOut, coerceAxis, axOut.type, {
142143
tickSuffixDflt: axOut.thetaunit === 'degrees' ? '°' : undefined
143144
});
145+
handleTickLabelDefaults(axIn, axOut, coerceAxis, axOut.type);
144146
handleTickMarkDefaults(axIn, axOut, coerceAxis, {outerTicks: true});
145147

146148
var showTickLabels = coerceAxis('showticklabels');

src/plots/ternary/layout_defaults.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var Lib = require('../../lib');
66

77
var handleSubplotDefaults = require('../subplot_defaults');
88
var handleTickLabelDefaults = require('../cartesian/tick_label_defaults');
9+
var handlePrefixSuffixDefaults = require('../cartesian/prefix_suffix_defaults');
910
var handleTickMarkDefaults = require('../cartesian/tick_mark_defaults');
1011
var handleTickValueDefaults = require('../cartesian/tick_value_defaults');
1112
var handleLineGridDefaults = require('../cartesian/line_grid_defaults');
@@ -90,7 +91,8 @@ function handleAxisDefaults(containerIn, containerOut, options, ternaryLayoutOut
9091
coerce('min');
9192

9293
handleTickValueDefaults(containerIn, containerOut, coerce, 'linear');
93-
handleTickLabelDefaults(containerIn, containerOut, coerce, 'linear', {});
94+
handlePrefixSuffixDefaults(containerIn, containerOut, coerce, 'linear');
95+
handleTickLabelDefaults(containerIn, containerOut, coerce, 'linear');
9496
handleTickMarkDefaults(containerIn, containerOut, coerce,
9597
{ outerTicks: true });
9698

src/traces/carpet/axis_defaults.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var Registry = require('../../registry');
77
var Lib = require('../../lib');
88
var handleTickValueDefaults = require('../../plots/cartesian/tick_value_defaults');
99
var handleTickLabelDefaults = require('../../plots/cartesian/tick_label_defaults');
10+
var handlePrefixSuffixDefaults = require('../../plots/cartesian/prefix_suffix_defaults');
1011
var handleCategoryOrderDefaults = require('../../plots/cartesian/category_order_defaults');
1112
var setConvert = require('../../plots/cartesian/set_convert');
1213
var autoType = require('../../plots/cartesian/axis_autotype');
@@ -128,6 +129,7 @@ module.exports = function handleAxisDefaults(containerIn, containerOut, options)
128129
coerce('fixedrange');
129130

130131
handleTickValueDefaults(containerIn, containerOut, coerce, axType);
132+
handlePrefixSuffixDefaults(containerIn, containerOut, coerce, axType, options);
131133
handleTickLabelDefaults(containerIn, containerOut, coerce, axType, options);
132134
handleCategoryOrderDefaults(containerIn, containerOut, coerce, {
133135
data: options.data,

src/traces/indicator/defaults.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var cn = require('./constants.js');
1010
var handleTickValueDefaults = require('../../plots/cartesian/tick_value_defaults');
1111
var handleTickMarkDefaults = require('../../plots/cartesian/tick_mark_defaults');
1212
var handleTickLabelDefaults = require('../../plots/cartesian/tick_label_defaults');
13+
var handlePrefixSuffixDefaults = require('../../plots/cartesian/prefix_suffix_defaults');
1314

1415
function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
1516
function coerce(attr, dflt) {
@@ -128,6 +129,7 @@ function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
128129

129130
var opts = {outerTicks: true};
130131
handleTickValueDefaults(axisIn, axisOut, coerceGaugeAxis, 'linear');
132+
handlePrefixSuffixDefaults(axisIn, axisOut, coerceGaugeAxis, 'linear', opts);
131133
handleTickLabelDefaults(axisIn, axisOut, coerceGaugeAxis, 'linear', opts);
132134
handleTickMarkDefaults(axisIn, axisOut, coerceGaugeAxis, opts);
133135
} else {

0 commit comments

Comments
 (0)