Skip to content

Commit 51ad8c2

Browse files
committed
get rid of (x|y)LabelVal(0|1), in favor of (x|y)Label
and move range-or-single-value logic into Axes.hoverLabelText
1 parent 7581567 commit 51ad8c2

File tree

4 files changed

+27
-27
lines changed

4 files changed

+27
-27
lines changed

src/components/fx/hover.js

+2-20
Original file line numberDiff line numberDiff line change
@@ -1065,11 +1065,11 @@ function cleanPoint(d, hovermode) {
10651065

10661066
// and convert the x and y label values into formatted text
10671067
if(d.xLabelVal !== undefined) {
1068-
d.xLabel = ('xLabel' in d) ? d.xLabel : getDimText(d, 'x');
1068+
d.xLabel = ('xLabel' in d) ? d.xLabel : Axes.hoverLabelText(d.xa, d.xLabelVal);
10691069
d.xVal = d.xa.c2d(d.xLabelVal);
10701070
}
10711071
if(d.yLabelVal !== undefined) {
1072-
d.yLabel = ('yLabel' in d) ? d.yLabel : getDimText(d, 'y');
1072+
d.yLabel = ('yLabel' in d) ? d.yLabel : Axes.hoverLabelText(d.ya, d.yLabelVal);
10731073
d.yVal = d.ya.c2d(d.yLabelVal);
10741074
}
10751075
if(d.zLabelVal !== undefined) d.zLabel = String(d.zLabelVal);
@@ -1112,24 +1112,6 @@ function cleanPoint(d, hovermode) {
11121112
return d;
11131113
}
11141114

1115-
// get text for either a value range (val0 .. val1) if it exists,
1116-
// or single value (val) if the range does not exist
1117-
function getDimText(d, axLetter) {
1118-
var val = d[axLetter + 'LabelVal'];
1119-
var val0 = d[axLetter + 'LabelVal0'];
1120-
var val1 = d[axLetter + 'LabelVal1'];
1121-
var ax = d[axLetter + 'a'];
1122-
1123-
if(val0 !== undefined && val1 !== undefined) {
1124-
var text0 = Axes.hoverLabelText(ax, val0);
1125-
var text1 = Axes.hoverLabelText(ax, val1);
1126-
1127-
if(text0 === text1) return text0;
1128-
return text0 + ' - ' + text1;
1129-
}
1130-
return Axes.hoverLabelText(ax, val);
1131-
}
1132-
11331115
function createSpikelines(hoverData, opts) {
11341116
var hovermode = opts.hovermode;
11351117
var container = opts.container;

src/plots/cartesian/axes.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var ONEHOUR = constants.ONEHOUR;
2828
var ONEMIN = constants.ONEMIN;
2929
var ONESEC = constants.ONESEC;
3030
var MINUS_SIGN = constants.MINUS_SIGN;
31+
var BADNUM = constants.BADNUM;
3132

3233
var MID_SHIFT = require('../../constants/alignment').MID_SHIFT;
3334

@@ -1216,7 +1217,23 @@ axes.tickText = function(ax, x, hover) {
12161217
return out;
12171218
};
12181219

1219-
axes.hoverLabelText = function(ax, val) {
1220+
/**
1221+
* create text for a hover label on this axis, with special handling of
1222+
* log axes (where negative values can't be displayed but can appear in hover text)
1223+
*
1224+
* @param {object} ax: the axis to format text for
1225+
* @param {number} val: calcdata value to format
1226+
* @param {Optional(number)} val2: a second value to display
1227+
*
1228+
* @returns {string} `val` formatted as a string appropriate to this axis, or
1229+
* `val` and `val2` as a range (ie '<val> - <val2>') if `val2` is provided and
1230+
* it's different from `val`.
1231+
*/
1232+
axes.hoverLabelText = function(ax, val, val2) {
1233+
if(val2 !== BADNUM && val2 !== val) {
1234+
return axes.hoverLabelText(ax, val) + ' - ' + axes.hoverLabelText(ax, val2);
1235+
}
1236+
12201237
var logOffScale = (ax.type === 'log' && val <= 0);
12211238
var tx = axes.tickText(ax, ax.c2l(logOffScale ? -val : val), 'hover').text;
12221239

src/traces/histogram/hover.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
'use strict';
1111

1212
var barHover = require('../bar/hover');
13+
var hoverLabelText = require('../../plots/cartesian/axes').hoverLabelText;
1314

1415
module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
1516
var pts = barHover(pointData, xval, yval, hovermode);
@@ -23,8 +24,7 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
2324
if(!trace.cumulative.enabled) {
2425
var posLetter = trace.orientation === 'h' ? 'y' : 'x';
2526

26-
pointData[posLetter + 'LabelVal0'] = di.p0;
27-
pointData[posLetter + 'LabelVal1'] = di.p1;
27+
pointData[posLetter + 'Label'] = hoverLabelText(pointData[posLetter + 'a'], di.p0, di.p1);
2828
pointData.pts = di.pts;
2929
}
3030

src/traces/histogram2d/hover.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
'use strict';
1111

1212
var heatmapHover = require('../heatmap/hover');
13+
var hoverLabelText = require('../../plots/cartesian/axes').hoverLabelText;
1314

1415
module.exports = function hoverPoints(pointData, xval, yval, hovermode, contour) {
1516
var pts = heatmapHover(pointData, xval, yval, hovermode, contour);
@@ -21,11 +22,11 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode, contour)
2122
var ny = indices[0];
2223
var nx = indices[1];
2324
var cd0 = pointData.cd[0];
25+
var xRange = cd0.xRanges[nx];
26+
var yRange = cd0.yRanges[ny];
2427

25-
pointData.xLabelVal0 = cd0.xRanges[nx][0];
26-
pointData.xLabelVal1 = cd0.xRanges[nx][1];
27-
pointData.yLabelVal0 = cd0.yRanges[ny][0];
28-
pointData.yLabelVal1 = cd0.yRanges[ny][1];
28+
pointData.xLabel = hoverLabelText(pointData.xa, xRange[0], xRange[1]);
29+
pointData.yLabel = hoverLabelText(pointData.ya, yRange[0], yRange[1]);
2930
pointData.pts = cd0.pts[ny][nx];
3031

3132
return pts;

0 commit comments

Comments
 (0)