Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trace meta text #3865

Merged
merged 7 commits into from
May 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/components/annotations/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {

var font = options.font;

var text = fullLayout.meta ?
Lib.templateString(options.text, {meta: fullLayout.meta}) :
var text = fullLayout._meta ?
Lib.templateString(options.text, fullLayout._meta) :
options.text;

var annText = annTextGroupInner.append('text')
Expand Down
3 changes: 3 additions & 0 deletions src/components/colorbar/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ function makeColorBarData(gd) {
opts._id = 'cb' + trace.uid + (allowsMultiplotCbs && contName ? '-' + contName : '');
opts._traceIndex = trace.index;
opts._propPrefix = (contName ? contName + '.' : '') + 'colorbar.';
opts._meta = trace._meta;
calcOpts();
out.push(opts);
}
Expand All @@ -156,6 +157,7 @@ function makeColorBarData(gd) {
opts = initOpts(cont.colorbar);
opts._id = 'cb' + k;
opts._propPrefix = k + '.colorbar.';
opts._meta = fullLayout._meta;

cbOpt = {min: 'cmin', max: 'cmax'};
if(colorAxOpts[0] !== 'heatmap') {
Expand Down Expand Up @@ -281,6 +283,7 @@ function drawColorBar(g, opts, gd) {
propContainer: ax,
propName: opts._propPrefix + 'title',
traceIndex: opts._traceIndex,
_meta: opts._meta,
placeholder: fullLayout._dfltTitle.colorbar,
containerGroup: g.select('.' + cn.cbtitle)
};
Expand Down
8 changes: 4 additions & 4 deletions src/components/fx/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -887,8 +887,8 @@ function createHoverText(hoverData, opts, gd) {
if(d.nameOverride !== undefined) d.name = d.nameOverride;

if(d.name) {
if(fullLayout.meta) {
d.name = Lib.templateString(d.name, {meta: fullLayout.meta});
if(d.trace._meta) {
d.name = Lib.templateString(d.name, d.trace._meta);
}
name = plainText(d.name, d.nameLength);
}
Expand Down Expand Up @@ -925,7 +925,7 @@ function createHoverText(hoverData, opts, gd) {
}

// hovertemplate
var d3locale = gd._fullLayout._d3locale;
var d3locale = fullLayout._d3locale;
var hovertemplate = d.hovertemplate || false;
var hovertemplateLabels = d.hovertemplateLabels || d;
var eventData = d.eventData[0] || {};
Expand All @@ -935,7 +935,7 @@ function createHoverText(hoverData, opts, gd) {
hovertemplateLabels,
d3locale,
eventData,
{meta: fullLayout.meta}
d.trace._meta
);

text = text.replace(EXTRA_STRING_REGEX, function(match, extra) {
Expand Down
4 changes: 2 additions & 2 deletions src/components/legend/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,8 @@ function drawTexts(g, gd, maxLength) {
var isEditable = gd._context.edits.legendText && !isPie;

var name = isPie ? legendItem.label : trace.name;
if(fullLayout.meta) {
name = Lib.templateString(name, {meta: fullLayout.meta});
if(trace._meta) {
name = Lib.templateString(name, trace._meta);
}

var textEl = Lib.ensureSingle(g, 'text', 'legendtext');
Expand Down
8 changes: 4 additions & 4 deletions src/components/rangeselector/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,14 @@ function drawButtonText(button, selectorLayout, d, gd) {
});

text.call(Drawing.font, selectorLayout.font)
.text(getLabel(d, gd._fullLayout.meta))
.text(getLabel(d, gd._fullLayout._meta))
.call(textLayout);
}

function getLabel(opts, meta) {
function getLabel(opts, _meta) {
if(opts.label) {
return meta ?
Lib.templateString(opts.label, {meta: meta}) :
return _meta ?
Lib.templateString(opts.label, _meta) :
opts.label;
}

Expand Down
12 changes: 4 additions & 8 deletions src/components/sliders/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,8 @@ function drawCurrentValue(sliderGroup, sliderOpts, valueOverride) {
str += valueOverride;
} else {
var curVal = sliderOpts.steps[sliderOpts.active].label;
var meta = sliderOpts._gd._fullLayout.meta;
if(meta) {
curVal = Lib.templateString(curVal, {meta: meta});
}
var _meta = sliderOpts._gd._fullLayout._meta;
if(_meta) curVal = Lib.templateString(curVal, _meta);
str += curVal;
}

Expand Down Expand Up @@ -367,10 +365,8 @@ function drawLabel(item, data, sliderOpts) {
});

var tx = data.step.label;
var meta = sliderOpts._gd._fullLayout.meta;
if(meta) {
tx = Lib.templateString(tx, {meta: meta});
}
var _meta = sliderOpts._gd._fullLayout._meta;
if(_meta) tx = Lib.templateString(tx, _meta);

text.call(Drawing.font, sliderOpts.font)
.text(tx)
Expand Down
8 changes: 6 additions & 2 deletions src/components/titles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ var numStripRE = / [XY][0-9]* /;
* offset - shift up/down in the rotated frame (unused?)
* containerGroup - if an svg <g> element already exists to hold this
* title, include here. Otherwise it will go in fullLayout._infolayer
* _meta {object (optional} - meta key-value to for title with
* Lib.templateString, default to fullLayout._meta, if not provided
*
* @return {selection} d3 selection of title container group
*/
Expand Down Expand Up @@ -97,8 +99,10 @@ function draw(gd, titleClass, options) {
if(!editable) txt = '';
}

if(fullLayout.meta) {
txt = Lib.templateString(txt, {meta: fullLayout.meta});
if(options._meta) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if we need to bypass [ ] or { } here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need. Lib.termplateString handles those cases correctly.

txt = Lib.templateString(txt, options._meta);
} else if(fullLayout._meta) {
txt = Lib.templateString(txt, fullLayout._meta);
}

var elShouldExist = txt || editable;
Expand Down
6 changes: 2 additions & 4 deletions src/components/updatemenus/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,10 +438,8 @@ function drawItemText(item, menuOpts, itemOpts, gd) {
});

var tx = itemOpts.label;
var meta = gd._fullLayout.meta;
if(meta) {
tx = Lib.templateString(tx, {meta: meta});
}
var _meta = gd._fullLayout._meta;
if(_meta) tx = Lib.templateString(tx, _meta);

text.call(Drawing.font, menuOpts.font)
.text(tx)
Expand Down
19 changes: 19 additions & 0 deletions src/plots/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,25 @@ module.exports = {
'DOM elements'
].join(' ')
},
meta: {
valType: 'any',
arrayOk: true,
role: 'info',
editType: 'plot',
description: [
'Assigns extra meta information associated with this trace',
'that can be used in various text attributes.',
'Attributes such as trace `name`, graph, axis and colorbar `title.text`, annotation `text`',
'`rangeselector`, `updatemenues` and `sliders` `label` text',
'all support `meta`.',
'To access the trace `meta` values in an attribute in the same trace, simply use',
'`%{meta[i]}` where `i` is the index or key of the `meta`',
'item in question.',
'To access trace `meta` in layout attributes, use',
'`%{data[n[.meta[i]}` where `i` is the index or key of the `meta`',
'and `n` is the trace index.'
].join(' ')
},

// N.B. these cannot be 'data_array' as they do not have the same length as
// other data arrays and arrayOk attributes in general
Expand Down
4 changes: 2 additions & 2 deletions src/plots/gl3d/layout/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ proto.merge = function(fullLayout, sceneLayout) {
}

// Axes labels
opts.labels[i] = fullLayout.meta ?
Lib.templateString(axes.title.text, {meta: fullLayout.meta}) :
opts.labels[i] = fullLayout._meta ?
Lib.templateString(axes.title.text, fullLayout._meta) :
axes.title.text;

if('font' in axes.title) {
Expand Down
8 changes: 6 additions & 2 deletions src/plots/layout_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,15 +410,19 @@ module.exports = {
},

meta: {
valType: 'data_array',
valType: 'any',
arrayOk: true,
role: 'info',
editType: 'plot',
description: [
'Assigns extra meta information that can be used in various `text` attributes.',
'Attributes such as the graph, axis and colorbar `title.text`, annotation `text`',
'`trace.name` in legend items, `rangeselector`, `updatemenues` and `sliders` `label` text',
'all support `meta`. One can access `meta` fields using template strings:',
'`%{meta[i]}` where `i` is the index of the `meta`',
'item in question.'
'item in question.',
'`meta` can also be an object for example `{key: value}` which can be accessed',
'%{meta[key]}.'
].join(' ')
},

Expand Down
38 changes: 37 additions & 1 deletion src/plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* LICENSE file in the root directory of this source tree.
*/


'use strict';

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


// fill in meta helpers
fillMetaTextHelpers(newFullData, newFullLayout);

// relink functions and _ attributes to promote consistency between plots
relinkPrivateKeys(newFullLayout, oldFullLayout);

Expand Down Expand Up @@ -698,6 +701,38 @@ function getFormatter(formatObj, separators) {
return d3.locale(formatObj);
}

function fillMetaTextHelpers(newFullData, newFullLayout) {
var _meta;
var meta4data = [];

if(newFullLayout.meta) {
_meta = newFullLayout._meta = {
meta: newFullLayout.meta,
layout: {meta: newFullLayout.meta}
};
}

for(var i = 0; i < newFullData.length; i++) {
var trace = newFullData[i];

if(trace.meta) {
meta4data[trace.index] = trace._meta = {meta: trace.meta};
} else if(newFullLayout.meta) {
trace._meta = {meta: newFullLayout.meta};
}
if(newFullLayout.meta) {
trace._meta.layout = {meta: newFullLayout.meta};
}
}

if(meta4data.length) {
if(!_meta) {
_meta = newFullLayout._meta = {};
}
_meta.data = meta4data;
}
}

// Create storage for all of the data related to frames and transitions:
plots.createTransitionData = function(gd) {
// Set up the default keyframe if it doesn't exist:
Expand Down Expand Up @@ -1236,6 +1271,7 @@ plots.supplyTraceDefaults = function(traceIn, traceOut, colorIndex, layout, trac
if(visible) {
coerce('customdata');
coerce('ids');
coerce('meta');

if(Registry.traceIs(traceOut, 'showLegend')) {
traceOut._dfltShowLegend = true;
Expand Down
17 changes: 9 additions & 8 deletions src/traces/pie/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,10 @@ function plot(gd, cdpie) {
s.attr('data-notex', 1);
});

var txt = fullLayout.meta ?
Lib.templateString(trace.title.text, {meta: fullLayout.meta}) :
trace.title.text;
var txt = trace.title.text;
if(trace._meta) {
txt = Lib.templateString(txt, trace._meta);
}

titleText.text(txt)
.attr({
Expand Down Expand Up @@ -481,18 +482,18 @@ function determineInsideTextFont(trace, pt, layoutFont) {
}

function prerenderTitles(cdpie, gd) {
var fullLayout = gd._fullLayout;

var cd0, trace;

// Determine the width and height of the title for each pie.
for(var i = 0; i < cdpie.length; i++) {
cd0 = cdpie[i][0];
trace = cd0.trace;

if(trace.title.text) {
var txt = fullLayout.meta ?
Lib.templateString(trace.title.text, {meta: fullLayout.meta}) :
trace.title.text;
var txt = trace.title.text;
if(trace._meta) {
txt = Lib.templateString(txt, trace._meta);
}

var dummyTitle = Drawing.tester.append('text')
.attr('data-notex', 1)
Expand Down
Binary file added test/image/baselines/trace_metatext.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading