Skip to content

Commit 76d8a11

Browse files
committed
add formatLabels module method for scatter{polar,geo,ternary,carpet}
... in an effort to bring texttemplate and hover coordinates formatting to the same place.
1 parent ac978e5 commit 76d8a11

File tree

15 files changed

+163
-30
lines changed

15 files changed

+163
-30
lines changed

src/traces/barpolar/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module.exports = {
2424

2525
plot: require('./plot'),
2626
colorbar: require('../scatter/marker_colorbar'),
27+
formatLabels: require('../scatterpolar/format_labels'),
2728

2829
style: require('../bar/style').style,
2930
styleOnSelect: require('../bar/style').styleOnSelect,

src/traces/scatter/format_labels.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 Axes = require('../../plots/cartesian/axes');
12+
13+
module.exports = function formatLabels(cdi, trace, fullLayout) {
14+
var labels = {};
15+
16+
var mockGd = {_fullLayout: fullLayout};
17+
var xa = Axes.getFromTrace(mockGd, trace, 'x');
18+
var ya = Axes.getFromTrace(mockGd, trace, 'y');
19+
20+
labels.xLabel = Axes.tickText(xa, cdi.x, true).text;
21+
labels.yLabel = Axes.tickText(ya, cdi.y, true).text;
22+
23+
return labels;
24+
};

src/traces/scatter/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module.exports = {
2424
arraysToCalcdata: require('./arrays_to_calcdata'),
2525
plot: require('./plot'),
2626
colorbar: require('./marker_colorbar'),
27+
formatLabels: require('./format_labels'),
2728
style: require('./style').style,
2829
styleOnSelect: require('./style').styleOnSelect,
2930
hoverPoints: require('./hover'),
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
module.exports = function formatLabels(cdi, trace) {
12+
var labels = {};
13+
14+
var carpet = trace._carpet;
15+
var ij = carpet.ab2ij([cdi.a, cdi.b]);
16+
var i0 = Math.floor(ij[0]);
17+
var ti = ij[0] - i0;
18+
var j0 = Math.floor(ij[1]);
19+
var tj = ij[1] - j0;
20+
var xy = carpet.evalxy([], i0, j0, ti, tj);
21+
22+
labels.yLabel = xy[1].toFixed(3);
23+
24+
return labels;
25+
};

src/traces/scattercarpet/hover.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,8 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
4949
var trace = newPointData.trace;
5050
var carpet = trace._carpet;
5151

52-
var ij = carpet.ab2ij([cdi.a, cdi.b]);
53-
var i0 = Math.floor(ij[0]);
54-
var ti = ij[0] - i0;
55-
var j0 = Math.floor(ij[1]);
56-
var tj = ij[1] - j0;
57-
var xy = carpet.evalxy([], i0, j0, ti, tj);
58-
newPointData.yLabel = xy[1].toFixed(3);
52+
var labels = trace._module.formatLabels(cdi, trace);
53+
newPointData.yLabel = labels.yLabel;
5954

6055
delete newPointData.text;
6156
var text = [];

src/traces/scattercarpet/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module.exports = {
1212
attributes: require('./attributes'),
1313
supplyDefaults: require('./defaults'),
1414
colorbar: require('../scatter/marker_colorbar'),
15+
formatLabels: require('./format_labels'),
1516
calc: require('./calc'),
1617
plot: require('./plot'),
1718
style: require('../scatter/style').style,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 Axes = require('../../plots/cartesian/axes');
12+
13+
module.exports = function formatLabels(cdi, trace, fullLayout) {
14+
var labels = {};
15+
16+
var geo = fullLayout[trace.geo]._subplot;
17+
var ax = geo.mockAxis;
18+
var lonlat = cdi.lonlat;
19+
labels.lonLabel = Axes.tickText(ax, ax.c2l(lonlat[0]), true).text;
20+
labels.latLabel = Axes.tickText(ax, ax.c2l(lonlat[1]), true).text;
21+
22+
return labels;
23+
};

src/traces/scattergeo/hover.js

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

1111
var Fx = require('../../components/fx');
12-
var Axes = require('../../plots/cartesian/axes');
1312
var BADNUM = require('../../constants/numerical').BADNUM;
1413

1514
var getTraceColor = require('../scatter/get_trace_color');
@@ -63,9 +62,11 @@ module.exports = function hoverPoints(pointData, xval, yval) {
6362
pointData.lon = lonlat[0];
6463
pointData.lat = lonlat[1];
6564

66-
var ax = geo.mockAxis;
67-
pointData.lonLabel = Axes.tickText(ax, ax.c2l(pointData.lon), 'hover').text;
68-
pointData.latLabel = Axes.tickText(ax, ax.c2l(pointData.lat), 'hover').text;
65+
var fullLayout = {};
66+
fullLayout[trace.geo] = {_subplot: geo};
67+
var labels = trace._module.formatLabels(di, trace, fullLayout);
68+
pointData.lonLabel = labels.lonLabel;
69+
pointData.latLabel = labels.latLabel;
6970

7071
pointData.color = getTraceColor(trace, di);
7172
pointData.extraText = getExtraText(trace, di, pointData, cd[0].t.labels);

src/traces/scattergeo/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module.exports = {
1212
attributes: require('./attributes'),
1313
supplyDefaults: require('./defaults'),
1414
colorbar: require('../scatter/marker_colorbar'),
15+
formatLabels: require('./format_labels'),
1516
calc: require('./calc'),
1617
plot: require('./plot'),
1718
style: require('./style'),
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 Lib = require('../../lib');
12+
var Axes = require('../../plots/cartesian/axes');
13+
14+
module.exports = function formatLabels(cdi, trace, fullLayout) {
15+
var labels = {};
16+
17+
var subplot = fullLayout[trace.subplot]._subplot;
18+
var radialAxis;
19+
var angularAxis;
20+
21+
// for scatterpolargl texttemplate, _subplot is NOT defined, this takes part during the convert step
22+
// TODO we should consider moving the texttemplate formatting logic to the plot step
23+
if(!subplot) {
24+
subplot = fullLayout[trace.subplot];
25+
radialAxis = subplot.radialaxis;
26+
angularAxis = subplot.angularaxis;
27+
} else {
28+
radialAxis = subplot.radialAxis;
29+
angularAxis = subplot.angularAxis;
30+
}
31+
32+
var rVal = radialAxis.c2l(cdi.r);
33+
labels.rLabel = Axes.tickText(radialAxis, rVal, true).text;
34+
35+
// N.B here the ° sign is part of the formatted value for thetaunit:'degrees'
36+
var thetaVal = angularAxis.thetaunit === 'degrees' ? Lib.rad2deg(cdi.theta) : cdi.theta;
37+
labels.thetaLabel = Axes.tickText(angularAxis, thetaVal, true).text;
38+
39+
return labels;
40+
};

0 commit comments

Comments
 (0)