Skip to content

Commit 28cd420

Browse files
committed
add trace meta attribute + add _meta helpers
1 parent 8945bd1 commit 28cd420

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

Diff for: src/plots/attributes.js

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

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

Diff for: src/plots/plots.js

+38-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,39 @@ 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.meta = newFullLayout.meta;
711+
_meta.layout = {meta: newFullLayout.meta};
712+
}
713+
714+
for(var i = 0; i < newFullData.length; i++) {
715+
var trace = newFullData[i];
716+
717+
if(trace.meta) {
718+
meta4data[trace.index] = {meta: trace.meta};
719+
}
720+
721+
trace._meta = {
722+
meta: Lib.extendFlat({}, newFullLayout.meta || {}, trace.meta)
723+
};
724+
if(newFullLayout.meta) {
725+
trace._meta.layout = {meta: newFullLayout.meta};
726+
}
727+
}
728+
729+
if(meta4data.length) {
730+
if(!_meta) {
731+
_meta = newFullLayout._meta = {};
732+
}
733+
_meta.data = meta4data;
734+
}
735+
}
736+
701737
// Create storage for all of the data related to frames and transitions:
702738
plots.createTransitionData = function(gd) {
703739
// Set up the default keyframe if it doesn't exist:
@@ -1236,6 +1272,7 @@ plots.supplyTraceDefaults = function(traceIn, traceOut, colorIndex, layout, trac
12361272
if(visible) {
12371273
coerce('customdata');
12381274
coerce('ids');
1275+
coerce('meta');
12391276

12401277
if(Registry.traceIs(traceOut, 'showLegend')) {
12411278
traceOut._dfltShowLegend = true;

0 commit comments

Comments
 (0)