Skip to content

Commit ddadef1

Browse files
committed
add support for layout.meta template for trace.name
- both in legend item and in hover labels!
1 parent 71cad03 commit ddadef1

File tree

5 files changed

+40
-4
lines changed

5 files changed

+40
-4
lines changed

src/components/fx/hover.js

+5
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,7 @@ function _hover(gd, evt, subplot, noHoverEvent) {
738738
var EXTRA_STRING_REGEX = /<extra>([\s\S]*)<\/extra>/;
739739

740740
function createHoverText(hoverData, opts, gd) {
741+
var fullLayout = gd._fullLayout;
741742
var hovermode = opts.hovermode;
742743
var rotateLabels = opts.rotateLabels;
743744
var bgColor = opts.bgColor;
@@ -925,6 +926,10 @@ function createHoverText(hoverData, opts, gd) {
925926
if(d.nameOverride !== undefined) d.name = d.nameOverride;
926927

927928
if(d.name) {
929+
if(fullLayout.meta) {
930+
d.name = Lib.templateString(d.name, {meta: fullLayout.meta});
931+
}
932+
928933
name = svgTextUtils.plainText(d.name || '', {
929934
len: d.nameLength,
930935
allowedTags: ['br', 'sub', 'sup', 'b', 'i', 'em']

src/components/legend/draw.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,13 @@ function drawTexts(g, gd, maxLength) {
404404
var trace = legendItem.trace;
405405
var isPie = Registry.traceIs(trace, 'pie');
406406
var traceIndex = trace.index;
407-
var name = isPie ? legendItem.label : trace.name;
408407
var isEditable = gd._context.edits.legendText && !isPie;
409408

409+
var name = isPie ? legendItem.label : trace.name;
410+
if(fullLayout.meta) {
411+
name = Lib.templateString(name, {meta: fullLayout.meta});
412+
}
413+
410414
var textEl = Lib.ensureSingle(g, 'text', 'legendtext');
411415

412416
textEl.attr('text-anchor', 'start')
5.55 KB
Loading

test/image/mocks/layout_metatext.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"data": [{
3-
"y": [1, 2, 1]
3+
"x": ["2019-01-01", "2019-02-10", "2019-03-24"],
4+
"y": [1, 2, 1],
5+
"name": "TRACE %{meta[3]}"
46
}, {
57
"type": "scatterpolar",
68
"r": [1, 2, 1]
@@ -29,7 +31,6 @@
2931
"width": 700,
3032
"height": 800,
3133
"margin": {"b": 40},
32-
"showlegend": false,
3334

3435
"title": {"text": "This graph is %{meta[2]}"},
3536
"xaxis": {
@@ -78,7 +79,7 @@
7879
"text": "<i>N.B.</i> %{meta[2]}",
7980
"xref": "x",
8081
"yref": "y",
81-
"x": 1,
82+
"x": "2019-02-10",
8283
"y": 2
8384
}]
8485
}

test/jasmine/tests/hover_label_test.js

+26
Original file line numberDiff line numberDiff line change
@@ -1734,6 +1734,32 @@ describe('hover info', function() {
17341734
.then(done);
17351735
});
17361736
});
1737+
1738+
it('should work with trace.name linked to layout.meta', function(done) {
1739+
var gd = createGraphDiv();
1740+
1741+
Plotly.plot(gd, [{
1742+
y: [1, 1, 1],
1743+
name: '%{meta[0]}',
1744+
marker: {size: 40}
1745+
}, {
1746+
y: [1]
1747+
}], {
1748+
meta: ['yo!'],
1749+
width: 400,
1750+
height: 400
1751+
})
1752+
.then(function() { _hoverNatural(gd, 200, 200); })
1753+
.then(function() {
1754+
assertHoverLabelContent({
1755+
nums: '1',
1756+
name: 'yo!',
1757+
axis: '2'
1758+
});
1759+
})
1760+
.catch(failTest)
1761+
.then(done);
1762+
});
17371763
});
17381764

17391765
describe('hover info on stacked subplots', function() {

0 commit comments

Comments
 (0)