Skip to content

Commit 8d9411b

Browse files
committed
display original position in hover for scatter and bar traces
1 parent 866b069 commit 8d9411b

File tree

5 files changed

+34
-15
lines changed

5 files changed

+34
-15
lines changed

src/plots/cartesian/align_period.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ module.exports = function alignPeriod(trace, ax, axLetter, vals) {
4444
var period0 = trace[axLetter + 'period0'];
4545
var base = (dateTime2ms(period0, calendar) || 0) - offset;
4646

47+
var newVals = [];
4748
var len = vals.length;
4849
for(var i = 0; i < len; i++) {
4950
var v = vals[i] - base;
@@ -98,10 +99,9 @@ module.exports = function alignPeriod(trace, ax, axLetter, vals) {
9899
);
99100
}
100101

101-
if(newD) {
102-
vals[i] = newD.getTime() + base;
103-
}
102+
newVals[i] = newD ? newD.getTime() + base : vals[i];
104103
}
104+
return newVals;
105105
}
106106
return vals;
107107
};

src/traces/bar/calc.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,23 @@ var calcSelection = require('../scatter/calc_selection');
1818
module.exports = function calc(gd, trace) {
1919
var xa = Axes.getFromId(gd, trace.xaxis || 'x');
2020
var ya = Axes.getFromId(gd, trace.yaxis || 'y');
21-
var size, pos;
21+
var size, pos, oPos;
2222

2323
var sizeOpts = {
2424
msUTC: !!(trace.base || trace.base === 0)
2525
};
2626

27+
var hasPeriod;
2728
if(trace.orientation === 'h') {
2829
size = xa.makeCalcdata(trace, 'x', sizeOpts);
29-
pos = ya.makeCalcdata(trace, 'y');
30-
pos = alignPeriod(trace, ya, 'y', pos);
30+
oPos = ya.makeCalcdata(trace, 'y');
31+
pos = alignPeriod(trace, ya, 'y', oPos);
32+
hasPeriod = !!trace.yperiodalignment;
3133
} else {
3234
size = ya.makeCalcdata(trace, 'y', sizeOpts);
33-
pos = xa.makeCalcdata(trace, 'x');
34-
pos = alignPeriod(trace, xa, 'x', pos);
35+
oPos = xa.makeCalcdata(trace, 'x');
36+
pos = alignPeriod(trace, xa, 'x', oPos);
37+
hasPeriod = !!trace.xperiodalignment;
3538
}
3639

3740
// create the "calculated data" to plot
@@ -42,6 +45,10 @@ module.exports = function calc(gd, trace) {
4245
for(var i = 0; i < serieslen; i++) {
4346
cd[i] = { p: pos[i], s: size[i] };
4447

48+
if(hasPeriod) {
49+
cd[i].orig_p = oPos[i]; // used by hover
50+
}
51+
4552
if(trace.ids) {
4653
cd[i].id = String(trace.ids[i]);
4754
}

src/traces/bar/hover.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ function hoverOnBars(pointData, xval, yval, hovermode) {
158158
var extent = t.extents[t.extents.round(di.p)];
159159
pointData[posLetter + '0'] = pa.c2p(isClosest ? minPos(di) : extent[0], true);
160160
pointData[posLetter + '1'] = pa.c2p(isClosest ? maxPos(di) : extent[1], true);
161-
pointData[posLetter + 'LabelVal'] = di.p;
161+
162+
var hasPeriod = di.orig_p !== undefined;
163+
pointData[posLetter + 'LabelVal'] = hasPeriod ? di.orig_p : di.p;
162164

163165
pointData.labelLabel = hoverLabelText(pa, pointData[posLetter + 'LabelVal']);
164166
pointData.valueLabel = hoverLabelText(sa, pointData[sizeLetter + 'LabelVal']);

src/traces/scatter/calc.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ function calc(gd, trace) {
2424
var fullLayout = gd._fullLayout;
2525
var xa = Axes.getFromId(gd, trace.xaxis || 'x');
2626
var ya = Axes.getFromId(gd, trace.yaxis || 'y');
27-
var x = xa.makeCalcdata(trace, 'x');
28-
var y = ya.makeCalcdata(trace, 'y');
29-
x = alignPeriod(trace, xa, 'x', x);
30-
y = alignPeriod(trace, ya, 'y', y);
27+
var oX = xa.makeCalcdata(trace, 'x');
28+
var oY = ya.makeCalcdata(trace, 'y');
29+
var x = alignPeriod(trace, xa, 'x', oX);
30+
var y = alignPeriod(trace, ya, 'y', oY);
3131

3232
var serieslen = trace._length;
3333
var cd = new Array(serieslen);
@@ -59,13 +59,23 @@ function calc(gd, trace) {
5959
calcAxisExpansion(gd, trace, xa, ya, x, y, ppad);
6060
}
6161

62+
var hasPeriodX = !!trace.xperiodalignment;
63+
var hasPeriodY = !!trace.yperiodalignment;
64+
6265
for(i = 0; i < serieslen; i++) {
6366
var cdi = cd[i] = {};
6467
var xValid = isNumeric(x[i]);
6568
var yValid = isNumeric(y[i]);
6669
if(xValid && yValid) {
6770
cdi[xAttr] = x[i];
6871
cdi[yAttr] = y[i];
72+
73+
if(hasPeriodX) {
74+
cdi.orig_x = oX[i];
75+
}
76+
if(hasPeriodY) {
77+
cdi.orig_y = oY[i];
78+
}
6979
} else if(stackGroupOpts && (isV ? xValid : yValid)) {
7080
// if we're stacking we need to hold on to all valid positions
7181
// even with invalid sizes

src/traces/scatter/hover.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
7878
// the normalized individual sizes, so that's what I'm doing here
7979
// for now.
8080
var sizeVal = orientation && (di.sNorm || di.s);
81-
var xLabelVal = (orientation === 'h') ? sizeVal : di.x;
82-
var yLabelVal = (orientation === 'v') ? sizeVal : di.y;
81+
var xLabelVal = (orientation === 'h') ? sizeVal : di.orig_x !== undefined ? di.orig_x : di.x;
82+
var yLabelVal = (orientation === 'v') ? sizeVal : di.orig_y !== undefined ? di.orig_y : di.y;
8383

8484
Lib.extendFlat(pointData, {
8585
color: getTraceColor(trace, di),

0 commit comments

Comments
 (0)