Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions draftlogs/8206_fix.md
Original file line number Diff line number Diff line change
@@ -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!
2 changes: 1 addition & 1 deletion src/components/fx/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/traces/bar/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/traces/bar/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
Expand Down
4 changes: 2 additions & 2 deletions src/traces/scatter/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {};
Expand Down
4 changes: 2 additions & 2 deletions src/traces/scatter/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/traces/waterfall/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
43 changes: 43 additions & 0 deletions test/jasmine/tests/bar_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
Expand Down
36 changes: 36 additions & 0 deletions test/jasmine/tests/hover_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
25 changes: 25 additions & 0 deletions test/jasmine/tests/scatter_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
43 changes: 43 additions & 0 deletions test/jasmine/tests/waterfall_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
Expand Down