Skip to content
Closed
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/XXXX_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 [[#XXXX](https://github.com/plotly/plotly.js/pull/XXXX)], 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
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
29 changes: 29 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
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
29 changes: 29 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
Loading