mirror of
https://github.com/chartjs/Chart.js.git
synced 2025-12-08 20:36:08 +00:00
Return false from the average tooltip positioner on no valid data (#11863)
This commit is contained in:
parent
b51b57aca4
commit
3dac05ed00
@ -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;
|
||||
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user