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

Diff for: src/components/annotations/draw.js

+2-2
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')

Diff for: src/components/colorbar/draw.js

+3
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
};

Diff for: src/components/fx/hover.js

+4-4
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) {

Diff for: src/components/legend/draw.js

+2-2
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');

Diff for: src/components/rangeselector/draw.js

+4-4
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

Diff for: src/components/sliders/draw.js

+4-8
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)

Diff for: src/components/titles/index.js

+6-2
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;

Diff for: src/components/updatemenus/draw.js

+2-4
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)

Diff for: src/plots/attributes.js

+19
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

Diff for: src/plots/gl3d/layout/convert.js

+2-2
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) {

Diff for: src/plots/layout_attributes.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -410,15 +410,19 @@ module.exports = {
410410
},
411411

412412
meta: {
413-
valType: 'data_array',
413+
valType: 'any',
414+
arrayOk: true,
415+
role: 'info',
414416
editType: 'plot',
415417
description: [
416418
'Assigns extra meta information that can be used in various `text` attributes.',
417419
'Attributes such as the graph, axis and colorbar `title.text`, annotation `text`',
418420
'`trace.name` in legend items, `rangeselector`, `updatemenues` and `sliders` `label` text',
419421
'all support `meta`. One can access `meta` fields using template strings:',
420422
'`%{meta[i]}` where `i` is the index of the `meta`',
421-
'item in question.'
423+
'item in question.',
424+
'`meta` can also be an object for example `{key: value}` which can be accessed',
425+
'%{meta[key]}.'
422426
].join(' ')
423427
},
424428

Diff for: src/plots/plots.js

+37-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
109
'use strict';
1110

1211
var d3 = require('d3');
@@ -483,6 +482,10 @@ plots.supplyDefaults = function(gd, opts) {
483482
oldFullLayout._zoomlayer.selectAll('.select-outline').remove();
484483
}
485484

485+
486+
// fill in meta helpers
487+
fillMetaTextHelpers(newFullData, newFullLayout);
488+
486489
// relink functions and _ attributes to promote consistency between plots
487490
relinkPrivateKeys(newFullLayout, oldFullLayout);
488491

@@ -698,6 +701,38 @@ function getFormatter(formatObj, separators) {
698701
return d3.locale(formatObj);
699702
}
700703

704+
function fillMetaTextHelpers(newFullData, newFullLayout) {
705+
var _meta;
706+
var meta4data = [];
707+
708+
if(newFullLayout.meta) {
709+
_meta = newFullLayout._meta = {
710+
meta: newFullLayout.meta,
711+
layout: {meta: newFullLayout.meta}
712+
};
713+
}
714+
715+
for(var i = 0; i < newFullData.length; i++) {
716+
var trace = newFullData[i];
717+
718+
if(trace.meta) {
719+
meta4data[trace.index] = trace._meta = {meta: trace.meta};
720+
} else if(newFullLayout.meta) {
721+
trace._meta = {meta: newFullLayout.meta};
722+
}
723+
if(newFullLayout.meta) {
724+
trace._meta.layout = {meta: newFullLayout.meta};
725+
}
726+
}
727+
728+
if(meta4data.length) {
729+
if(!_meta) {
730+
_meta = newFullLayout._meta = {};
731+
}
732+
_meta.data = meta4data;
733+
}
734+
}
735+
701736
// Create storage for all of the data related to frames and transitions:
702737
plots.createTransitionData = function(gd) {
703738
// Set up the default keyframe if it doesn't exist:
@@ -1236,6 +1271,7 @@ plots.supplyTraceDefaults = function(traceIn, traceOut, colorIndex, layout, trac
12361271
if(visible) {
12371272
coerce('customdata');
12381273
coerce('ids');
1274+
coerce('meta');
12391275

12401276
if(Registry.traceIs(traceOut, 'showLegend')) {
12411277
traceOut._dfltShowLegend = true;

Diff for: src/traces/pie/plot.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,10 @@ function plot(gd, cdpie) {
204204
s.attr('data-notex', 1);
205205
});
206206

207-
var txt = fullLayout.meta ?
208-
Lib.templateString(trace.title.text, {meta: fullLayout.meta}) :
209-
trace.title.text;
207+
var txt = trace.title.text;
208+
if(trace._meta) {
209+
txt = Lib.templateString(txt, trace._meta);
210+
}
210211

211212
titleText.text(txt)
212213
.attr({
@@ -481,18 +482,18 @@ function determineInsideTextFont(trace, pt, layoutFont) {
481482
}
482483

483484
function prerenderTitles(cdpie, gd) {
484-
var fullLayout = gd._fullLayout;
485-
486485
var cd0, trace;
486+
487487
// Determine the width and height of the title for each pie.
488488
for(var i = 0; i < cdpie.length; i++) {
489489
cd0 = cdpie[i][0];
490490
trace = cd0.trace;
491491

492492
if(trace.title.text) {
493-
var txt = fullLayout.meta ?
494-
Lib.templateString(trace.title.text, {meta: fullLayout.meta}) :
495-
trace.title.text;
493+
var txt = trace.title.text;
494+
if(trace._meta) {
495+
txt = Lib.templateString(txt, trace._meta);
496+
}
496497

497498
var dummyTitle = Drawing.tester.append('text')
498499
.attr('data-notex', 1)

Diff for: test/image/baselines/trace_metatext.png

96.3 KB
Loading

0 commit comments

Comments
 (0)