Skip to content

Commit 2d50269

Browse files
committed
Merge branch 'texttemplate-gl' into texttemplate
2 parents 3ad30ab + f9062e4 commit 2d50269

File tree

14 files changed

+311
-9
lines changed

14 files changed

+311
-9
lines changed

src/lib/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ function templateFormatString(string, labels, d3locale) {
10671067

10681068
if(format) {
10691069
var fmt;
1070-
if(d3locale) {
1070+
if(d3locale && d3locale.numberFormat) {
10711071
fmt = d3locale.numberFormat;
10721072
} else {
10731073
fmt = d3.format;

src/traces/scatter3d/attributes.js

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
var scatterAttrs = require('../scatter/attributes');
1212
var colorAttributes = require('../../components/colorscale/attributes');
1313
var hovertemplateAttrs = require('../../components/fx/hovertemplate_attributes');
14+
var texttemplateAttrs = require('../../plots/texttemplate_attributes');
1415
var baseAttrs = require('../../plots/attributes');
1516
var DASHES = require('../../constants/gl3d_dashes');
1617

@@ -84,6 +85,9 @@ var attrs = module.exports = overrideAll({
8485
'If trace `hoverinfo` contains a *text* flag and *hovertext* is not set,',
8586
'these elements will be seen in the hover labels.'
8687
].join(' ')
88+
}),
89+
texttemplate: texttemplateAttrs({arrayOk: true}, {
90+
8791
}),
8892
hovertext: extendFlat({}, scatterAttrs.hovertext, {
8993
description: [

src/traces/scatter3d/convert.js

+23
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ var makeBubbleSizeFn = require('../scatter/make_bubble_size_func');
2222
var DASH_PATTERNS = require('../../constants/gl3d_dashes');
2323
var MARKER_SYMBOLS = require('../../constants/gl3d_markers');
2424

25+
var appendArrayPointValue = require('../../components/fx/helpers').appendArrayPointValue;
26+
2527
var calculateError = require('./calc_errors');
2628

2729
function LineWithMarkers(scene, uid) {
@@ -239,6 +241,27 @@ function convertPlotlyOptions(scene, data) {
239241
for(i = 0; i < len; i++) text[i] = data.text;
240242
}
241243

244+
// check texttemplate
245+
if(data.texttemplate) {
246+
if(Array.isArray(data.texttemplate)) {
247+
text = new Array(data.texttemplate.length);
248+
for(i = 0; i < data.texttemplate.length; i++) {
249+
var pt = {};
250+
pt.text = text[i];
251+
appendArrayPointValue(pt, data, i);
252+
text[i] = Lib.texttemplateString(data.texttemplate[i], pt, function() {}, pt);
253+
}
254+
} else {
255+
text = new Array(len);
256+
for(i = 0; i < len; i++) {
257+
var pt1 = {};
258+
pt1.text = text[i];
259+
appendArrayPointValue(pt1, data, i);
260+
text[i] = Lib.texttemplateString(data.texttemplate, pt1, function() {}, pt1);
261+
}
262+
}
263+
}
264+
242265
// Build object parameters
243266
params = {
244267
position: points,

src/traces/scatter3d/defaults.js

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
3131
}
3232

3333
coerce('text');
34+
coerce('texttemplate');
3435
coerce('hovertext');
3536
coerce('hovertemplate');
3637
coerce('mode');

src/traces/scattergl/attributes.js

+1
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,4 @@ var attrs = module.exports = overrideAll({
9898

9999
attrs.x.editType = attrs.y.editType = attrs.x0.editType = attrs.y0.editType = 'calc+clearAxisTypes';
100100
attrs.hovertemplate = scatterAttrs.hovertemplate;
101+
attrs.texttemplate = scatterAttrs.texttemplate;

src/traces/scattergl/convert.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ var TEXTOFFSETSIGN = {
2828
start: 1, left: 1, end: -1, right: -1, middle: 0, center: 0, bottom: 1, top: -1
2929
};
3030

31+
var appendArrayPointValue = require('../../components/fx/helpers').appendArrayPointValue;
32+
3133
function convertStyle(gd, trace) {
3234
var i;
3335

@@ -111,7 +113,26 @@ function convertTextStyle(trace) {
111113
var optsOut = {};
112114
var i;
113115

114-
optsOut.text = trace.text;
116+
var texttemplate = trace.texttemplate;
117+
if(texttemplate) {
118+
optsOut.text = [];
119+
var pt;
120+
if(Array.isArray(texttemplate)) {
121+
for(i = 0; i < texttemplate.length; i++) {
122+
pt = {};
123+
appendArrayPointValue(pt, trace, i);
124+
optsOut.text.push(Lib.texttemplateString(texttemplate[i], pt, function() {}, pt));
125+
}
126+
} else {
127+
for(i = 0; i < count; i++) {
128+
pt = {};
129+
appendArrayPointValue(pt, trace, i);
130+
optsOut.text.push(Lib.texttemplateString(texttemplate, pt, function() {}, pt));
131+
}
132+
}
133+
} else {
134+
optsOut.text = trace.text;
135+
}
115136
optsOut.opacity = trace.opacity;
116137
optsOut.font = {};
117138
optsOut.align = [];

src/traces/scattergl/defaults.js

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
3636
var defaultMode = len < constants.PTS_LINESONLY ? 'lines+markers' : 'lines';
3737

3838
coerce('text');
39+
coerce('texttemplate');
3940
coerce('hovertext');
4041
coerce('hovertemplate');
4142
coerce('mode', defaultMode);

src/traces/scatterpolargl/attributes.js

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
var scatterPolarAttrs = require('../scatterpolar/attributes');
1212
var scatterGlAttrs = require('../scattergl/attributes');
13+
var texttemplateAttrs = require('../../plots/texttemplate_attributes');
1314

1415
module.exports = {
1516
mode: scatterPolarAttrs.mode,
@@ -22,6 +23,9 @@ module.exports = {
2223
thetaunit: scatterPolarAttrs.thetaunit,
2324

2425
text: scatterPolarAttrs.text,
26+
texttemplate: texttemplateAttrs({editType: 'plot'}, {
27+
keys: ['r', 'theta', 'text']
28+
}),
2529
hovertext: scatterPolarAttrs.hovertext,
2630
hovertemplate: scatterPolarAttrs.hovertemplate,
2731

src/traces/scatterpolargl/defaults.js

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ module.exports = 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

97.2 KB
Loading

test/image/mocks/texttemplate_2.json

+193
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
{
2+
"data": [{
3+
"type": "scatter3d",
4+
"x": [0, 1, 2],
5+
"y": [0, 2, 1],
6+
"z": [-5, -2, 4],
7+
"texttemplate": "%{x}, %{y}, %{z}",
8+
"mode": "markers+text"
9+
}, {
10+
"type": "scattergeo",
11+
"mode": "markers+text",
12+
"lon": [
13+
-73.57,
14+
-79.24,
15+
-123.06
16+
],
17+
"lat": [
18+
45.5,
19+
43.4,
20+
49.13
21+
],
22+
"text": [
23+
"Montreal",
24+
"Toronto",
25+
"Vancouver"
26+
],
27+
"texttemplate": "%{text} (%{lonlat[0]}, %{lonlat[1]}): %{customdata:.2s}",
28+
"textposition": "top center",
29+
"customdata": [1780000, 2930000, 675218],
30+
"geo": "geo"
31+
}, {
32+
"type": "carpet",
33+
"carpet": "carpet1",
34+
"a": [0.1, 0.2, 0.3],
35+
"b": [1, 2, 3],
36+
"y": [
37+
[1, 2.2, 3],
38+
[1.5, 2.7, 3.5],
39+
[1.7, 2.9, 3.7]
40+
],
41+
"cheaterslope": 1,
42+
43+
"xaxis": "x2",
44+
"yaxis": "y2"
45+
},
46+
{
47+
"type": "scattercarpet",
48+
"carpet": "carpet1",
49+
"name": "b = 1.5",
50+
"mode": "markers+text",
51+
"a": [0.1, 0.15, 0.25, 0.3],
52+
"b": [1.5, 1.5, 1.5, 1.5],
53+
54+
"text": ["a", "b", "c", "d"],
55+
"texttemplate": "%{text}: (%{a}, %{b})",
56+
"textposition": "top center",
57+
"xaxis": "x2",
58+
"yaxis": "y2"
59+
}, {
60+
"type": "scattergl",
61+
"mode": "markers+text",
62+
"x": [0, 1, 2, 3],
63+
"y": [0, 1, 4, 9],
64+
65+
"text": ["a", "b", "c", "d"],
66+
"texttemplate": "%{text}: (%{x}, %{y})",
67+
"textposition": "top center",
68+
"xaxis": "x8",
69+
"yaxis": "y8"
70+
}, {
71+
"type": "scatterpolar",
72+
"mode": "markers+text",
73+
"text": ["A", "B", "C", "D"],
74+
"texttemplate": "%{text}: (%{r:0.2f},%{theta:0.2f})",
75+
"textposition": "top center",
76+
"r": [1, 0.5, 1, 1.5],
77+
"theta": [0, 90, 180, 270],
78+
"showgrid": false
79+
}, {
80+
"type": "scatterpolargl",
81+
"mode": "markers+text",
82+
"text": ["A", "B", "C", "D"],
83+
"texttemplate": "%{text}: (%{r:0.2f},%{theta:0.2f})",
84+
"textposition": "top center",
85+
"r": [1, 0.5, 1, 1.5],
86+
"theta": [0, 90, 180, 270],
87+
"showgrid": false,
88+
"subplot": "polar2"
89+
}, {
90+
"type": "scatterternary",
91+
"a": [
92+
3,
93+
2,
94+
5
95+
],
96+
"b": [
97+
2,
98+
5,
99+
2
100+
],
101+
"c": [
102+
5,
103+
2,
104+
2
105+
],
106+
"mode": "markers+text",
107+
"text": ["A", "B", "C"],
108+
"texttemplate": "%{text}<br>(%{a:.2f}, %{b:.2f}, %{c:.2f})",
109+
"textposition": "bottom center"
110+
}
111+
],
112+
"layout": {
113+
"showlegend": false,
114+
"width": 1000,
115+
"height": 500,
116+
"margin": {
117+
"t": 50,
118+
"b": 50,
119+
"l": 50,
120+
"r": 50
121+
},
122+
"geo": {
123+
"scope": "north america",
124+
"domain": {
125+
"row": 0,
126+
"column": 2
127+
},
128+
"lonaxis": {
129+
"range": [
130+
-130,
131+
-55
132+
]
133+
},
134+
"lataxis": {
135+
"range": [
136+
40,
137+
70
138+
]
139+
},
140+
"center": {
141+
"lat": 57
142+
}
143+
},
144+
"mapbox": {
145+
"domain": {"row": 1, "column": 0},
146+
"center": {"lon": -90, "lat": 45},
147+
"zoom": 0.7,
148+
"style": "white-bg"
149+
},
150+
"polar": {
151+
"radialaxis": {
152+
"showline": false,
153+
"linewidth": 0,
154+
"tickwidth": 2,
155+
"gridcolor": "white",
156+
"gridwidth": 0
157+
}
158+
},
159+
"polar2": {
160+
"domain": {
161+
"row": 1,
162+
"column": 2
163+
},
164+
"radialaxis": {
165+
"showline": false,
166+
"linewidth": 0,
167+
"tickwidth": 2,
168+
"gridcolor": "white",
169+
"gridwidth": 0
170+
}
171+
},
172+
"ternary": {
173+
"sum": 10,
174+
"domain": {
175+
"row": 0,
176+
"column": 3
177+
}
178+
},
179+
"scene": {
180+
"domain": {"row": 1, "column": 1}
181+
},
182+
"scene1": {
183+
"domain": {"row": 1, "column": 3}
184+
},
185+
"grid": {
186+
"rows": 2,
187+
"columns": 4,
188+
"pattern": "independent",
189+
"xgap": 5,
190+
"ygap": 5
191+
}
192+
}
193+
}

0 commit comments

Comments
 (0)