Skip to content

Commit fce716e

Browse files
authored
Merge pull request #846 from plotly/fix-meta-titles
Fix meta titles
2 parents 8d0c41f + 8b63f28 commit fce716e

File tree

6 files changed

+59
-31
lines changed

6 files changed

+59
-31
lines changed

src/components/containers/AnnotationAccordion.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import {LayoutPanel} from './derived';
33
import {PanelMessage} from './PanelEmpty';
44
import PropTypes from 'prop-types';
55
import React, {Component} from 'react';
6-
import {connectAnnotationToLayout} from 'lib';
7-
import {templateString} from 'plotly.js/src/lib';
6+
import {connectAnnotationToLayout, getParsedTemplateString} from 'lib';
87

98
const AnnotationFold = connectAnnotationToLayout(PlotlyFold);
109

@@ -19,12 +18,11 @@ class AnnotationAccordion extends Component {
1918
const content =
2019
annotations.length &&
2120
annotations.map((ann, i) => {
22-
const textPostTemplate = templateString(ann.text, {meta});
2321
return (
2422
<AnnotationFold
2523
key={i}
2624
annotationIndex={i}
27-
name={textPostTemplate === '' ? ann.text : textPostTemplate}
25+
name={getParsedTemplateString(ann.text, meta)}
2826
canDelete={canAdd}
2927
>
3028
{children}

src/components/containers/RangeSelectorAccordion.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import PlotlyFold from './PlotlyFold';
22
import PlotlyPanel from './PlotlyPanel';
33
import PropTypes from 'prop-types';
44
import React, {Component} from 'react';
5-
import {connectRangeSelectorToAxis} from 'lib';
5+
import {connectRangeSelectorToAxis, getParsedTemplateString} from 'lib';
66

77
const RangeSelectorFold = connectRangeSelectorToAxis(PlotlyFold);
88

@@ -23,13 +23,19 @@ class RangeSelectorAccordion extends Component {
2323
rangeselector: {buttons = []},
2424
},
2525
localize: _,
26+
layout: meta,
2627
} = this.context;
2728
const {children} = this.props;
2829

2930
const content =
3031
buttons.length &&
3132
buttons.map((btn, i) => (
32-
<RangeSelectorFold key={i} rangeselectorIndex={i} name={btn.label} canDelete={true}>
33+
<RangeSelectorFold
34+
key={i}
35+
rangeselectorIndex={i}
36+
name={getParsedTemplateString(btn.label, meta)}
37+
canDelete={true}
38+
>
3339
{children}
3440
</RangeSelectorFold>
3541
));
@@ -57,6 +63,7 @@ class RangeSelectorAccordion extends Component {
5763
RangeSelectorAccordion.contextTypes = {
5864
fullContainer: PropTypes.object,
5965
localize: PropTypes.func,
66+
layout: PropTypes.object,
6067
};
6168

6269
RangeSelectorAccordion.propTypes = {

src/components/fields/AxesCreator.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ import React, {Component} from 'react';
55
import {EDITOR_ACTIONS} from 'lib/constants';
66
import Button from '../widgets/Button';
77
import {PlusIcon} from 'plotly-icons';
8-
import {connectToContainer, traceTypeToAxisType, getAxisTitle, axisIdToAxisName} from 'lib';
8+
import {
9+
connectToContainer,
10+
traceTypeToAxisType,
11+
getAxisTitle,
12+
axisIdToAxisName,
13+
getParsedTemplateString,
14+
} from 'lib';
915
import {PlotlySection} from 'components';
1016

1117
class UnconnectedAxisCreator extends Component {
@@ -128,7 +134,10 @@ class UnconnectedAxesCreator extends Component {
128134

129135
function getOptions(axisType) {
130136
return fullLayout._subplots[axisType].map(axisId => ({
131-
label: getAxisTitle(fullLayout[axisIdToAxisName(axisId)]),
137+
label: getParsedTemplateString(
138+
getAxisTitle(fullLayout[axisIdToAxisName(axisId)]),
139+
fullLayout.meta
140+
),
132141
value: axisId,
133142
}));
134143
}

src/components/fields/TextEditor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class UnconnectedTextEditor extends Component {
3232
if (index) {
3333
const adjustedIndex = parseInt(index[3], 10) - 1;
3434
if (!isNaN(adjustedIndex)) {
35-
return `%{meta[${adjustedIndex}]}`;
35+
return `%{meta[${adjustedIndex < 0 ? 0 : adjustedIndex}]}`;
3636
}
3737
}
3838
return match;

src/lib/connectTraceToPlot.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
renderTraceIcon,
88
traceTypeToAxisType,
99
getFullTrace,
10+
getParsedTemplateString,
1011
} from '../lib';
1112
import {deepCopyPublic, setMultiValuedContainer} from './multiValues';
1213
import {EDITOR_ACTIONS, SUBPLOT_TO_ATTR} from 'lib/constants';
@@ -27,7 +28,7 @@ export default function connectTraceToPlot(WrappedComponent) {
2728

2829
setLocals(props, context) {
2930
const {traceIndexes} = props;
30-
const {data, fullData, plotly} = context;
31+
const {data, fullData, plotly, layout: meta} = context;
3132

3233
const trace = data[traceIndexes[0]];
3334
const fullTrace = getFullTrace(props, context);
@@ -68,7 +69,7 @@ export default function connectTraceToPlot(WrappedComponent) {
6869

6970
if (trace && fullTrace) {
7071
this.icon = renderTraceIcon(plotlyTraceToCustomTrace(trace));
71-
this.name = fullTrace.name;
72+
this.name = getParsedTemplateString(fullTrace.name, meta);
7273
}
7374
}
7475

@@ -194,6 +195,7 @@ export default function connectTraceToPlot(WrappedComponent) {
194195
data: PropTypes.array,
195196
plotly: PropTypes.object,
196197
onUpdate: PropTypes.func,
198+
layout: PropTypes.object,
197199
};
198200

199201
TraceConnectedComponent.childContextTypes = {

src/lib/index.js

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import * as PlotlyIcons from 'plotly-icons';
3030
import striptags from './striptags';
3131
import {capitalize, lowerCase, upperCase, removeNonWord, camelCase, pascalCase} from './strings';
3232
import {getColorscale} from 'react-colorscales';
33+
import {templateString} from 'plotly.js/src/lib';
3334

3435
const TOO_LIGHT_FACTOR = 0.8;
3536

@@ -202,51 +203,62 @@ function getFullTrace(props, context) {
202203
return fullTrace;
203204
}
204205

206+
function getParsedTemplateString(originalString, meta) {
207+
let text = originalString;
208+
209+
if (originalString && meta && meta.length) {
210+
text = templateString(originalString, {meta});
211+
}
212+
213+
return text === '' && originalString ? originalString : text;
214+
}
215+
205216
export {
206217
adjustColorscale,
207218
axisIdToAxisName,
208219
bem,
209-
capitalize,
210-
lowerCase,
211-
upperCase,
212-
removeNonWord,
213220
camelCase,
214-
pascalCase,
221+
capitalize,
215222
clamp,
223+
computeTraceOptionsFromSchema,
224+
connectAggregationToTransform,
225+
connectAnnotationToLayout,
226+
connectAxesToLayout,
216227
connectCartesianSubplotToLayout,
228+
connectImageToLayout,
229+
connectLayoutToPlot,
217230
connectNonCartesianSubplotToLayout,
218-
connectAnnotationToLayout,
231+
connectRangeSelectorToAxis,
219232
connectShapeToLayout,
220233
connectSliderToLayout,
221-
connectUpdateMenuToLayout,
222-
connectImageToLayout,
223-
connectAxesToLayout,
224-
connectLayoutToPlot,
225234
connectToContainer,
226-
connectRangeSelectorToAxis,
227-
connectTransformToTrace,
228-
connectAggregationToTransform,
229235
connectTraceToPlot,
236+
connectTransformToTrace,
237+
connectUpdateMenuToLayout,
230238
containerConnectedContextTypes,
231-
computeTraceOptionsFromSchema,
232-
traceTypeToPlotlyInitFigure,
233239
dereference,
234240
getAllAxes,
235241
getAxisTitle,
236-
getSubplotTitle,
237242
getDisplayName,
243+
getFullTrace,
244+
getSubplotTitle,
238245
isPlainObject,
239246
localize,
240247
localizeString,
248+
lowerCase,
241249
maybeAdjustSrc,
242250
maybeTransposeData,
251+
getParsedTemplateString,
252+
pascalCase,
243253
plotlyTraceToCustomTrace,
254+
removeNonWord,
244255
renderTraceIcon,
245-
unpackPlotProps,
246-
walkObject,
247-
tooLight,
248256
striptags,
257+
tooLight,
249258
traceTypeToAxisType,
259+
traceTypeToPlotlyInitFigure,
250260
transpose,
251-
getFullTrace,
261+
unpackPlotProps,
262+
upperCase,
263+
walkObject,
252264
};

0 commit comments

Comments
 (0)