Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return false from the average tooltip positioner on no valid data #11863

Merged
merged 6 commits into from
Aug 17, 2024
Merged
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: 0 additions & 1 deletion src/core/core.scale.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,6 @@ export default class Scale extends Element {
* @since 3.0
*/
getMinMax(canStack) {
// eslint-disable-next-line prefer-const
let {min, max, minDefined, maxDefined} = this.getUserBounds();
let range;

Expand Down
5 changes: 5 additions & 0 deletions src/plugins/plugin.tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ const positioners = {
}
}

// No visible items where found, return false so we don't have to divide by 0 which reduces in NaN
if (count === 0 || xSet.size === 0) {
return false;
}

const xAverage = [...xSet].reduce((a, b) => a + b) / xSet.size;

return {
Expand Down
9 changes: 9 additions & 0 deletions test/specs/plugin.tooltip.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,15 @@ describe('Plugin.Tooltip', function() {
expect(tooltipModel.caretX).not.toBe(xPositionArrayAverage);
expect(tooltipModel.caretX).toBe(xPositionSetAverage);
});

it('Should not fail with all hiden data elements on the average positioner', function() {
const averagePositioner = tooltipPlugin.positioners.average;

// Simulate `hasValue` returns false
expect(() => averagePositioner([{x: 'invalidNumber', y: 'invalidNumber'}])).not.toThrow();
const result = averagePositioner([{x: 'invalidNumber', y: 'invalidNumber'}]);
expect(result).toBe(false);
});
});

it('Should avoid tooltip truncation in x axis if there is enough space to show tooltip without truncation', async function() {
Expand Down
Loading