Skip to content

Commit c44ed53

Browse files
authored
Merge pull request #3439 from plotly/metatext
Metatext
2 parents 7d67f80 + b03e777 commit c44ed53

File tree

9 files changed

+130
-10
lines changed

9 files changed

+130
-10
lines changed

src/components/annotations/draw.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,13 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
209209

210210
var font = options.font;
211211

212+
var text = fullLayout.meta ?
213+
Lib.templateString(options.text, {meta: fullLayout.meta}) :
214+
options.text;
215+
212216
var annText = annTextGroupInner.append('text')
213217
.classed('annotation-text', true)
214-
.text(options.text);
218+
.text(text);
215219

216220
function textLayout(s) {
217221
s.call(Drawing.font, font)
@@ -678,6 +682,7 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
678682
.call(textLayout)
679683
.on('edit', function(_text) {
680684
options.text = _text;
685+
681686
this.call(textLayout);
682687

683688
modifyItem('text', _text);

src/components/titles/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ function draw(gd, titleClass, options) {
9797
if(!editable) txt = '';
9898
}
9999

100+
if(fullLayout.meta) {
101+
txt = Lib.templateString(txt, {meta: fullLayout.meta});
102+
}
103+
100104
var elShouldExist = txt || editable;
101105

102106
if(!group) {

src/plots/gl3d/layout/convert.js

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

9-
109
'use strict';
1110

1211
var str2RgbaArray = require('../../../lib/str2rgbarray');
12+
var Lib = require('../../../lib');
1313

1414
var AXES_NAMES = ['xaxis', 'yaxis', 'zaxis'];
1515

@@ -66,7 +66,7 @@ function AxesOptions() {
6666

6767
var proto = AxesOptions.prototype;
6868

69-
proto.merge = function(sceneLayout) {
69+
proto.merge = function(fullLayout, sceneLayout) {
7070
var opts = this;
7171
for(var i = 0; i < 3; ++i) {
7272
var axes = sceneLayout[AXES_NAMES[i]];
@@ -83,7 +83,10 @@ proto.merge = function(sceneLayout) {
8383
}
8484

8585
// Axes labels
86-
opts.labels[i] = axes.title.text;
86+
opts.labels[i] = fullLayout.meta ?
87+
Lib.templateString(axes.title.text, {meta: fullLayout.meta}) :
88+
axes.title.text;
89+
8790
if('font' in axes.title) {
8891
if(axes.title.font.color) opts.labelColor[i] = str2RgbaArray(axes.title.font.color);
8992
if(axes.title.font.family) opts.labelFont[i] = axes.title.font.family;
@@ -151,9 +154,9 @@ proto.merge = function(sceneLayout) {
151154
};
152155

153156

154-
function createAxesOptions(plotlyOptions) {
157+
function createAxesOptions(fullLayout, sceneLayout) {
155158
var result = new AxesOptions();
156-
result.merge(plotlyOptions);
159+
result.merge(fullLayout, sceneLayout);
157160
return result;
158161
}
159162

src/plots/gl3d/scene.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ function Scene(options, fullLayout) {
305305
/*
306306
* Move this to calc step? Why does it work here?
307307
*/
308-
this.axesOptions = createAxesOptions(fullLayout[this.id]);
308+
this.axesOptions = createAxesOptions(fullLayout, fullLayout[this.id]);
309309
this.spikeOptions = createSpikeOptions(fullLayout[this.id]);
310310
this.container = sceneContainer;
311311
this.staticMode = !!options.staticPlot;
@@ -406,7 +406,7 @@ proto.plot = function(sceneData, fullLayout, layout) {
406406
this.fullSceneLayout = fullSceneLayout;
407407

408408
this.glplotLayout = fullSceneLayout;
409-
this.axesOptions.merge(fullSceneLayout);
409+
this.axesOptions.merge(fullLayout, fullSceneLayout);
410410
this.spikeOptions.merge(fullSceneLayout);
411411

412412
// Update camera and camera mode

src/plots/layout_attributes.js

+11
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,17 @@ module.exports = {
409409
},
410410
editType: 'modebar'
411411
},
412+
meta: {
413+
valType: 'data_array',
414+
editType: 'plot',
415+
description: [
416+
'Assigns extra meta information that can be used in various `text` attributes.',
417+
'Attributes such as the graph, axis and colorbar `title.text` and annotation `text`',
418+
'support `meta`. One can access `meta` fields using template strings:',
419+
'`%{meta[i]}` where `i` is the index of the `meta`',
420+
'item in question.'
421+
].join(' ')
422+
},
412423
_deprecated: {
413424
title: {
414425
valType: 'string',

src/plots/plots.js

+2
Original file line numberDiff line numberDiff line change
@@ -1455,6 +1455,8 @@ plots.supplyLayoutGlobalDefaults = function(layoutIn, layoutOut, formatObj) {
14551455
coerce('modebar.activecolor', Color.addOpacity(modebarDefaultColor, 0.7));
14561456
coerce('modebar.uirevision', uirevision);
14571457

1458+
coerce('meta');
1459+
14581460
Registry.getComponentMethod(
14591461
'calendars',
14601462
'handleDefaults'

src/traces/pie/plot.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,11 @@ module.exports = function plot(gd, cdpie) {
334334
s.attr('data-notex', 1);
335335
});
336336

337-
titleText.text(trace.title.text)
337+
var txt = fullLayout.meta ?
338+
Lib.templateString(trace.title.text, {meta: fullLayout.meta}) :
339+
trace.title.text;
340+
341+
titleText.text(txt)
338342
.attr({
339343
'class': 'titletext',
340344
transform: '',
@@ -467,16 +471,22 @@ function determineInsideTextFont(trace, pt, layoutFont) {
467471
}
468472

469473
function prerenderTitles(cdpie, gd) {
474+
var fullLayout = gd._fullLayout;
475+
470476
var cd0, trace;
471477
// Determine the width and height of the title for each pie.
472478
for(var i = 0; i < cdpie.length; i++) {
473479
cd0 = cdpie[i][0];
474480
trace = cd0.trace;
475481

476482
if(trace.title.text) {
483+
var txt = fullLayout.meta ?
484+
Lib.templateString(trace.title.text, {meta: fullLayout.meta}) :
485+
trace.title.text;
486+
477487
var dummyTitle = Drawing.tester.append('text')
478488
.attr('data-notex', 1)
479-
.text(trace.title.text)
489+
.text(txt)
480490
.call(Drawing.font, trace.title.font)
481491
.call(svgTextUtils.convertToTspans, gd);
482492
var bBox = Drawing.bBox(dummyTitle.node(), true);
94.8 KB
Loading

test/image/mocks/layout_metatext.json

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
{
2+
"data": [{
3+
"y": [1, 2, 1]
4+
}, {
5+
"type": "scatterpolar",
6+
"r": [1, 2, 1]
7+
}, {
8+
"type": "scatterternary",
9+
"a": [2, 1, 1],
10+
"b": [1, 2, 1],
11+
"c": [1, 1, 2.12]
12+
}, {
13+
"type": "surface",
14+
"z": [[1, 2, 3], [1, 2, 1], [3, 2, 1]],
15+
"colorbar": {
16+
"title": {"text": "Product %{meta[0]}", "side": "right"},
17+
"len": 0.3
18+
}
19+
}, {
20+
"type": "pie",
21+
"labels": ["a", "b", "c"],
22+
"values": [1, 2, 3],
23+
"domain": {"row": 0, "column": 1},
24+
"title": {"text": "Employee %{meta[1]}"}
25+
}],
26+
"layout": {
27+
"meta": ["A", "B", "<b>IMPORTANT</b>", "company<sup>TM</sup>", 1000],
28+
"grid": {"rows": 3, "columns": 2, "xgap": 0.2, "ygap": 0.35},
29+
"width": 700,
30+
"height": 800,
31+
"margin": {"b": 40},
32+
"showlegend": false,
33+
34+
"title": {"text": "This graph is %{meta[2]}"},
35+
"xaxis": {
36+
"domain": {"row": 0, "column": 0},
37+
"title": {"text": "Worth more than %{meta[4]} %{meta[1]}"}
38+
},
39+
"yaxis": {
40+
"domain": {"row": 0, "column": 0},
41+
"title": {"text": "$ by %{meta[3]}"}
42+
},
43+
"polar": {
44+
"bgcolor": "#d3d3d3",
45+
"domain": {"row": 1, "column": 0},
46+
"radialaxis": {
47+
"title": {
48+
"text": "%{meta[3]} -> %{meta[4]}",
49+
"font": {"color": "red"}
50+
}
51+
}
52+
},
53+
"ternary": {
54+
"domain": {"row": 2, "column": 0},
55+
"aaxis": {"title": {"text": "%{meta[2]}"}},
56+
"baxis": {"title": {"text": "%{meta[1]}"}},
57+
"caxis": {"title": {"text": "%{meta[4]}"}}
58+
},
59+
"scene": {
60+
"domain": {"row": 1, "column": 1},
61+
"camera": {"eye": {"x": 0.01, "y": 0.01, "z": 2.165}},
62+
"xaxis": {"title": {"text": "AX:%{meta[1]}"}},
63+
"yaxis": {"title": {"text": "AX:%{meta[0]}"}},
64+
"zaxis": {"title": {"text": "%{meta[1]}%{meta[0]}"}},
65+
"annotations": [{
66+
"text": "Look at %{meta[1]}",
67+
"bgcolor": "#d3d3d3",
68+
"borderpad": 2,
69+
"bordercolor": "#000",
70+
"borderwidth": "1",
71+
"x": 1,
72+
"y": 1,
73+
"z": 2
74+
}]
75+
},
76+
77+
"annotations": [{
78+
"text": "<i>N.B.</i> %{meta[2]}",
79+
"xref": "x",
80+
"yref": "y",
81+
"x": 1,
82+
"y": 2
83+
}]
84+
}
85+
}

0 commit comments

Comments
 (0)