Skip to content

Commit acbd0de

Browse files
committed
fix texttemplate formatting for scattergl and scatterpolargl
... by adding a formatLabels method. The result is a bit hackier than for the SVG trace types as we have to juggle with calcdata mocks. Note also that text templates are formatting during the calc step for scattergl and scatterpolargl which may cause some issues down the road. I didn't notice anything in this iteration, but we should probably move the text templates formatting down to the plot step down the road.
1 parent 493ee75 commit acbd0de

File tree

9 files changed

+121
-26
lines changed

9 files changed

+121
-26
lines changed

Diff for: src/traces/scattergl/calc.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ module.exports = function calc(gd, trace) {
3636
var stash = {};
3737
var i, xx, yy;
3838

39-
var x = xa.makeCalcdata(trace, 'x');
40-
var y = ya.makeCalcdata(trace, 'y');
39+
var x = trace._x = xa.makeCalcdata(trace, 'x');
40+
var y = trace._y = ya.makeCalcdata(trace, 'y');
4141

4242
// we need hi-precision for scatter2d,
4343
// regl-scatter2d uses NaNs for bad/missing values

Diff for: src/traces/scattergl/convert.js

+20-11
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ function convertStyle(gd, trace) {
4949
if(trace.visible !== true) return opts;
5050

5151
if(subTypes.hasText(trace)) {
52-
opts.text = convertTextStyle(trace, gd);
53-
opts.textSel = convertTextSelection(trace, trace.selected);
54-
opts.textUnsel = convertTextSelection(trace, trace.unselected);
52+
opts.text = convertTextStyle(gd, trace);
53+
opts.textSel = convertTextSelection(gd, trace, trace.selected);
54+
opts.textUnsel = convertTextSelection(gd, trace, trace.unselected);
5555
}
5656

5757
if(subTypes.hasMarkers(trace)) {
@@ -102,7 +102,8 @@ function convertStyle(gd, trace) {
102102
return opts;
103103
}
104104

105-
function convertTextStyle(trace, gd) {
105+
function convertTextStyle(gd, trace) {
106+
var fullLayout = gd._fullLayout;
106107
var count = trace._length;
107108
var textfontIn = trace.textfont;
108109
var textpositionIn = trace.textposition;
@@ -116,14 +117,21 @@ function convertTextStyle(trace, gd) {
116117
var texttemplate = trace.texttemplate;
117118
if(texttemplate) {
118119
optsOut.text = [];
120+
121+
var d3locale = fullLayout._d3locale;
119122
var isArray = Array.isArray(texttemplate);
120123
var N = isArray ? Math.min(texttemplate.length, count) : count;
121-
var txt = isArray ? function(i) {return texttemplate[i];} : function() { return texttemplate;};
122-
var d3locale = gd._fullLayout._d3locale;
124+
var txt = isArray ?
125+
function(i) { return texttemplate[i]; } :
126+
function() { return texttemplate; };
127+
123128
for(i = 0; i < N; i++) {
124-
var pt = {};
125-
appendArrayPointValue(pt, trace, i);
126-
optsOut.text.push(Lib.texttemplateString(txt(i), pt, d3locale, pt, trace._meta || {}));
129+
var d = {i: i};
130+
var labels = trace._module.formatLabels(d, trace, fullLayout);
131+
var pointValues = {};
132+
appendArrayPointValue(pointValues, trace, i);
133+
var meta = trace._meta || {};
134+
optsOut.text.push(Lib.texttemplateString(txt(i), labels, d3locale, pointValues, d, meta));
127135
}
128136
} else {
129137
if(Array.isArray(trace.text) && trace.text.length < count) {
@@ -339,7 +347,7 @@ function convertMarkerSelection(trace, target) {
339347
return optsOut;
340348
}
341349

342-
function convertTextSelection(trace, target) {
350+
function convertTextSelection(gd, trace, target) {
343351
var optsOut = {};
344352

345353
if(!target) return optsOut;
@@ -348,13 +356,14 @@ function convertTextSelection(trace, target) {
348356
var optsIn = {
349357
opacity: 1,
350358
text: trace.text,
359+
texttemplate: trace.texttemplate,
351360
textposition: trace.textposition,
352361
textfont: Lib.extendFlat({}, trace.textfont)
353362
};
354363
if(target.textfont) {
355364
Lib.extendFlat(optsIn.textfont, target.textfont);
356365
}
357-
optsOut = convertTextStyle(optsIn);
366+
optsOut = convertTextStyle(gd, optsIn);
358367
}
359368

360369
return optsOut;

Diff for: src/traces/scattergl/format_labels.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Copyright 2012-2019, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
'use strict';
10+
11+
var scatterFormatLabels = require('../scatter/format_labels');
12+
13+
module.exports = function formatLabels(cdi, trace, fullLayout) {
14+
var i = cdi.i;
15+
if(!('x' in cdi)) cdi.x = trace._x[i];
16+
if(!('y' in cdi)) cdi.y = trace._y[i];
17+
return scatterFormatLabels(cdi, trace, fullLayout);
18+
};

Diff for: src/traces/scattergl/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module.exports = {
2020
supplyDefaults: require('./defaults'),
2121
crossTraceDefaults: require('../scatter/cross_trace_defaults'),
2222
colorbar: require('../scatter/marker_colorbar'),
23+
formatLabels: require('./format_labels'),
2324
calc: require('./calc'),
2425
plot: require('./plot'),
2526
hoverPoints: hover.hoverPoints,

Diff for: src/traces/scatterpolargl/calc.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ module.exports = function calc(gd, trace) {
1919
var subplotId = trace.subplot;
2020
var radialAxis = fullLayout[subplotId].radialaxis;
2121
var angularAxis = fullLayout[subplotId].angularaxis;
22-
var rArray = radialAxis.makeCalcdata(trace, 'r');
23-
var thetaArray = angularAxis.makeCalcdata(trace, 'theta');
22+
var rArray = trace._r = radialAxis.makeCalcdata(trace, 'r');
23+
var thetaArray = trace._theta = angularAxis.makeCalcdata(trace, 'theta');
2424
var len = trace._length;
2525
var stash = {};
2626

Diff for: src/traces/scatterpolargl/format_labels.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Copyright 2012-2019, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
'use strict';
10+
11+
var scatterPolarFormatLabels = require('../scatterpolar/format_labels');
12+
13+
module.exports = function formatLabels(cdi, trace, fullLayout) {
14+
var i = cdi.i;
15+
if(!('r' in cdi)) cdi.r = trace._r[i];
16+
if(!('theta' in cdi)) cdi.theta = trace._theta[i];
17+
return scatterPolarFormatLabels(cdi, trace, fullLayout);
18+
};

Diff for: src/traces/scatterpolargl/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module.exports = {
1717
attributes: require('./attributes'),
1818
supplyDefaults: require('./defaults'),
1919
colorbar: require('../scatter/marker_colorbar'),
20+
formatLabels: require('./format_labels'),
2021

2122
calc: require('./calc'),
2223
plot: require('./plot'),

Diff for: test/jasmine/tests/scattergl_test.js

+42-11
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,6 @@ describe('end-to-end scattergl tests', function() {
3939
}).catch(failTest).then(done);
4040
});
4141

42-
checkTextTemplate([{
43-
type: 'scattergl',
44-
mode: 'text+lines',
45-
x: [1, 2, 3, 4],
46-
y: [2, 3, 4, 5],
47-
text: ['A', 'B', 'C', 'D'],
48-
}], '@gl', [
49-
['%{text}: %{x}, %{y}', ['A: 1, 2', 'B: 2, 3', 'C: 3, 4', 'D: 4, 5']],
50-
[['%{x}', '%{x}', '%{text}', '%{y}'], ['1', '2', 'C', '5']]
51-
]);
52-
5342
it('@gl should update a plot with text labels', function(done) {
5443
Plotly.react(gd, [{
5544
type: 'scattergl',
@@ -761,3 +750,45 @@ describe('Test scattergl autorange:', function() {
761750
});
762751
});
763752
});
753+
754+
describe('Test texttemplate for scattergl', function() {
755+
checkTextTemplate([{
756+
type: 'scattergl',
757+
mode: 'text+lines',
758+
x: [1, 2, 3, 4],
759+
y: [2, 3, 4, 5],
760+
text: ['A', 'B', 'C', 'D'],
761+
}], '@gl', [
762+
['%{text}: %{x}, %{y}', ['A: 1, 2', 'B: 2, 3', 'C: 3, 4', 'D: 4, 5']],
763+
[['%{x}', '%{x}', '%{text}', '%{y}'], ['1', '2', 'C', '5']]
764+
]);
765+
766+
checkTextTemplate({
767+
data: [{
768+
type: 'scattergl',
769+
mode: 'text',
770+
x: ['a', 'b'],
771+
y: ['1000', '1200']
772+
}],
773+
layout: {
774+
xaxis: { tickprefix: '*', ticksuffix: '*' },
775+
yaxis: { tickprefix: '$', ticksuffix: ' !', tickformat: '.2f'}
776+
}
777+
}, '@gl', [
778+
['%{x} is %{y}', ['*a* is $1000.00 !', '*b* is $1200.00 !']]
779+
]);
780+
781+
checkTextTemplate({
782+
data: [{
783+
type: 'scattergl',
784+
mode: 'text',
785+
y: ['1000', '1200']
786+
}],
787+
layout: {
788+
xaxis: { tickprefix: '*', ticksuffix: '*' },
789+
yaxis: { tickprefix: '$', ticksuffix: ' !', tickformat: '.2f'}
790+
}
791+
}, '@gl', [
792+
['%{x} is %{y}', ['*0* is $1000.00 !', '*1* is $1200.00 !']]
793+
]);
794+
});

Diff for: test/jasmine/tests/scatterpolargl_test.js

+17
Original file line numberDiff line numberDiff line change
@@ -391,4 +391,21 @@ describe('Test scatterpolargl texttemplate:', function() {
391391
['%{text}: (%{r:0.2f}, %{theta:0.1f})', ['A: (1.00, 0.0)', 'B: (0.50, 90.0)', 'C: (1.00, 180.0)']],
392392
[['', 'b%{theta:0.2f}', '%{theta:0.2f}'], ['', 'b90.00', '180.00']]
393393
]);
394+
395+
checkTextTemplate({
396+
data: [{
397+
type: 'scatterpolargl',
398+
mode: 'text',
399+
theta: ['a', 'b'],
400+
r: ['1000', '1200']
401+
}],
402+
layout: {
403+
polar: {
404+
radialaxis: { tickprefix: '$', ticksuffix: ' !', tickformat: '.2f'},
405+
angularaxis: { tickprefix: '*', ticksuffix: '*' }
406+
}
407+
}
408+
}, '.textpoint', [
409+
['%{theta} is %{r}', ['*a* is $1000.00 !', '*b* is $1200.00 !']]
410+
]);
394411
});

0 commit comments

Comments
 (0)