Skip to content

Commit

Permalink
add formatLabels module method for scatter{polar,geo,ternary,carpet}
Browse files Browse the repository at this point in the history
...  in an effort to bring texttemplate and hover coordinates
     formatting to the same place.
  • Loading branch information
etpinard committed Nov 22, 2019
1 parent ac978e5 commit 76d8a11
Show file tree
Hide file tree
Showing 15 changed files with 163 additions and 30 deletions.
1 change: 1 addition & 0 deletions src/traces/barpolar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = {

plot: require('./plot'),
colorbar: require('../scatter/marker_colorbar'),
formatLabels: require('../scatterpolar/format_labels'),

style: require('../bar/style').style,
styleOnSelect: require('../bar/style').styleOnSelect,
Expand Down
24 changes: 24 additions & 0 deletions src/traces/scatter/format_labels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright 2012-2019, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

var Axes = require('../../plots/cartesian/axes');

module.exports = function formatLabels(cdi, trace, fullLayout) {
var labels = {};

var mockGd = {_fullLayout: fullLayout};
var xa = Axes.getFromTrace(mockGd, trace, 'x');
var ya = Axes.getFromTrace(mockGd, trace, 'y');

labels.xLabel = Axes.tickText(xa, cdi.x, true).text;
labels.yLabel = Axes.tickText(ya, cdi.y, true).text;

return labels;
};
1 change: 1 addition & 0 deletions src/traces/scatter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = {
arraysToCalcdata: require('./arrays_to_calcdata'),
plot: require('./plot'),
colorbar: require('./marker_colorbar'),
formatLabels: require('./format_labels'),
style: require('./style').style,
styleOnSelect: require('./style').styleOnSelect,
hoverPoints: require('./hover'),
Expand Down
25 changes: 25 additions & 0 deletions src/traces/scattercarpet/format_labels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright 2012-2019, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

module.exports = function formatLabels(cdi, trace) {
var labels = {};

var carpet = trace._carpet;
var ij = carpet.ab2ij([cdi.a, cdi.b]);
var i0 = Math.floor(ij[0]);
var ti = ij[0] - i0;
var j0 = Math.floor(ij[1]);
var tj = ij[1] - j0;
var xy = carpet.evalxy([], i0, j0, ti, tj);

labels.yLabel = xy[1].toFixed(3);

return labels;
};
9 changes: 2 additions & 7 deletions src/traces/scattercarpet/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,8 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
var trace = newPointData.trace;
var carpet = trace._carpet;

var ij = carpet.ab2ij([cdi.a, cdi.b]);
var i0 = Math.floor(ij[0]);
var ti = ij[0] - i0;
var j0 = Math.floor(ij[1]);
var tj = ij[1] - j0;
var xy = carpet.evalxy([], i0, j0, ti, tj);
newPointData.yLabel = xy[1].toFixed(3);
var labels = trace._module.formatLabels(cdi, trace);
newPointData.yLabel = labels.yLabel;

delete newPointData.text;
var text = [];
Expand Down
1 change: 1 addition & 0 deletions src/traces/scattercarpet/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = {
attributes: require('./attributes'),
supplyDefaults: require('./defaults'),
colorbar: require('../scatter/marker_colorbar'),
formatLabels: require('./format_labels'),
calc: require('./calc'),
plot: require('./plot'),
style: require('../scatter/style').style,
Expand Down
23 changes: 23 additions & 0 deletions src/traces/scattergeo/format_labels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright 2012-2019, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

var Axes = require('../../plots/cartesian/axes');

module.exports = function formatLabels(cdi, trace, fullLayout) {
var labels = {};

var geo = fullLayout[trace.geo]._subplot;
var ax = geo.mockAxis;
var lonlat = cdi.lonlat;
labels.lonLabel = Axes.tickText(ax, ax.c2l(lonlat[0]), true).text;
labels.latLabel = Axes.tickText(ax, ax.c2l(lonlat[1]), true).text;

return labels;
};
9 changes: 5 additions & 4 deletions src/traces/scattergeo/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
'use strict';

var Fx = require('../../components/fx');
var Axes = require('../../plots/cartesian/axes');
var BADNUM = require('../../constants/numerical').BADNUM;

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

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

pointData.color = getTraceColor(trace, di);
pointData.extraText = getExtraText(trace, di, pointData, cd[0].t.labels);
Expand Down
1 change: 1 addition & 0 deletions src/traces/scattergeo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = {
attributes: require('./attributes'),
supplyDefaults: require('./defaults'),
colorbar: require('../scatter/marker_colorbar'),
formatLabels: require('./format_labels'),
calc: require('./calc'),
plot: require('./plot'),
style: require('./style'),
Expand Down
40 changes: 40 additions & 0 deletions src/traces/scatterpolar/format_labels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright 2012-2019, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

var Lib = require('../../lib');
var Axes = require('../../plots/cartesian/axes');

module.exports = function formatLabels(cdi, trace, fullLayout) {
var labels = {};

var subplot = fullLayout[trace.subplot]._subplot;
var radialAxis;
var angularAxis;

// for scatterpolargl texttemplate, _subplot is NOT defined, this takes part during the convert step
// TODO we should consider moving the texttemplate formatting logic to the plot step
if(!subplot) {
subplot = fullLayout[trace.subplot];
radialAxis = subplot.radialaxis;
angularAxis = subplot.angularaxis;
} else {
radialAxis = subplot.radialAxis;
angularAxis = subplot.angularAxis;
}

var rVal = radialAxis.c2l(cdi.r);
labels.rLabel = Axes.tickText(radialAxis, rVal, true).text;

// N.B here the ° sign is part of the formatted value for thetaunit:'degrees'
var thetaVal = angularAxis.thetaunit === 'degrees' ? Lib.rad2deg(cdi.theta) : cdi.theta;
labels.thetaLabel = Axes.tickText(angularAxis, thetaVal, true).text;

return labels;
};
13 changes: 5 additions & 8 deletions src/traces/scatterpolar/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
'use strict';

var scatterHover = require('../scatter/hover');
var Axes = require('../../plots/cartesian/axes');
var Lib = require('../../lib');

function hoverPoints(pointData, xval, yval, hovermode) {
var scatterPointData = scatterHover(pointData, xval, yval, hovermode);
Expand Down Expand Up @@ -42,12 +40,11 @@ function makeHoverPointText(cdi, trace, subplot, pointData) {
radialAxis._hovertitle = 'r';
angularAxis._hovertitle = 'θ';

var rVal = radialAxis.c2l(cdi.r);
pointData.rLabel = Axes.tickText(radialAxis, rVal, 'hover').text;

// N.B here the ° sign is part of the formatted value for thetaunit:'degrees'
var thetaVal = angularAxis.thetaunit === 'degrees' ? Lib.rad2deg(cdi.theta) : cdi.theta;
pointData.thetaLabel = Axes.tickText(angularAxis, thetaVal, 'hover').text;
var fullLayout = {};
fullLayout[trace.subplot] = {_subplot: subplot};
var labels = trace._module.formatLabels(cdi, trace, fullLayout);
pointData.rLabel = labels.rLabel;
pointData.thetaLabel = labels.thetaLabel;

var hoverinfo = cdi.hi || trace.hoverinfo;
var text = [];
Expand Down
1 change: 1 addition & 0 deletions src/traces/scatterpolar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = {
attributes: require('./attributes'),
supplyDefaults: require('./defaults').supplyDefaults,
colorbar: require('../scatter/marker_colorbar'),
formatLabels: require('./format_labels'),
calc: require('./calc'),
plot: require('./plot'),
style: require('../scatter/style').style,
Expand Down
22 changes: 22 additions & 0 deletions src/traces/scatterternary/format_labels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Copyright 2012-2019, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

var Axes = require('../../plots/cartesian/axes');

module.exports = function formatLabels(cdi, trace, fullLayout) {
var labels = {};

var subplot = fullLayout[trace.subplot]._subplot;
labels.aLabel = Axes.tickText(subplot.aaxis, cdi.a, true).text;
labels.bLabel = Axes.tickText(subplot.baxis, cdi.b, true).text;
labels.cLabel = Axes.tickText(subplot.caxis, cdi.c, true).text;

return labels;
};
22 changes: 11 additions & 11 deletions src/traces/scatterternary/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@
* LICENSE file in the root directory of this source tree.
*/


'use strict';

var scatterHover = require('../scatter/hover');
var Axes = require('../../plots/cartesian/axes');


module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
var scatterPointData = scatterHover(pointData, xval, yval, hovermode);
Expand Down Expand Up @@ -40,6 +37,8 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
}

var cdi = newPointData.cd[newPointData.index];
var trace = newPointData.trace;
var subplot = newPointData.subplot;

newPointData.a = cdi.a;
newPointData.b = cdi.b;
Expand All @@ -48,12 +47,13 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
newPointData.xLabelVal = undefined;
newPointData.yLabelVal = undefined;

var ternary = newPointData.subplot;
newPointData.aLabel = Axes.tickText(ternary.aaxis, cdi.a, 'hover').text;
newPointData.bLabel = Axes.tickText(ternary.baxis, cdi.b, 'hover').text;
newPointData.cLabel = Axes.tickText(ternary.caxis, cdi.c, 'hover').text;
var fullLayout = {};
fullLayout[trace.subplot] = {_subplot: subplot};
var labels = trace._module.formatLabels(cdi, trace, fullLayout);
newPointData.aLabel = labels.aLabel;
newPointData.bLabel = labels.bLabel;
newPointData.cLabel = labels.cLabel;

var trace = newPointData.trace;
var hoverinfo = cdi.hi || trace.hoverinfo;
var text = [];
function textPart(ax, val) {
Expand All @@ -62,9 +62,9 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
if(!trace.hovertemplate) {
var parts = hoverinfo.split('+');
if(parts.indexOf('all') !== -1) parts = ['a', 'b', 'c'];
if(parts.indexOf('a') !== -1) textPart(ternary.aaxis, newPointData.aLabel);
if(parts.indexOf('b') !== -1) textPart(ternary.baxis, newPointData.bLabel);
if(parts.indexOf('c') !== -1) textPart(ternary.caxis, newPointData.cLabel);
if(parts.indexOf('a') !== -1) textPart(subplot.aaxis, newPointData.aLabel);
if(parts.indexOf('b') !== -1) textPart(subplot.baxis, newPointData.bLabel);
if(parts.indexOf('c') !== -1) textPart(subplot.caxis, newPointData.cLabel);
}
newPointData.extraText = text.join('<br>');
newPointData.hovertemplate = trace.hovertemplate;
Expand Down
1 change: 1 addition & 0 deletions src/traces/scatterternary/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = {
attributes: require('./attributes'),
supplyDefaults: require('./defaults'),
colorbar: require('../scatter/marker_colorbar'),
formatLabels: require('./format_labels'),
calc: require('./calc'),
plot: require('./plot'),
style: require('../scatter/style').style,
Expand Down

0 comments on commit 76d8a11

Please sign in to comment.