Skip to content

Commit 2933803

Browse files
committed
fix hover on bars with period
1 parent 53fea8e commit 2933803

File tree

13 files changed

+60
-32
lines changed

13 files changed

+60
-32
lines changed

src/plots/cartesian/align_period.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ var constants = require('../../constants/numerical');
88
var ONEAVGMONTH = constants.ONEAVGMONTH;
99

1010
module.exports = function alignPeriod(trace, ax, axLetter, vals) {
11-
if(ax.type !== 'date') return vals;
11+
if(ax.type !== 'date') return {vals: vals};
1212

1313
var alignment = trace[axLetter + 'periodalignment'];
14-
if(!alignment) return vals;
14+
if(!alignment) return {vals: vals};
1515

1616
var period = trace[axLetter + 'period'];
1717
var mPeriod;
1818
if(isNumeric(period)) {
1919
period = +period;
20-
if(period <= 0) return vals;
20+
if(period <= 0) return {vals: vals};
2121
} else if(typeof period === 'string' && period.charAt(0) === 'M') {
2222
var n = +(period.substring(1));
2323
if(n > 0 && Math.round(n) === n) {
2424
mPeriod = n;
25-
} else return vals;
25+
} else return {vals: vals};
2626
}
2727

2828
var calendar = ax.calendar;
@@ -35,6 +35,9 @@ module.exports = function alignPeriod(trace, ax, axLetter, vals) {
3535
var base = dateTime2ms(period0, calendar) || 0;
3636

3737
var newVals = [];
38+
var starts = [];
39+
var ends = [];
40+
3841
var len = vals.length;
3942
for(var i = 0; i < len; i++) {
4043
var v = vals[i];
@@ -77,6 +80,14 @@ module.exports = function alignPeriod(trace, ax, axLetter, vals) {
7780
isEnd ? endTime :
7881
(startTime + endTime) / 2
7982
);
83+
84+
starts[i] = startTime;
85+
ends[i] = endTime;
8086
}
81-
return newVals;
87+
88+
return {
89+
vals: newVals,
90+
starts: starts,
91+
ends: ends
92+
};
8293
};

src/traces/bar/calc.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,24 @@ var calcSelection = require('../scatter/calc_selection');
1010
module.exports = function calc(gd, trace) {
1111
var xa = Axes.getFromId(gd, trace.xaxis || 'x');
1212
var ya = Axes.getFromId(gd, trace.yaxis || 'y');
13-
var size, pos, origPos;
13+
var size, pos, origPos, pObj, hasPeriod;
1414

1515
var sizeOpts = {
1616
msUTC: !!(trace.base || trace.base === 0)
1717
};
1818

19-
var hasPeriod;
2019
if(trace.orientation === 'h') {
2120
size = xa.makeCalcdata(trace, 'x', sizeOpts);
2221
origPos = ya.makeCalcdata(trace, 'y');
23-
pos = alignPeriod(trace, ya, 'y', origPos);
22+
pObj = alignPeriod(trace, ya, 'y', origPos);
2423
hasPeriod = !!trace.yperiodalignment;
2524
} else {
2625
size = ya.makeCalcdata(trace, 'y', sizeOpts);
2726
origPos = xa.makeCalcdata(trace, 'x');
28-
pos = alignPeriod(trace, xa, 'x', origPos);
27+
pObj = alignPeriod(trace, xa, 'x', origPos);
2928
hasPeriod = !!trace.xperiodalignment;
3029
}
30+
pos = pObj.vals;
3131

3232
// create the "calculated data" to plot
3333
var serieslen = Math.min(pos.length, size.length);
@@ -39,6 +39,8 @@ module.exports = function calc(gd, trace) {
3939

4040
if(hasPeriod) {
4141
cd[i].orig_p = origPos[i]; // used by hover
42+
cd[i].pEnd = pObj.ends[i];
43+
cd[i].pStart = pObj.starts[i];
4244
}
4345

4446
if(trace.ids) {

src/traces/bar/cross_trace_calc.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,12 +436,20 @@ function setBarCenterAndWidth(pa, sieve) {
436436
var barwidth = t.barwidth;
437437
var barwidthIsArray = Array.isArray(barwidth);
438438

439+
var trace = calcTrace[0].trace;
440+
var isPeriod = !!trace[pLetter + 'periodalignment'];
441+
439442
for(var j = 0; j < calcTrace.length; j++) {
440443
var calcBar = calcTrace[j];
441444

442445
// store the actual bar width and position, for use by hover
443446
var width = calcBar.w = barwidthIsArray ? barwidth[j] : barwidth;
444447
calcBar[pLetter] = calcBar.p + (poffsetIsArray ? poffset[j] : poffset) + width / 2;
448+
449+
if(isPeriod) {
450+
calcBar.wPeriod =
451+
calcBar.pEnd - calcBar.pStart;
452+
}
445453
}
446454
}
447455
}

src/traces/bar/hover.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,9 @@ function hoverOnBars(pointData, xval, yval, hovermode, opts) {
5757
function thisBarMaxPos(di) { return thisBarExtPos(di, 1); }
5858

5959
function thisBarExtPos(di, sgn) {
60-
if(period) {
61-
return di.p + sgn * Math.abs(di.p - di.orig_p);
62-
}
63-
return di[posLetter] + sgn * di.w / 2;
60+
var w = (period) ? di.wPeriod : di.w;
61+
62+
return di[posLetter] + sgn * w / 2;
6463
}
6564

6665
var minPos = isClosest || period ?
@@ -180,6 +179,9 @@ function hoverOnBars(pointData, xval, yval, hovermode, opts) {
180179

181180
var hasPeriod = di.orig_p !== undefined;
182181
pointData[posLetter + 'LabelVal'] = hasPeriod ? di.orig_p : di.p;
182+
if(hasPeriod) {
183+
pointData[posLetter + 'Period'] = di.p;
184+
}
183185

184186
pointData.labelLabel = hoverLabelText(pa, pointData[posLetter + 'LabelVal'], trace[posLetter + 'hoverformat']);
185187
pointData.valueLabel = hoverLabelText(sa, pointData[sizeLetter + 'LabelVal'], trace[sizeLetter + 'hoverformat']);

src/traces/box/calc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ function getPosArrays(trace, posLetter, posAxis, num) {
311311

312312
if(hasPosArray || (hasPos0 && hasPosStep)) {
313313
var origPos = posAxis.makeCalcdata(trace, posLetter);
314-
var pos = alignPeriod(trace, posAxis, posLetter, origPos);
314+
var pos = alignPeriod(trace, posAxis, posLetter, origPos).vals;
315315
return [pos, origPos];
316316
}
317317

src/traces/candlestick/calc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = function(gd, trace) {
1212
var ya = Axes.getFromId(gd, trace.yaxis);
1313

1414
var origX = xa.makeCalcdata(trace, 'x');
15-
var x = alignPeriod(trace, xa, 'x', origX);
15+
var x = alignPeriod(trace, xa, 'x', origX).vals;
1616

1717
var cd = calcCommon(gd, trace, origX, x, ya, ptFunc);
1818

src/traces/funnel/calc.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ var BADNUM = require('../../constants/numerical').BADNUM;
99
module.exports = function calc(gd, trace) {
1010
var xa = Axes.getFromId(gd, trace.xaxis || 'x');
1111
var ya = Axes.getFromId(gd, trace.yaxis || 'y');
12-
var size, pos, origPos, i, cdi;
12+
var size, pos, origPos, pObj, hasPeriod, i, cdi;
1313

14-
var hasPeriod;
1514
if(trace.orientation === 'h') {
1615
size = xa.makeCalcdata(trace, 'x');
1716
origPos = ya.makeCalcdata(trace, 'y');
18-
pos = alignPeriod(trace, ya, 'y', origPos);
17+
pObj = alignPeriod(trace, ya, 'y', origPos);
1918
hasPeriod = !!trace.yperiodalignment;
2019
} else {
2120
size = ya.makeCalcdata(trace, 'y');
2221
origPos = xa.makeCalcdata(trace, 'x');
23-
pos = alignPeriod(trace, xa, 'x', origPos);
22+
pObj = alignPeriod(trace, xa, 'x', origPos);
2423
hasPeriod = !!trace.xperiodalignment;
2524
}
25+
pos = pObj.vals;
2626

2727
// create the "calculated data" to plot
2828
var serieslen = Math.min(pos.length, size.length);
@@ -55,6 +55,8 @@ module.exports = function calc(gd, trace) {
5555

5656
if(hasPeriod) {
5757
cd[i].orig_p = origPos[i]; // used by hover
58+
cd[i].pEnd = pObj.ends[i];
59+
cd[i].pStart = pObj.starts[i];
5860
}
5961

6062
if(trace.ids) {

src/traces/heatmap/calc.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ module.exports = function calc(gd, trace) {
5454
} else {
5555
origX = trace.x ? xa.makeCalcdata(trace, 'x') : [];
5656
origY = trace.y ? ya.makeCalcdata(trace, 'y') : [];
57-
x = alignPeriod(trace, xa, 'x', origX);
58-
y = alignPeriod(trace, ya, 'y', origY);
57+
x = alignPeriod(trace, xa, 'x', origX).vals;
58+
y = alignPeriod(trace, ya, 'y', origY).vals;
5959
trace._x = x;
6060
trace._y = y;
6161
}

src/traces/heatmap/convert_column_xyz.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ module.exports = function convertColumnData(trace, ax1, ax2, var1Name, var2Name,
88
var colLen = trace._length;
99
var col1 = ax1.makeCalcdata(trace, var1Name);
1010
var col2 = ax2.makeCalcdata(trace, var2Name);
11-
col1 = alignPeriod(trace, ax1, var1Name, col1);
12-
col2 = alignPeriod(trace, ax2, var2Name, col2);
11+
col1 = alignPeriod(trace, ax1, var1Name, col1).vals;
12+
col2 = alignPeriod(trace, ax2, var2Name, col2).vals;
1313

1414
var textCol = trace.text;
1515
var hasColumnText = (textCol !== undefined && Lib.isArray1D(textCol));

src/traces/ohlc/calc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ function convertTickWidth(gd, xa, trace) {
144144
var origX = xa.makeCalcdata(tracei, 'x');
145145
tracei._origX = origX;
146146

147-
var xcalc = alignPeriod(trace, xa, 'x', origX);
147+
var xcalc = alignPeriod(trace, xa, 'x', origX).vals;
148148
tracei._xcalc = xcalc;
149149

150150
var _minDiff = Lib.distinctVals(xcalc).minDiff;

src/traces/scatter/calc.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ function calc(gd, trace) {
1818
var ya = Axes.getFromId(gd, trace.yaxis || 'y');
1919
var origX = xa.makeCalcdata(trace, 'x');
2020
var origY = ya.makeCalcdata(trace, 'y');
21-
var x = alignPeriod(trace, xa, 'x', origX);
22-
var y = alignPeriod(trace, ya, 'y', origY);
21+
var x = alignPeriod(trace, xa, 'x', origX).vals;
22+
var y = alignPeriod(trace, ya, 'y', origY).vals;
2323

2424
var serieslen = trace._length;
2525
var cd = new Array(serieslen);

src/traces/scattergl/calc.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ module.exports = function calc(gd, trace) {
3131

3232
var origX = xa.makeCalcdata(trace, 'x');
3333
var origY = ya.makeCalcdata(trace, 'y');
34-
var x = alignPeriod(trace, xa, 'x', origX);
35-
var y = alignPeriod(trace, ya, 'y', origY);
34+
var x = alignPeriod(trace, xa, 'x', origX).vals;
35+
var y = alignPeriod(trace, ya, 'y', origY).vals;
3636
trace._x = x;
3737
trace._y = y;
3838

src/traces/waterfall/calc.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,21 @@ function isTotal(a) {
1717
module.exports = function calc(gd, trace) {
1818
var xa = Axes.getFromId(gd, trace.xaxis || 'x');
1919
var ya = Axes.getFromId(gd, trace.yaxis || 'y');
20-
var size, pos, origPos;
20+
var size, pos, origPos, pObj, hasPeriod;
2121

22-
var hasPeriod;
2322
if(trace.orientation === 'h') {
2423
size = xa.makeCalcdata(trace, 'x');
2524
origPos = ya.makeCalcdata(trace, 'y');
26-
pos = alignPeriod(trace, ya, 'y', origPos);
25+
pObj = alignPeriod(trace, ya, 'y', origPos);
26+
pos = pObj.vals;
2727
hasPeriod = !!trace.yperiodalignment;
2828
} else {
2929
size = ya.makeCalcdata(trace, 'y');
3030
origPos = xa.makeCalcdata(trace, 'x');
31-
pos = alignPeriod(trace, xa, 'x', origPos);
31+
pObj = alignPeriod(trace, xa, 'x', origPos);
3232
hasPeriod = !!trace.xperiodalignment;
3333
}
34+
pos = pObj.vals;
3435

3536
// create the "calculated data" to plot
3637
var serieslen = Math.min(pos.length, size.length);
@@ -85,6 +86,8 @@ module.exports = function calc(gd, trace) {
8586

8687
if(hasPeriod) {
8788
cd[i].orig_p = origPos[i]; // used by hover
89+
cd[i].pEnd = pObj.ends[i];
90+
cd[i].pStart = pObj.starts[i];
8891
}
8992

9093
if(trace.ids) {

0 commit comments

Comments
 (0)