Skip to content

Separate file to handle prefix and suffix defaults #5963

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/components/colorbar/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var Template = require('../../plot_api/plot_template');
var handleTickValueDefaults = require('../../plots/cartesian/tick_value_defaults');
var handleTickMarkDefaults = require('../../plots/cartesian/tick_mark_defaults');
var handleTickLabelDefaults = require('../../plots/cartesian/tick_label_defaults');
var handlePrefixSuffixDefaults = require('../../plots/cartesian/prefix_suffix_defaults');

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

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

Expand Down
5 changes: 3 additions & 2 deletions src/plots/cartesian/axis_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var layoutAttributes = require('./layout_attributes');
var handleTickValueDefaults = require('./tick_value_defaults');
var handleTickMarkDefaults = require('./tick_mark_defaults');
var handleTickLabelDefaults = require('./tick_label_defaults');
var handlePrefixSuffixDefaults = require('./prefix_suffix_defaults');
var handleCategoryOrderDefaults = require('./category_order_defaults');
var handleLineGridDefaults = require('./line_grid_defaults');
var setConvert = require('./set_convert');
Expand Down Expand Up @@ -110,7 +111,7 @@ module.exports = function handleAxisDefaults(containerIn, containerOut, coerce,
// try to get default title from splom trace, fallback to graph-wide value
var dfltTitle = splomStash.label || layoutOut._dfltTitle[letter];

handleTickLabelDefaults(containerIn, containerOut, coerce, axType, options, {pass: 1});
handlePrefixSuffixDefaults(containerIn, containerOut, coerce, axType, options);
if(!visible) return containerOut;

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

handleTickValueDefaults(containerIn, containerOut, coerce, axType);
handleTickLabelDefaults(containerIn, containerOut, coerce, axType, options, {pass: 2});
handleTickLabelDefaults(containerIn, containerOut, coerce, axType, options);
handleTickMarkDefaults(containerIn, containerOut, coerce, options);
handleLineGridDefaults(containerIn, containerOut, coerce, {
dfltColor: dfltColor,
Expand Down
16 changes: 16 additions & 0 deletions src/plots/cartesian/prefix_suffix_defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

var getShowAttrDflt = require('./show_dflt');

module.exports = function handlePrefixSuffixDefaults(containerIn, containerOut, coerce, axType, options) {
if(!options) options = {};
var tickSuffixDflt = options.tickSuffixDflt;

var showAttrDflt = getShowAttrDflt(containerIn);

var tickPrefix = coerce('tickprefix');
if(tickPrefix) coerce('showtickprefix', showAttrDflt);

var tickSuffix = coerce('ticksuffix', tickSuffixDflt);
if(tickSuffix) coerce('showticksuffix', showAttrDflt);
};
29 changes: 29 additions & 0 deletions src/plots/cartesian/show_dflt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

/*
* Attributes 'showexponent', 'showtickprefix' and 'showticksuffix'
* share values.
*
* If only 1 attribute is set,
* the remaining attributes inherit that value.
*
* If 2 attributes are set to the same value,
* the remaining attribute inherits that value.
*
* If 2 attributes are set to different values,
* the remaining is set to its dflt value.
*
*/
module.exports = function getShowAttrDflt(containerIn) {
var showAttrsAll = ['showexponent', 'showtickprefix', 'showticksuffix'];
var showAttrs = showAttrsAll.filter(function(a) {
return containerIn[a] !== undefined;
});
var sameVal = function(a) {
return containerIn[a] === containerIn[showAttrs[0]];
};

if(showAttrs.every(sameVal) || showAttrs.length === 1) {
return containerIn[showAttrs[0]];
}
};
54 changes: 4 additions & 50 deletions src/plots/cartesian/tick_label_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,11 @@
var Lib = require('../../lib');
var contrast = require('../../components/color').contrast;
var layoutAttributes = require('./layout_attributes');
var getShowAttrDflt = require('./show_dflt');
var handleArrayContainerDefaults = require('../array_container_defaults');

module.exports = function handleTickLabelDefaults(containerIn, containerOut, coerce, axType, options, config) {
if(!config || config.pass === 1) {
handlePrefixSuffix(containerIn, containerOut, coerce, axType, options);
}

if(!config || config.pass === 2) {
handleOtherDefaults(containerIn, containerOut, coerce, axType, options);
}
};

function handlePrefixSuffix(containerIn, containerOut, coerce, axType, options) {
var showAttrDflt = getShowAttrDflt(containerIn);

var tickPrefix = coerce('tickprefix');
if(tickPrefix) coerce('showtickprefix', showAttrDflt);

var tickSuffix = coerce('ticksuffix', options.tickSuffixDflt);
if(tickSuffix) coerce('showticksuffix', showAttrDflt);
}

function handleOtherDefaults(containerIn, containerOut, coerce, axType, options) {
module.exports = function handleTickLabelDefaults(containerIn, containerOut, coerce, axType, options) {
if(!options) options = {};
var showAttrDflt = getShowAttrDflt(containerIn);

var showTickLabels = coerce('showticklabels');
Expand Down Expand Up @@ -67,35 +49,7 @@ function handleOtherDefaults(containerIn, containerOut, coerce, axType, options)
}
}
}
}

/*
* Attributes 'showexponent', 'showtickprefix' and 'showticksuffix'
* share values.
*
* If only 1 attribute is set,
* the remaining attributes inherit that value.
*
* If 2 attributes are set to the same value,
* the remaining attribute inherits that value.
*
* If 2 attributes are set to different values,
* the remaining is set to its dflt value.
*
*/
function getShowAttrDflt(containerIn) {
var showAttrsAll = ['showexponent', 'showtickprefix', 'showticksuffix'];
var showAttrs = showAttrsAll.filter(function(a) {
return containerIn[a] !== undefined;
});
var sameVal = function(a) {
return containerIn[a] === containerIn[showAttrs[0]];
};

if(showAttrs.every(sameVal) || showAttrs.length === 1) {
return containerIn[showAttrs[0]];
}
}
};

function tickformatstopDefaults(valueIn, valueOut) {
function coerce(attr, dflt) {
Expand Down
4 changes: 3 additions & 1 deletion src/plots/polar/layout_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var getSubplotData = require('../get_data').getSubplotData;
var handleTickValueDefaults = require('../cartesian/tick_value_defaults');
var handleTickMarkDefaults = require('../cartesian/tick_mark_defaults');
var handleTickLabelDefaults = require('../cartesian/tick_label_defaults');
var handlePrefixSuffixDefaults = require('../cartesian/prefix_suffix_defaults');
var handleCategoryOrderDefaults = require('../cartesian/category_order_defaults');
var handleLineGridDefaults = require('../cartesian/line_grid_defaults');
var autoType = require('../cartesian/axis_autotype');
Expand Down Expand Up @@ -138,9 +139,10 @@ function handleDefaults(contIn, contOut, coerce, opts) {

if(visible) {
handleTickValueDefaults(axIn, axOut, coerceAxis, axOut.type);
handleTickLabelDefaults(axIn, axOut, coerceAxis, axOut.type, {
handlePrefixSuffixDefaults(axIn, axOut, coerceAxis, axOut.type, {
tickSuffixDflt: axOut.thetaunit === 'degrees' ? '°' : undefined
});
handleTickLabelDefaults(axIn, axOut, coerceAxis, axOut.type);
handleTickMarkDefaults(axIn, axOut, coerceAxis, {outerTicks: true});

var showTickLabels = coerceAxis('showticklabels');
Expand Down
4 changes: 3 additions & 1 deletion src/plots/ternary/layout_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var Lib = require('../../lib');

var handleSubplotDefaults = require('../subplot_defaults');
var handleTickLabelDefaults = require('../cartesian/tick_label_defaults');
var handlePrefixSuffixDefaults = require('../cartesian/prefix_suffix_defaults');
var handleTickMarkDefaults = require('../cartesian/tick_mark_defaults');
var handleTickValueDefaults = require('../cartesian/tick_value_defaults');
var handleLineGridDefaults = require('../cartesian/line_grid_defaults');
Expand Down Expand Up @@ -90,7 +91,8 @@ function handleAxisDefaults(containerIn, containerOut, options, ternaryLayoutOut
coerce('min');

handleTickValueDefaults(containerIn, containerOut, coerce, 'linear');
handleTickLabelDefaults(containerIn, containerOut, coerce, 'linear', {});
handlePrefixSuffixDefaults(containerIn, containerOut, coerce, 'linear');
handleTickLabelDefaults(containerIn, containerOut, coerce, 'linear');
handleTickMarkDefaults(containerIn, containerOut, coerce,
{ outerTicks: true });

Expand Down
2 changes: 2 additions & 0 deletions src/traces/carpet/axis_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var Registry = require('../../registry');
var Lib = require('../../lib');
var handleTickValueDefaults = require('../../plots/cartesian/tick_value_defaults');
var handleTickLabelDefaults = require('../../plots/cartesian/tick_label_defaults');
var handlePrefixSuffixDefaults = require('../../plots/cartesian/prefix_suffix_defaults');
var handleCategoryOrderDefaults = require('../../plots/cartesian/category_order_defaults');
var setConvert = require('../../plots/cartesian/set_convert');
var autoType = require('../../plots/cartesian/axis_autotype');
Expand Down Expand Up @@ -128,6 +129,7 @@ module.exports = function handleAxisDefaults(containerIn, containerOut, options)
coerce('fixedrange');

handleTickValueDefaults(containerIn, containerOut, coerce, axType);
handlePrefixSuffixDefaults(containerIn, containerOut, coerce, axType, options);
handleTickLabelDefaults(containerIn, containerOut, coerce, axType, options);
handleCategoryOrderDefaults(containerIn, containerOut, coerce, {
data: options.data,
Expand Down
2 changes: 2 additions & 0 deletions src/traces/indicator/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var cn = require('./constants.js');
var handleTickValueDefaults = require('../../plots/cartesian/tick_value_defaults');
var handleTickMarkDefaults = require('../../plots/cartesian/tick_mark_defaults');
var handleTickLabelDefaults = require('../../plots/cartesian/tick_label_defaults');
var handlePrefixSuffixDefaults = require('../../plots/cartesian/prefix_suffix_defaults');

function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
function coerce(attr, dflt) {
Expand Down Expand Up @@ -128,6 +129,7 @@ function supplyDefaults(traceIn, traceOut, defaultColor, layout) {

var opts = {outerTicks: true};
handleTickValueDefaults(axisIn, axisOut, coerceGaugeAxis, 'linear');
handlePrefixSuffixDefaults(axisIn, axisOut, coerceGaugeAxis, 'linear', opts);
handleTickLabelDefaults(axisIn, axisOut, coerceGaugeAxis, 'linear', opts);
handleTickMarkDefaults(axisIn, axisOut, coerceGaugeAxis, opts);
} else {
Expand Down