Skip to content

Commit 66b2f40

Browse files
authored
Merge pull request #3865 from plotly/trace-meta
Trace meta text
2 parents 425900e + 1cf8a85 commit 66b2f40

File tree

16 files changed

+251
-39
lines changed

16 files changed

+251
-39
lines changed

src/components/annotations/draw.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
212212

213213
var font = options.font;
214214

215-
var text = fullLayout.meta ?
216-
Lib.templateString(options.text, {meta: fullLayout.meta}) :
215+
var text = fullLayout._meta ?
216+
Lib.templateString(options.text, fullLayout._meta) :
217217
options.text;
218218

219219
var annText = annTextGroupInner.append('text')

src/components/colorbar/draw.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ function makeColorBarData(gd) {
140140
opts._id = 'cb' + trace.uid + (allowsMultiplotCbs && contName ? '-' + contName : '');
141141
opts._traceIndex = trace.index;
142142
opts._propPrefix = (contName ? contName + '.' : '') + 'colorbar.';
143+
opts._meta = trace._meta;
143144
calcOpts();
144145
out.push(opts);
145146
}
@@ -156,6 +157,7 @@ function makeColorBarData(gd) {
156157
opts = initOpts(cont.colorbar);
157158
opts._id = 'cb' + k;
158159
opts._propPrefix = k + '.colorbar.';
160+
opts._meta = fullLayout._meta;
159161

160162
cbOpt = {min: 'cmin', max: 'cmax'};
161163
if(colorAxOpts[0] !== 'heatmap') {
@@ -281,6 +283,7 @@ function drawColorBar(g, opts, gd) {
281283
propContainer: ax,
282284
propName: opts._propPrefix + 'title',
283285
traceIndex: opts._traceIndex,
286+
_meta: opts._meta,
284287
placeholder: fullLayout._dfltTitle.colorbar,
285288
containerGroup: g.select('.' + cn.cbtitle)
286289
};

src/components/fx/hover.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -887,8 +887,8 @@ function createHoverText(hoverData, opts, gd) {
887887
if(d.nameOverride !== undefined) d.name = d.nameOverride;
888888

889889
if(d.name) {
890-
if(fullLayout.meta) {
891-
d.name = Lib.templateString(d.name, {meta: fullLayout.meta});
890+
if(d.trace._meta) {
891+
d.name = Lib.templateString(d.name, d.trace._meta);
892892
}
893893
name = plainText(d.name, d.nameLength);
894894
}
@@ -925,7 +925,7 @@ function createHoverText(hoverData, opts, gd) {
925925
}
926926

927927
// hovertemplate
928-
var d3locale = gd._fullLayout._d3locale;
928+
var d3locale = fullLayout._d3locale;
929929
var hovertemplate = d.hovertemplate || false;
930930
var hovertemplateLabels = d.hovertemplateLabels || d;
931931
var eventData = d.eventData[0] || {};
@@ -935,7 +935,7 @@ function createHoverText(hoverData, opts, gd) {
935935
hovertemplateLabels,
936936
d3locale,
937937
eventData,
938-
{meta: fullLayout.meta}
938+
d.trace._meta
939939
);
940940

941941
text = text.replace(EXTRA_STRING_REGEX, function(match, extra) {

src/components/legend/draw.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,8 @@ function drawTexts(g, gd, maxLength) {
404404
var isEditable = gd._context.edits.legendText && !isPie;
405405

406406
var name = isPie ? legendItem.label : trace.name;
407-
if(fullLayout.meta) {
408-
name = Lib.templateString(name, {meta: fullLayout.meta});
407+
if(trace._meta) {
408+
name = Lib.templateString(name, trace._meta);
409409
}
410410

411411
var textEl = Lib.ensureSingle(g, 'text', 'legendtext');

src/components/rangeselector/draw.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,14 @@ function drawButtonText(button, selectorLayout, d, gd) {
149149
});
150150

151151
text.call(Drawing.font, selectorLayout.font)
152-
.text(getLabel(d, gd._fullLayout.meta))
152+
.text(getLabel(d, gd._fullLayout._meta))
153153
.call(textLayout);
154154
}
155155

156-
function getLabel(opts, meta) {
156+
function getLabel(opts, _meta) {
157157
if(opts.label) {
158-
return meta ?
159-
Lib.templateString(opts.label, {meta: meta}) :
158+
return _meta ?
159+
Lib.templateString(opts.label, _meta) :
160160
opts.label;
161161
}
162162

src/components/sliders/draw.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,8 @@ function drawCurrentValue(sliderGroup, sliderOpts, valueOverride) {
315315
str += valueOverride;
316316
} else {
317317
var curVal = sliderOpts.steps[sliderOpts.active].label;
318-
var meta = sliderOpts._gd._fullLayout.meta;
319-
if(meta) {
320-
curVal = Lib.templateString(curVal, {meta: meta});
321-
}
318+
var _meta = sliderOpts._gd._fullLayout._meta;
319+
if(_meta) curVal = Lib.templateString(curVal, _meta);
322320
str += curVal;
323321
}
324322

@@ -367,10 +365,8 @@ function drawLabel(item, data, sliderOpts) {
367365
});
368366

369367
var tx = data.step.label;
370-
var meta = sliderOpts._gd._fullLayout.meta;
371-
if(meta) {
372-
tx = Lib.templateString(tx, {meta: meta});
373-
}
368+
var _meta = sliderOpts._gd._fullLayout._meta;
369+
if(_meta) tx = Lib.templateString(tx, _meta);
374370

375371
text.call(Drawing.font, sliderOpts.font)
376372
.text(tx)

src/components/titles/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ var numStripRE = / [XY][0-9]* /;
5353
* offset - shift up/down in the rotated frame (unused?)
5454
* containerGroup - if an svg <g> element already exists to hold this
5555
* title, include here. Otherwise it will go in fullLayout._infolayer
56+
* _meta {object (optional} - meta key-value to for title with
57+
* Lib.templateString, default to fullLayout._meta, if not provided
5658
*
5759
* @return {selection} d3 selection of title container group
5860
*/
@@ -97,8 +99,10 @@ function draw(gd, titleClass, options) {
9799
if(!editable) txt = '';
98100
}
99101

100-
if(fullLayout.meta) {
101-
txt = Lib.templateString(txt, {meta: fullLayout.meta});
102+
if(options._meta) {
103+
txt = Lib.templateString(txt, options._meta);
104+
} else if(fullLayout._meta) {
105+
txt = Lib.templateString(txt, fullLayout._meta);
102106
}
103107

104108
var elShouldExist = txt || editable;

src/components/updatemenus/draw.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -438,10 +438,8 @@ function drawItemText(item, menuOpts, itemOpts, gd) {
438438
});
439439

440440
var tx = itemOpts.label;
441-
var meta = gd._fullLayout.meta;
442-
if(meta) {
443-
tx = Lib.templateString(tx, {meta: meta});
444-
}
441+
var _meta = gd._fullLayout._meta;
442+
if(_meta) tx = Lib.templateString(tx, _meta);
445443

446444
text.call(Drawing.font, menuOpts.font)
447445
.text(tx)

src/plots/attributes.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,25 @@ module.exports = {
102102
'DOM elements'
103103
].join(' ')
104104
},
105+
meta: {
106+
valType: 'any',
107+
arrayOk: true,
108+
role: 'info',
109+
editType: 'plot',
110+
description: [
111+
'Assigns extra meta information associated with this trace',
112+
'that can be used in various text attributes.',
113+
'Attributes such as trace `name`, graph, axis and colorbar `title.text`, annotation `text`',
114+
'`rangeselector`, `updatemenues` and `sliders` `label` text',
115+
'all support `meta`.',
116+
'To access the trace `meta` values in an attribute in the same trace, simply use',
117+
'`%{meta[i]}` where `i` is the index or key of the `meta`',
118+
'item in question.',
119+
'To access trace `meta` in layout attributes, use',
120+
'`%{data[n[.meta[i]}` where `i` is the index or key of the `meta`',
121+
'and `n` is the trace index.'
122+
].join(' ')
123+
},
105124

106125
// N.B. these cannot be 'data_array' as they do not have the same length as
107126
// other data arrays and arrayOk attributes in general

src/plots/gl3d/layout/convert.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ proto.merge = function(fullLayout, sceneLayout) {
8383
}
8484

8585
// Axes labels
86-
opts.labels[i] = fullLayout.meta ?
87-
Lib.templateString(axes.title.text, {meta: fullLayout.meta}) :
86+
opts.labels[i] = fullLayout._meta ?
87+
Lib.templateString(axes.title.text, fullLayout._meta) :
8888
axes.title.text;
8989

9090
if('font' in axes.title) {

0 commit comments

Comments
 (0)