Skip to content

Commit e7fb35b

Browse files
committed
Standardize axis titles
1 parent 41d4e62 commit e7fb35b

File tree

5 files changed

+40
-15
lines changed

5 files changed

+40
-15
lines changed

src/components/fields/AxisCreator.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@ import Field from './Field';
33
import Info from './Info';
44
import PropTypes from 'prop-types';
55
import React, {Component, Fragment} from 'react';
6-
import {connectToContainer, localize, traceTypeToAxisType} from 'lib';
76
import {EDITOR_ACTIONS} from 'lib/constants';
87
import {PlusIcon} from 'plotly-icons';
8+
import {
9+
connectToContainer,
10+
localize,
11+
traceTypeToAxisType,
12+
getAxisTitle,
13+
axisIdToAxisName,
14+
} from 'lib';
915

1016
export class UnconnectedAxisCreator extends Component {
1117
constructor(props, context) {
@@ -58,7 +64,7 @@ export class UnconnectedAxisCreator extends Component {
5864
},
5965
}}
6066
options={context.fullLayout._subplots[subplot].map(subplot => ({
61-
label: subplot,
67+
label: getAxisTitle(context.fullLayout[axisIdToAxisName(subplot)]),
6268
value: subplot,
6369
}))}
6470
/>

src/components/fields/derived.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import {UnconnectedFlaglist} from './Flaglist';
44
import {UnconnectedNumeric} from './Numeric';
55
import {UnconnectedRadio} from './Radio';
66
import {
7-
capitalize,
87
connectLayoutToPlot,
98
connectToContainer,
109
getAllAxes,
10+
getAxisTitle,
11+
axisIdToAxisName,
1112
supplyLayoutPlotProps,
12-
striptags,
1313
} from 'lib';
1414

1515
export const AxisAnchorDropdown = connectToContainer(UnconnectedDropdown, {
@@ -19,11 +19,17 @@ export const AxisAnchorDropdown = connectToContainer(UnconnectedDropdown, {
1919
plotProps.options = [{label: 'x', value: 'x'}];
2020
if (plotProps.fullContainer.subplot.includes('xaxis')) {
2121
options = context.fullLayout._subplots.yaxis.map(axis => {
22-
return {label: capitalize(axis), value: axis};
22+
return {
23+
label: getAxisTitle(context.fullLayout[axisIdToAxisName(axis)]),
24+
value: axis,
25+
};
2326
});
2427
} else if (plotProps.fullContainer.subplot.includes('yaxis')) {
2528
options = context.fullLayout._subplots.xaxis.map(axis => {
26-
return {label: capitalize(axis), value: axis};
29+
return {
30+
label: getAxisTitle(context.fullLayout[axisIdToAxisName(axis)]),
31+
value: axis,
32+
};
2733
});
2834
}
2935
options.push({label: _('Free'), value: 'free'});
@@ -293,8 +299,7 @@ function computeAxesRefOptions(axes, propsAttr) {
293299
ax._id.charAt(0) === propsAttr.charAt(0) ||
294300
ax._id.charAt(0) === propsAttr.charAt(1)
295301
) {
296-
// checking user data for title avoids default "Click to enter axis title"
297-
const label = striptags(ax._input.title || ax._id);
302+
const label = getAxisTitle(ax);
298303
options.push({label, value: ax._id});
299304
}
300305
}

src/lib/connectAxesToLayout.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
getAllAxes,
88
getDisplayName,
99
localize,
10-
striptags,
10+
getAxisTitle,
1111
} from '../lib';
1212

1313
function computeAxesOptions(axes, _) {
@@ -26,11 +26,7 @@ function computeAxesOptions(axes, _) {
2626
label,
2727
value,
2828
axisGroup: ax.axisGroup,
29-
title: striptags(
30-
ax._input.title
31-
? `${label} Axis: ${ax._input.title}`
32-
: capitalize(ax._id)
33-
),
29+
title: getAxisTitle(ax),
3430
};
3531
}
3632

src/lib/getAllAxes.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {TRACE_TO_AXIS} from 'lib/constants';
2+
import {capitalize, striptags} from 'lib';
23

34
export default function getAllAxes(fullLayout) {
45
const axes = [];
@@ -61,3 +62,14 @@ export function traceTypeToAxisType(traceType) {
6162

6263
throw new Error(`Sorry, could not find ${traceType} in any category.`);
6364
}
65+
66+
export function axisIdToAxisName(id) {
67+
return id.charAt(0) + 'axis' + id.slice(1);
68+
}
69+
70+
export function getAxisTitle(axis) {
71+
const axisType = capitalize(axis._name.split('axis')[0]);
72+
return axis._input && axis._input.title
73+
? striptags(`${axisType} Axis: ${axis._input.title}`)
74+
: capitalize(axis._id);
75+
}

src/lib/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import connectToContainer, {
1010
import connectTraceToPlot from './connectTraceToPlot';
1111
import dereference from './dereference';
1212
import findFullTraceIndex from './findFullTraceIndex';
13-
import getAllAxes, {traceTypeToAxisType} from './getAllAxes';
13+
import getAllAxes, {
14+
axisIdToAxisName,
15+
traceTypeToAxisType,
16+
getAxisTitle,
17+
} from './getAllAxes';
1418
import localize, {localizeString} from './localize';
1519
import tinyColor from 'tinycolor2';
1620
import unpackPlotProps from './unpackPlotProps';
@@ -49,6 +53,7 @@ function renderTraceIcon(trace) {
4953
}
5054

5155
export {
56+
axisIdToAxisName,
5257
bem,
5358
capitalize,
5459
clamp,
@@ -64,6 +69,7 @@ export {
6469
dereference,
6570
findFullTraceIndex,
6671
getAllAxes,
72+
getAxisTitle,
6773
getDisplayName,
6874
getLayoutContext,
6975
isPlainObject,

0 commit comments

Comments
 (0)