Skip to content

Commit 902d2d3

Browse files
committed
texttemplate: add support for scatter(carpet|geo|polar|ternary)
1 parent e0bde45 commit 902d2d3

File tree

15 files changed

+225
-1
lines changed

15 files changed

+225
-1
lines changed

src/components/drawing/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,10 @@ drawing.textPointStyle = function(s, trace, gd) {
700700
}
701701

702702
if(template) {
703-
text = Lib.texttemplateString(text, {}, gd._fullLayout._d3locale, d, trace._meta || {}, {customdata: d.data});
703+
var extra = {};
704+
if(d.tx) extra.text = d.tx;
705+
if(d.data) extra.customdata = d.data;
706+
text = Lib.texttemplateString(text, {}, gd._fullLayout._d3locale, d, trace._meta || {}, extra);
704707
}
705708

706709
var pos = d.tp || trace.textposition;

src/traces/scattercarpet/attributes.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
var scatterAttrs = require('../scatter/attributes');
1212
var plotAttrs = require('../../plots/attributes');
1313
var hovertemplateAttrs = require('../../components/fx/hovertemplate_attributes');
14+
var texttemplateAttrs = require('../../plots/texttemplate_attributes');
1415
var colorScaleAttrs = require('../../components/colorscale/attributes');
1516

1617
var extendFlat = require('../../lib/extend').extendFlat;
@@ -52,6 +53,9 @@ module.exports = {
5253
'these elements will be seen in the hover labels.'
5354
].join(' ')
5455
}),
56+
texttemplate: texttemplateAttrs({editType: 'plot'}, {
57+
keys: ['a', 'b', 'text']
58+
}),
5559
hovertext: extendFlat({}, scatterAttrs.hovertext, {
5660
description: [
5761
'Sets hover text elements associated with each (a,b) point.',

src/traces/scattercarpet/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
4444
traceOut._length = len;
4545

4646
coerce('text');
47+
coerce('texttemplate');
4748
coerce('hovertext');
4849

4950
var defaultMode = len < constants.PTS_LINESONLY ? 'lines+markers' : 'lines';

src/traces/scattergeo/attributes.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
'use strict';
1010

1111
var hovertemplateAttrs = require('../../components/fx/hovertemplate_attributes');
12+
var texttemplateAttrs = require('../../plots/texttemplate_attributes');
1213
var scatterAttrs = require('../scatter/attributes');
1314
var plotAttrs = require('../../plots/attributes');
1415
var colorAttributes = require('../../components/colorscale/attributes');
@@ -64,6 +65,9 @@ module.exports = overrideAll({
6465
'these elements will be seen in the hover labels.'
6566
].join(' ')
6667
}),
68+
texttemplate: texttemplateAttrs({editType: 'plot'}, {
69+
keys: ['latlon', 'text']
70+
}),
6771
hovertext: extendFlat({}, scatterAttrs.hovertext, {
6872
description: [
6973
'Sets hover text elements associated with each (lon,lat) pair',

src/traces/scattergeo/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
3232
}
3333

3434
coerce('text');
35+
coerce('texttemplate');
3536
coerce('hovertext');
3637
coerce('hovertemplate');
3738
coerce('mode');

src/traces/scatterpolar/attributes.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
'use strict';
1010

1111
var hovertemplateAttrs = require('../../components/fx/hovertemplate_attributes');
12+
var texttemplateAttrs = require('../../plots/texttemplate_attributes');
1213
var extendFlat = require('../../lib/extend').extendFlat;
1314
var scatterAttrs = require('../scatter/attributes');
1415
var plotAttrs = require('../../plots/attributes');
@@ -85,6 +86,9 @@ module.exports = {
8586
},
8687

8788
text: scatterAttrs.text,
89+
texttemplate: texttemplateAttrs({editType: 'plot'}, {
90+
keys: ['r', 'theta', 'text']
91+
}),
8892
hovertext: scatterAttrs.hovertext,
8993

9094
line: {

src/traces/scatterpolar/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
3434
coerce('thetaunit');
3535
coerce('mode', len < PTS_LINESONLY ? 'lines+markers' : 'lines');
3636
coerce('text');
37+
coerce('texttemplate');
3738
coerce('hovertext');
3839
if(traceOut.hoveron !== 'fills') coerce('hovertemplate');
3940

src/traces/scatterternary/attributes.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
'use strict';
1010

1111
var hovertemplateAttrs = require('../../components/fx/hovertemplate_attributes');
12+
var texttemplateAttrs = require('../../plots/texttemplate_attributes');
1213
var scatterAttrs = require('../scatter/attributes');
1314
var plotAttrs = require('../../plots/attributes');
1415
var colorScaleAttrs = require('../../components/colorscale/attributes');
@@ -80,6 +81,9 @@ module.exports = {
8081
'these elements will be seen in the hover labels.'
8182
].join(' ')
8283
}),
84+
texttemplate: texttemplateAttrs({editType: 'plot'}, {
85+
keys: ['a', 'b', 'c', 'text']
86+
}),
8387
hovertext: extendFlat({}, scatterAttrs.hovertext, {
8488
description: [
8589
'Sets hover text elements associated with each (a,b,c) point.',

src/traces/scatterternary/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
5858
coerce('sum');
5959

6060
coerce('text');
61+
coerce('texttemplate');
6162
coerce('hovertext');
6263
if(traceOut.hoveron !== 'fills') coerce('hovertemplate');
6364

96.6 KB
Loading
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
{
2+
"data": [{
3+
"type": "scattergeo",
4+
"mode": "markers+text",
5+
"lon": [
6+
-73.57,
7+
-79.24,
8+
-123.06
9+
],
10+
"lat": [
11+
45.5,
12+
43.4,
13+
49.13
14+
],
15+
"text": [
16+
"Montreal",
17+
"Toronto",
18+
"Vancouver"
19+
],
20+
"texttemplate": "%{text}: (%{lonlat[0]:.0f}, %{lonlat[1]:0.f}): %{customdata:.2s}",
21+
"textposition": "top center",
22+
"customdata": [1780000, 2930000, 675218],
23+
"geo": "geo"
24+
}, {
25+
"type": "carpet",
26+
"carpet": "carpet1",
27+
"a": [0.1, 0.2, 0.3],
28+
"b": [1, 2, 3],
29+
"y": [
30+
[1, 2.2, 3],
31+
[1.5, 2.7, 3.5],
32+
[1.7, 2.9, 3.7]
33+
],
34+
"cheaterslope": 1,
35+
36+
"xaxis": "x2",
37+
"yaxis": "y2"
38+
},
39+
{
40+
"type": "scattercarpet",
41+
"carpet": "carpet1",
42+
"name": "b = 1.5",
43+
"mode": "markers+text",
44+
"a": [0.1, 0.15, 0.25, 0.3],
45+
"b": [1.5, 1.5, 1.5, 1.5],
46+
47+
"text": ["a", "b", "c", "d"],
48+
"texttemplate": "%{text}: (%{a}, %{b})",
49+
"textposition": "top center",
50+
"xaxis": "x2",
51+
"yaxis": "y2"
52+
}, {
53+
"type": "scatterpolar",
54+
"mode": "markers+text",
55+
"text": ["A", "B", "C", "D"],
56+
"texttemplate": "%{text}: (%{r:0.2f},%{theta:0.2f})",
57+
"textposition": "top center",
58+
"r": [1, 0.5, 1, 1.5],
59+
"theta": [0, 90, 180, 270],
60+
"showgrid": false
61+
}, {
62+
"type": "scatterternary",
63+
"a": [
64+
3,
65+
2,
66+
5
67+
],
68+
"b": [
69+
2,
70+
5,
71+
2
72+
],
73+
"c": [
74+
5,
75+
2,
76+
2
77+
],
78+
"mode": "markers+text",
79+
"text": ["A", "B", "C"],
80+
"texttemplate": "%{text}<br>(%{a:.2f}, %{b:.2f}, %{c:.2f})",
81+
"textposition": "bottom center"
82+
}
83+
],
84+
"layout": {
85+
"showlegend": false,
86+
"width": 800,
87+
"height": 800,
88+
"margin": {
89+
"t": 50,
90+
"b": 50,
91+
"l": 50,
92+
"r": 50
93+
},
94+
"geo": {
95+
"scope": "north america",
96+
"domain": {
97+
"row": 1,
98+
"column": 0
99+
},
100+
"lonaxis": {
101+
"range": [
102+
-130,
103+
-55
104+
]
105+
},
106+
"lataxis": {
107+
"range": [
108+
40,
109+
70
110+
]
111+
},
112+
"center": {
113+
"lat": 57
114+
}
115+
},
116+
"polar": {
117+
"radialaxis": {
118+
"showline": false,
119+
"linewidth": 0,
120+
"tickwidth": 2,
121+
"gridcolor": "white",
122+
"gridwidth": 0
123+
}
124+
},
125+
"ternary": {
126+
"sum": 10,
127+
"domain": {
128+
"row": 1,
129+
"column": 1
130+
}
131+
},
132+
"grid": {
133+
"rows": 2,
134+
"columns": 2,
135+
"pattern": "independent",
136+
"xgap": 5,
137+
"ygap": 5
138+
}
139+
}
140+
}

test/jasmine/tests/carpet_test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var failTest = require('../assets/fail_test');
1313

1414
var mouseEvent = require('../assets/mouse_event');
1515
var assertHoverLabelContent = require('../assets/custom_assertions').assertHoverLabelContent;
16+
var checkTextTemplate = require('../assets/check_texttemplate');
1617

1718
var supplyAllDefaults = require('../assets/supply_defaults');
1819

@@ -714,6 +715,24 @@ describe('scattercarpet hover labels', function() {
714715
});
715716
});
716717

718+
describe('scattercarpet texttemplates', function() {
719+
checkTextTemplate([{
720+
'type': 'scattercarpet',
721+
'carpet': 'carpet1',
722+
'mode': 'markers+text',
723+
'a': [0.1, 0.15, 0.25],
724+
'b': [1.5, 1.5, 1.5],
725+
'text': ['A', 'B', 'C']
726+
}, {
727+
'type': 'carpet',
728+
'carpet': 'carpet1',
729+
'a': [0.1, 0.2, 0.3],
730+
'b': [1, 2, 3],
731+
'y': [[1, 2.2, 3], [1.5, 2.7, 3.5], [1.7, 2.9, 3.7]]
732+
}], 'g.textpoint', [
733+
['%{text}: %{a:0.1f}, %{b:0.1f}', ['A: 0.1, 1.5', 'B: 0.1, 1.5', 'C: 0.3, 1.5']]
734+
]);
735+
});
717736
describe('contourcarpet plotting & editing', function() {
718737
var gd;
719738

test/jasmine/tests/scattergeo_test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var mouseEvent = require('../assets/mouse_event');
1212
var customAssertions = require('../assets/custom_assertions');
1313
var assertHoverLabelStyle = customAssertions.assertHoverLabelStyle;
1414
var assertHoverLabelContent = customAssertions.assertHoverLabelContent;
15+
var checkTextTemplate = require('../assets/check_texttemplate');
1516
var failTest = require('../assets/fail_test');
1617
var supplyAllDefaults = require('../assets/supply_defaults');
1718

@@ -436,3 +437,15 @@ describe('scattergeo drawing', function() {
436437
.then(done);
437438
});
438439
});
440+
441+
describe('Test scattergeo texttemplate:', function() {
442+
checkTextTemplate([{
443+
'type': 'scattergeo',
444+
'mode': 'markers+text',
445+
'lon': [-73.57, -79.24, -123.06],
446+
'lat': [45.5, 43.4, 49.13],
447+
'text': ['Montreal', 'Toronto', 'Vancouver']
448+
}], '.scattergeo text', [
449+
['%{text}: %{lonlat[0]}, %{lonlat[1]}', ['Montreal: -73.57, 45.5', 'Toronto: -79.24, 43.4', 'Vancouver: -123.06, 49.13']]
450+
]);
451+
});

test/jasmine/tests/scatterpolar_test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var mouseEvent = require('../assets/mouse_event');
99

1010
var customAssertions = require('../assets/custom_assertions');
1111
var assertHoverLabelContent = customAssertions.assertHoverLabelContent;
12+
var checkTextTemplate = require('../assets/check_texttemplate');
1213

1314
describe('Test scatterpolar trace defaults:', function() {
1415
var traceOut;
@@ -198,3 +199,17 @@ describe('Test scatterpolar hover:', function() {
198199
});
199200
});
200201
});
202+
203+
describe('Test scatterpolar texttemplate:', function() {
204+
checkTextTemplate([{
205+
'type': 'scatterpolar',
206+
'mode': 'markers+text',
207+
'text': ['A', 'B', 'C'],
208+
'textposition': 'top center',
209+
'r': [1, 0.5, 1],
210+
'theta': [0, 90, 180],
211+
}], 'g.textpoint', [
212+
['%{text}: (%{r:0.2f}, %{theta:0.2f})', ['A: (1.00, 0.00)', 'B: (0.50, 1.57)', 'C: (1.00, 3.14)']],
213+
[['', '', '%{theta:0.2f}'], ['', '', '3.14']]
214+
]);
215+
});

test/jasmine/tests/scatterternary_test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var supplyAllDefaults = require('../assets/supply_defaults');
1111

1212
var mouseEvent = require('../assets/mouse_event');
1313
var assertHoverLabelContent = customAssertions.assertHoverLabelContent;
14+
var checkTextTemplate = require('../assets/check_texttemplate');
1415

1516
var assertClip = customAssertions.assertClip;
1617
var assertNodeDisplay = customAssertions.assertNodeDisplay;
@@ -554,3 +555,16 @@ describe('Test scatterternary *cliponaxis*', function() {
554555
.then(done);
555556
});
556557
});
558+
559+
describe('Test scattergeo texttemplate:', function() {
560+
checkTextTemplate([{
561+
'type': 'scatterternary',
562+
'a': [3, 2, 5],
563+
'b': [2, 5, 2],
564+
'c': [5, 2, 2 ],
565+
'mode': 'markers+text',
566+
'text': ['A', 'B', 'C']
567+
}], 'g.textpoint', [
568+
['%{text} (%{a:.2f}, %{b:.2f}, %{c:.2f})', ['A (0.30, 0.20, 0.50)', 'B (0.22, 0.56, 0.22)', 'C (0.56, 0.22, 0.22)']]
569+
]);
570+
});

0 commit comments

Comments
 (0)