Skip to content
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

Add labelalias to various axes #6481

Merged
merged 17 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions draftlogs/6481_add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Add `labelalias` to various axes namely cartesian, gl3d, polar, smith, ternary, carpet, indicator and colorbar [[#6481](https://github.com/plotly/plotly.js/pull/6481)]
1 change: 1 addition & 0 deletions src/components/colorbar/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ module.exports = overrideAll({
tickcolor: axesAttrs.tickcolor,
ticklabelstep: axesAttrs.ticklabelstep,
showticklabels: axesAttrs.showticklabels,
labelalias: axesAttrs.labelalias,
tickfont: fontAttrs({
description: 'Sets the color bar\'s tick label font'
}),
Expand Down
1 change: 1 addition & 0 deletions src/components/colorbar/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,7 @@ function mockColorBarAxis(gd, opts, zrange) {
tickwidth: opts.tickwidth,
tickcolor: opts.tickcolor,
showticklabels: opts.showticklabels,
labelalias: opts.labelalias,
ticklabelposition: opts.ticklabelposition,
ticklabeloverflow: opts.ticklabeloverflow,
ticklabelstep: opts.ticklabelstep,
Expand Down
5 changes: 5 additions & 0 deletions src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -1660,6 +1660,11 @@ axes.tickText = function(ax, x, hover, noSuffixPrefix) {
if(ax.ticksuffix && !isHidden(ax.showticksuffix)) out.text += ax.ticksuffix;
}

if(ax.labelalias && ax.labelalias.hasOwnProperty(out.text)) {
var t = ax.labelalias[out.text];
if(typeof t === 'string') out.text = t;
}

// Setup ticks and grid lines boundaries
// at 1/2 a 'category' to the left/bottom
if(ax.tickson === 'boundaries' || ax.showdividers) {
Expand Down
13 changes: 13 additions & 0 deletions src/plots/cartesian/layout_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,19 @@ module.exports = {
editType: 'ticks',
description: 'Determines whether or not the tick labels are drawn.'
},
labelalias: {
valType: 'any',
dflt: false,
editType: 'ticks',
description: [
'Replacement text for specific tick or hover labels.',
'For example using {US: \'USA\', CA: \'Canada\'} changes US to USA',
'and CA to Canada. The labels we would have shown must match',
'the keys exactly, after adding any tickprefix or ticksuffix.',
'labelalias can be used with any axis type, and both keys (if needed)',
'and values (if desired) can include html-like tags or MathJax.'
].join(' ')
},
automargin: {
valType: 'flaglist',
flags: ['height', 'width', 'left', 'right', 'top', 'bottom'],
Expand Down
4 changes: 4 additions & 0 deletions src/plots/cartesian/tick_label_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ var handleArrayContainerDefaults = require('../array_container_defaults');

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

var labelalias = coerce('labelalias');
if(!Lib.isPlainObject(labelalias)) delete containerOut.labelalias;

var showAttrDflt = getShowAttrDflt(containerIn);

var showTickLabels = coerce('showticklabels');
Expand Down
1 change: 1 addition & 0 deletions src/plots/gl3d/layout/axis_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ module.exports = overrideAll({
tickwidth: axesAttrs.tickwidth,
tickcolor: axesAttrs.tickcolor,
showticklabels: axesAttrs.showticklabels,
labelalias: axesAttrs.labelalias,
tickfont: axesAttrs.tickfont,
tickangle: axesAttrs.tickangle,
tickprefix: axesAttrs.tickprefix,
Expand Down
1 change: 1 addition & 0 deletions src/plots/polar/layout_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var axisTickAttrs = overrideAll({
tickcolor: axesAttrs.tickcolor,
ticklabelstep: axesAttrs.ticklabelstep,
showticklabels: axesAttrs.showticklabels,
labelalias: axesAttrs.labelalias,
showtickprefix: axesAttrs.showtickprefix,
tickprefix: axesAttrs.tickprefix,
showticksuffix: axesAttrs.showticksuffix,
Expand Down
1 change: 1 addition & 0 deletions src/plots/smith/layout_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var axisTickAttrs = overrideAll({
tickwidth: extendFlat({}, axesAttrs.tickwidth, {dflt: 2}),
tickcolor: axesAttrs.tickcolor,
showticklabels: axesAttrs.showticklabels,
labelalias: axesAttrs.labelalias,
showtickprefix: axesAttrs.showtickprefix,
tickprefix: axesAttrs.tickprefix,
showticksuffix: axesAttrs.showticksuffix,
Expand Down
1 change: 1 addition & 0 deletions src/plots/ternary/layout_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var ternaryAxesAttrs = {
tickcolor: axesAttrs.tickcolor,
ticklabelstep: axesAttrs.ticklabelstep,
showticklabels: axesAttrs.showticklabels,
labelalias: axesAttrs.labelalias,
showtickprefix: axesAttrs.showtickprefix,
tickprefix: axesAttrs.tickprefix,
showticksuffix: axesAttrs.showticksuffix,
Expand Down
1 change: 1 addition & 0 deletions src/traces/carpet/axis_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ module.exports = {
'the high side, both, or neither side of the axis.'
].join(' ')
},
labelalias: extendFlat({}, axesAttrs.labelalias, {editType: 'calc'}),
tickfont: fontAttrs({
editType: 'calc',
description: 'Sets the tick font.'
Expand Down
1 change: 1 addition & 0 deletions src/traces/indicator/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ module.exports = {
tickcolor: axesAttrs.tickcolor,
ticklabelstep: axesAttrs.ticklabelstep,
showticklabels: axesAttrs.showticklabels,
labelalias: axesAttrs.labelalias,
tickfont: fontAttrs({
description: 'Sets the color bar\'s tick label font'
}),
Expand Down
Binary file modified test/image/baselines/carpet_grid_dash.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/grid_subplot_types.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/indicator_bullet.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/indicator_format_extremes.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/mathjax3___ternary-mathjax.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/smith_blank.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/ternary-mathjax.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/image/mocks/carpet_grid_dash.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"minorgriddash": "dash"
},
"baxis": {
"labelalias": { "0": "ZERO<sup>2</sup>" },
"minorgridcount": 3,
"gridcolor": "black",
"minorgridcolor": "red",
Expand Down
23 changes: 14 additions & 9 deletions test/image/mocks/grid_subplot_types.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,23 @@
"domain": {"row": 2, "column": 0}
},
{"type": "scattergeo", "lon": [0, -75], "lat": [0, 45], "marker": {"size": [20, 30]}},
{"type": "heatmap", "z": [[1, 2], [4, 1]], "showscale": false, "xaxis": "x3", "yaxis": "y3"}
{"type": "heatmap", "z": [[1, 2], [4, 1]], "showscale": true, "xaxis": "x3", "yaxis": "y3", "colorbar": {
"yanchor": "bottom",
"y": 0,
"len": 0.3,
"labelalias": {"2": "TWO<sup>2</sup>"}
}}
],
"layout": {
"xaxis": {"title": {"text": "x"}},
"xaxis2": {"title": {"text": "x2"}},
"yaxis": {"title": {"text": "y"}},
"yaxis2": {"title": {"text": "y2"}},
"xaxis3": {"title": {"text": "x3"}},
"yaxis3": {"title": {"text": "y3"}},
"xaxis": {"title": {"text": "x"}, "labelalias": {"0": "ZERO<sup>2</sup>"}},
"xaxis2": {"title": {"text": "x2"}, "labelalias": {"0": "ZERO<sup>2</sup>"}},
"yaxis": {"title": {"text": "y"}, "labelalias": {"0": "ZERO<sup>2</sup>"}},
"yaxis2": {"title": {"text": "y2"}, "labelalias": {"0": "ZERO<sup>2</sup>"}},
"xaxis3": {"title": {"text": "x3"}, "labelalias": {"0": "ZERO<sup>2</sup>"}},
"yaxis3": {"title": {"text": "y3"}, "labelalias": {"0": "ZERO<sup>2</sup>"}},
"grid": {"rows": 3, "columns": 3, "xgap": 0.3, "ygap": 0.3},
"scene": {"domain": {"row": 1, "column": 1}},
"ternary": {"domain": {"row": 1, "column": 2}},
"scene": {"domain": {"row": 1, "column": 1}, "zaxis": {"labelalias": {"2": "TWO<sup>2</sup>"}}},
"ternary": {"domain": {"row": 1, "column": 2}, "caxis": {"labelalias": {"1": "ONE"}}},
"geo": {"domain": {"row": 2, "column": 1}},
"width": 700,
"height": 700
Expand Down
3 changes: 3 additions & 0 deletions test/image/mocks/indicator_bullet.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"gauge": {
"shape": "bullet",
"axis": {
"labelalias": {
"0": "ZERO<sup>2</sup>"
},
"range": [null, 300]
},
"threshold": {
Expand Down
7 changes: 7 additions & 0 deletions test/image/mocks/indicator_format_extremes.json
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,13 @@
{
"type": "indicator",
"mode": "number+delta+gauge",
"gauge": {
"axis": {
"labelalias": {
"1": "ONE"
}
}
},
"title": {
"text": "null"
},
Expand Down
3 changes: 3 additions & 0 deletions test/image/mocks/polar_blank.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
"x": [0, 0.46],
"y": [0.56, 1]
},
"angularaxis": {
"labelalias": { "0": "ZERO<sup>2</sup>" }
archmoj marked this conversation as resolved.
Show resolved Hide resolved
},
"radialaxis": {
"title": {"text": "blank"},
"angle": 180
Expand Down
3 changes: 3 additions & 0 deletions test/image/mocks/smith_blank.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
"x": [0, 0.46],
"y": [0.56, 1]
},
"imaginaryaxis": {
"labelalias": { "0": "ZERO<sup>2</sup>" }
},
"realaxis": {
"side": "top"
}
Expand Down
10 changes: 9 additions & 1 deletion test/image/mocks/ternary-mathjax.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@
"text": "$C^2$"
},
"showline": true,
"showgrid": true
"showgrid": true,
"labelalias": {
"0": "$0^2$",
"20": "$20^2$",
"40": "$40^2$",
"60": "$60^2$",
"80": "$80^2$",
"100": "$100^2$"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if one could possibly add $ in prefix and suffix to render every tick by mathjax?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why you would do that, but maybe it would work? Not particularly related to this PR though...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To have dynamic mathjax labels when you zoom in & out.
Illustrated in c1d127e.

}
}
},
"annotations": [
Expand Down