diff --git a/draftlogs/8206_fix.md b/draftlogs/8206_fix.md new file mode 100644 index 00000000000..61ab50bb9be --- /dev/null +++ b/draftlogs/8206_fix.md @@ -0,0 +1 @@ +- Fix `xperiod` and `yperiod` breaking bar, waterfall and scatter traces on non-date axes, where they are now ignored [[#8206](https://github.com/plotly/plotly.js/pull/8206)], with thanks to @akysil for the contribution! diff --git a/src/components/fx/hover.js b/src/components/fx/hover.js index 3acd4a73b56..4a7c6e08db3 100644 --- a/src/components/fx/hover.js +++ b/src/components/fx/hover.js @@ -2729,7 +2729,7 @@ function orderRangePoints(hoverData, hovermode) { if (Registry.traceIs(d.trace, 'bar-like') || Registry.traceIs(d.trace, 'box-violin')) { last.push(d); - } else if (d.trace[axLetter + 'period']) { + } else if (d.trace[axLetter + 'period'] && d[axLetter + 'a'].type === 'date') { second.push(d); } else { first.push(d); diff --git a/src/traces/bar/calc.js b/src/traces/bar/calc.js index a0ec292e0be..154b10dde52 100644 --- a/src/traces/bar/calc.js +++ b/src/traces/bar/calc.js @@ -20,13 +20,13 @@ module.exports = function calc(gd, trace) { size = xa.makeCalcdata(trace, 'x', sizeOpts); origPos = ya.makeCalcdata(trace, 'y'); pObj = alignPeriod(trace, ya, 'y', origPos); - hasPeriod = !!trace.yperiodalignment; + hasPeriod = !!pObj.ends; pLetter = 'y'; } else { size = ya.makeCalcdata(trace, 'y', sizeOpts); origPos = xa.makeCalcdata(trace, 'x'); pObj = alignPeriod(trace, xa, 'x', origPos); - hasPeriod = !!trace.xperiodalignment; + hasPeriod = !!pObj.ends; pLetter = 'x'; } pos = pObj.vals; diff --git a/src/traces/bar/hover.js b/src/traces/bar/hover.js index e062c4b50af..ac5600e6684 100644 --- a/src/traces/bar/hover.js +++ b/src/traces/bar/hover.js @@ -51,7 +51,8 @@ function hoverOnBars(pointData, xval, yval, hovermode, opts) { dx = positionFn; } - var period = trace[posLetter + 'period']; + // calc writes period bounds only on date axes with a valid period + const period = `${posLetter}End` in cd[0]; var isClosestOrPeriod = isClosest || period; function thisBarMinPos(di) { return thisBarExtPos(di, -1); } diff --git a/src/traces/scatter/calc.js b/src/traces/scatter/calc.js index 619d3b83a44..519f79e27c1 100644 --- a/src/traces/scatter/calc.js +++ b/src/traces/scatter/calc.js @@ -53,8 +53,8 @@ function calc(gd, trace) { calcAxisExpansion(gd, trace, xa, ya, x, y, ppad); } - var hasPeriodX = !!trace.xperiodalignment; - var hasPeriodY = !!trace.yperiodalignment; + const hasPeriodX = !!xObj.ends; + const hasPeriodY = !!yObj.ends; for(i = 0; i < serieslen; i++) { var cdi = cd[i] = {}; diff --git a/src/traces/scatter/hover.js b/src/traces/scatter/hover.js index 04b0de04e17..327c5f65309 100644 --- a/src/traces/scatter/hover.js +++ b/src/traces/scatter/hover.js @@ -18,8 +18,8 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) { var hoveron = trace.hoveron || ''; var minRad = (trace.mode.indexOf('markers') !== -1) ? 3 : 0.5; - var xPeriod = !!trace.xperiodalignment; - var yPeriod = !!trace.yperiodalignment; + const xPeriod = !!trace.xperiodalignment && xa.type === 'date'; + const yPeriod = !!trace.yperiodalignment && ya.type === 'date'; // look for points to hover on first, then take fills only if we // didn't find a point diff --git a/src/traces/waterfall/calc.js b/src/traces/waterfall/calc.js index 11f9afa6da6..8c2a8c864a7 100644 --- a/src/traces/waterfall/calc.js +++ b/src/traces/waterfall/calc.js @@ -23,13 +23,13 @@ module.exports = function calc(gd, trace) { size = xa.makeCalcdata(trace, 'x'); origPos = ya.makeCalcdata(trace, 'y'); pObj = alignPeriod(trace, ya, 'y', origPos); - hasPeriod = !!trace.yperiodalignment; + hasPeriod = !!pObj.ends; pLetter = 'y'; } else { size = ya.makeCalcdata(trace, 'y'); origPos = xa.makeCalcdata(trace, 'x'); pObj = alignPeriod(trace, xa, 'x', origPos); - hasPeriod = !!trace.xperiodalignment; + hasPeriod = !!pObj.ends; pLetter = 'x'; } pos = pObj.vals; diff --git a/test/jasmine/tests/bar_test.js b/test/jasmine/tests/bar_test.js index 55ee5f3ee75..f107b71fb48 100644 --- a/test/jasmine/tests/bar_test.js +++ b/test/jasmine/tests/bar_test.js @@ -562,6 +562,35 @@ describe('Bar.calc', function() { var cd = gd.calcdata; assertPointField(cd, 's', [[2000]]); }); + + [ + { axType: 'category', pos: ['2026-01-15', '2026-02-15', '2026-03-15'], period: 'M1', p: [0, 1, 2] }, + { axType: 'linear', pos: [1, 2, 3], period: 2, p: [1, 2, 3] } + ].forEach(({ axType, pos, period, p }) => { + it(`should ignore xperiod on a ${axType} x axis`, () => { + const gd = mockBarPlot([{ x: pos, y: [4, 1, 3], xperiod: period }], { xaxis: { type: axType } }); + + assertPointField(gd.calcdata, 'p', [p]); + gd.calcdata[0].forEach((d) => { + expect(d.orig_p).toBeUndefined(); + expect(d.xStart).toBeUndefined(); + expect(d.xEnd).toBeUndefined(); + }); + }); + + it(`should ignore yperiod on a ${axType} y axis`, () => { + const gd = mockBarPlot([{ orientation: 'h', x: [4, 1, 3], y: pos, yperiod: period }], { + yaxis: { type: axType } + }); + + assertPointField(gd.calcdata, 'p', [p]); + gd.calcdata[0].forEach((d) => { + expect(d.orig_p).toBeUndefined(); + expect(d.yStart).toBeUndefined(); + expect(d.yEnd).toBeUndefined(); + }); + }); + }); }); describe('Bar.crossTraceCalc (formerly known as setPositions)', function() { @@ -2485,6 +2514,20 @@ describe('bar hover', function() { }); }); + [ + { axType: 'category', x: ['2026-01-15', '2026-02-15', '2026-03-15'], period: 'M1', xval: 1 }, + { axType: 'linear', x: [1, 2, 3], period: 2, xval: 2 } + ].forEach(({ axType, x, period, xval }) => { + it(`should return a hover point with xperiod on a ${axType} x axis`, async () => { + gd = createGraphDiv(); + await Plotly.newPlot(gd, [{ type: 'bar', x, y: [4, 1, 3], xperiod: period }], { + xaxis: { type: axType } + }); + + expect(_hover(gd, xval, 0, 'x').style[0]).toBe(1); + }); + }); + describe('with orientation *h*', function() { beforeAll(function(done) { gd = createGraphDiv(); diff --git a/test/jasmine/tests/hover_test.js b/test/jasmine/tests/hover_test.js index cee2c7831dc..378dfd6e71e 100644 --- a/test/jasmine/tests/hover_test.js +++ b/test/jasmine/tests/hover_test.js @@ -8261,3 +8261,39 @@ describe('hoversort', function () { .then(done, done.fail); }); }); + +describe('hover with xperiod on non-date axes', () => { + let gd; + + beforeEach(() => { + gd = createGraphDiv(); + }); + + afterEach(destroyGraphDiv); + + [undefined, 2].forEach((xperiod) => { + it(`picks the closest scatter point in unified hover on a linear axis with xperiod ${xperiod}`, async () => { + await Plotly.newPlot( + gd, + [ + { x: [1, 2, 3], y: [1, 1, 1], xperiod }, + { x: [1.05, 2.05, 3.05], y: [2, 2, 2] } + ], + { + hovermode: 'x unified', + width: 500, + height: 400, + margin: { l: 50, r: 50, t: 50, b: 50 }, + // 0.05 x units = 4px, within the default 20px hoverdistance + xaxis: { range: [0, 5] }, + yaxis: { range: [0, 3] } + } + ); + + Fx.hover(gd, { xval: 2 }); + Lib.clearThrottle(); + + expect(d3Select('g.hoverlayer g.legend text.legendtitletext').text()).toBe('2'); + }); + }); +}); diff --git a/test/jasmine/tests/scatter_test.js b/test/jasmine/tests/scatter_test.js index 4c27d0a9bb8..a842061a014 100644 --- a/test/jasmine/tests/scatter_test.js +++ b/test/jasmine/tests/scatter_test.js @@ -1472,6 +1472,31 @@ describe('scatter hoverPoints', function() { }) .then(done, done.fail); }); + + [ + { axType: 'category', vals: ['2026-01-15', '2026-02-15', '2026-03-15'], period: 'M1', c: [0, 1, 2] }, + { axType: 'linear', vals: [1, 2, 3], period: 2, c: [1, 2, 3] } + ].forEach(({ axType, vals, period, c }) => { + it(`should ignore xperiod on a ${axType} x axis`, async () => { + const gd = createGraphDiv(); + await Plotly.newPlot(gd, [{ x: vals, y: [4, 1, 3], xperiod: period }], { xaxis: { type: axType } }); + + expect(gd.calcdata[0].map((d) => d.x)).toEqual(c); + expect(gd.calcdata[0][1].xStart).toBeUndefined(); + expect(gd.calcdata[0][1].xEnd).toBeUndefined(); + expect(_hover(gd, c[1], 1, 'x')[0].index).toBe(1); + }); + + it(`should ignore yperiod on a ${axType} y axis`, async () => { + const gd = createGraphDiv(); + await Plotly.newPlot(gd, [{ x: [4, 1, 3], y: vals, yperiod: period }], { yaxis: { type: axType } }); + + expect(gd.calcdata[0].map((d) => d.y)).toEqual(c); + expect(gd.calcdata[0][1].yStart).toBeUndefined(); + expect(gd.calcdata[0][1].yEnd).toBeUndefined(); + expect(_hover(gd, 1, c[1], 'y')[0].index).toBe(1); + }); + }); }); describe('scatter hoverFills', function() { diff --git a/test/jasmine/tests/waterfall_test.js b/test/jasmine/tests/waterfall_test.js index 06044a7883d..83d803b8d90 100644 --- a/test/jasmine/tests/waterfall_test.js +++ b/test/jasmine/tests/waterfall_test.js @@ -375,6 +375,35 @@ describe('Waterfall.calc', function() { assertPointField(cd, 'x', [[1, NaN, NaN, 15]]); assertPointField(cd, 'y', [[1, 3, 13, 43]]); }); + + [ + { axType: 'category', pos: ['2026-01-15', '2026-02-15', '2026-03-15'], period: 'M1', p: [0, 1, 2] }, + { axType: 'linear', pos: [1, 2, 3], period: 2, p: [1, 2, 3] } + ].forEach(({ axType, pos, period, p }) => { + it(`should ignore xperiod on a ${axType} x axis`, () => { + const gd = mockWaterfallPlot([{ x: pos, y: [4, 1, 3], xperiod: period }], { xaxis: { type: axType } }); + + assertPointField(gd.calcdata, 'p', [p]); + gd.calcdata[0].forEach((d) => { + expect(d.orig_p).toBeUndefined(); + expect(d.xStart).toBeUndefined(); + expect(d.xEnd).toBeUndefined(); + }); + }); + + it(`should ignore yperiod on a ${axType} y axis`, () => { + const gd = mockWaterfallPlot([{ orientation: 'h', x: [4, 1, 3], y: pos, yperiod: period }], { + yaxis: { type: axType } + }); + + assertPointField(gd.calcdata, 'p', [p]); + gd.calcdata[0].forEach((d) => { + expect(d.orig_p).toBeUndefined(); + expect(d.yStart).toBeUndefined(); + expect(d.yEnd).toBeUndefined(); + }); + }); + }); }); describe('Waterfall.crossTraceCalc', function() { @@ -1398,6 +1427,20 @@ describe('waterfall hover', function() { }); }); + [ + { axType: 'category', x: ['2026-01-15', '2026-02-15', '2026-03-15'], period: 'M1', xval: 1 }, + { axType: 'linear', x: [1, 2, 3], period: 2, xval: 2 } + ].forEach(({ axType, x, period, xval }) => { + it(`should return a hover point with xperiod on a ${axType} x axis`, async () => { + gd = createGraphDiv(); + await Plotly.newPlot(gd, [{ type: 'waterfall', x, y: [4, 1, 3], xperiod: period }], { + xaxis: { type: axType } + }); + + expect(_hover(gd, xval, 0, 'x').style[0]).toBe(1); + }); + }); + describe('text labels', function() { it('should show \'hovertext\' items when present, \'text\' if not', function(done) { gd = createGraphDiv();