Skip to content

Commit 5cc6516

Browse files
committed
switch xvals/yvals in hover/click data to return actual data values rather than calcdata
1 parent fcf6e99 commit 5cc6516

3 files changed

Lines changed: 38 additions & 4 deletions

File tree

‎src/components/fx/click.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
var Registry = require('../../registry');
4+
var helpers = require('./helpers');
45
var hover = require('./hover').hover;
56

67
module.exports = function click(gd, evt, subplot) {
@@ -21,8 +22,8 @@ module.exports = function click(gd, evt, subplot) {
2122
// get coordinate values from latest hover call, if available
2223
clickData.xaxes ??= gd._hoverXAxes;
2324
clickData.yaxes ??= gd._hoverYAxes;
24-
clickData.xvals ??= gd._hoverXVals;
25-
clickData.yvals ??= gd._hoverYVals;
25+
clickData.xvals ??= gd._hoverXVals && helpers.c2dApply(gd._hoverXAxes, gd._hoverXVals);
26+
clickData.yvals ??= gd._hoverYVals && helpers.c2dApply(gd._hoverYAxes, gd._hoverYVals);
2627

2728
gd.emit('plotly_click', clickData);
2829
}

‎src/components/fx/helpers.js‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,39 @@ exports.p2c = function (axArray, v) {
4444
return out;
4545
};
4646

47+
/*
48+
* Given an array of calcdata values and an array of axes corresponding to each value,
49+
* convert the calcdata values to data values by calling the `c2d` method of each axis.
50+
*
51+
* This function is intended to be used in constructing hover and click events,
52+
* for converting x/y values from calc space to data space. axArray and valArray are arrays
53+
* rather than single values because in the case of stacked subplots, there may be multiple axes
54+
* (and therefore multiple data values) corresponding to a single hover or click event.
55+
*
56+
* For linear and log axes, this conversion has no effect beyond validating the inputs.
57+
* However, for some axes types, the converted values may be of a different type than the
58+
* inputs:
59+
* - For category axes, `c2d` converts calcdata values (numeric) into category labels (strings)
60+
* - For date axes, `c2d` converts calcdata values (numeric values in ms) into date strings
61+
*
62+
* For axes which don't define `c2d` (e.g. geo, map), the inputs are passed through untouched.
63+
*
64+
* @param {Array} axArray : axes corresponding to each value in valArray
65+
* @param {Array} valArray : calcdata values
66+
* @return {Array} : data values, computed by calling `ax.c2d` (if defined) on each input value
67+
*/
68+
exports.c2dApply = function (axArray, valArray) {
69+
if(axArray.length !== valArray.length) {
70+
Lib.warn('c2dApply: axArray and valArray must be the same length');
71+
}
72+
var out = new Array(valArray.length);
73+
for (var i = 0; i < valArray.length; i++) {
74+
var ax = axArray && axArray[i];
75+
out[i] = ax && ax.c2d ? ax.c2d(valArray[i]) : valArray[i];
76+
}
77+
return out;
78+
};
79+
4780
exports.getDistanceFunction = function (mode, dx, dy, dxy) {
4881
if (mode === 'closest') return dxy || exports.quadrature(dx, dy);
4982
return mode.charAt(0) === 'x' ? dx : dy;

‎src/components/fx/hover.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -976,8 +976,8 @@ function _hover(gd, evt, subplot, noHoverEvent, eventTarget) {
976976
points: points,
977977
xaxes: xaArray,
978978
yaxes: yaArray,
979-
xvals: xvalArray,
980-
yvals: yvalArray
979+
xvals: helpers.c2dApply(xaArray, xvalArray),
980+
yvals: helpers.c2dApply(yaArray, yvalArray)
981981
});
982982
}
983983
}

0 commit comments

Comments
 (0)