Return false from the average tooltip positioner on no valid data (#1… · chartjs/Chart.js@3dac05e (original) (raw)
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -301,7 +301,6 @@ export default class Scale extends Element { | ||
| 301 | 301 | * @since 3.0 |
| 302 | 302 | */ |
| 303 | 303 | getMinMax(canStack) { |
| 304 | -// eslint-disable-next-line prefer-const | |
| 305 | 304 | let {min, max, minDefined, maxDefined} = this.getUserBounds(); |
| 306 | 305 | let range; |
| 307 | 306 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -38,6 +38,11 @@ const positioners = { | ||
| 38 | 38 | } |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | +// No visible items where found, return false so we don't have to divide by 0 which reduces in NaN | |
| 42 | +if (count === 0 | | |
| 43 | +return false; | |
| 44 | +} | |
| 45 | + | |
| 41 | 46 | const xAverage = [...xSet].reduce((a, b) => a + b) / xSet.size; |
| 42 | 47 | |
| 43 | 48 | return { |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1144,6 +1144,15 @@ describe('Plugin.Tooltip', function() { | ||
| 1144 | 1144 | expect(tooltipModel.caretX).not.toBe(xPositionArrayAverage); |
| 1145 | 1145 | expect(tooltipModel.caretX).toBe(xPositionSetAverage); |
| 1146 | 1146 | }); |
| 1147 | + | |
| 1148 | +it('Should not fail with all hiden data elements on the average positioner', function() { | |
| 1149 | +const averagePositioner = tooltipPlugin.positioners.average; | |
| 1150 | + | |
| 1151 | +// Simulate `hasValue` returns false | |
| 1152 | +expect(() => averagePositioner([{x: 'invalidNumber', y: 'invalidNumber'}])).not.toThrow(); | |
| 1153 | +const result = averagePositioner([{x: 'invalidNumber', y: 'invalidNumber'}]); | |
| 1154 | +expect(result).toBe(false); | |
| 1155 | +}); | |
| 1147 | 1156 | }); |
| 1148 | 1157 | |
| 1149 | 1158 | it('Should avoid tooltip truncation in x axis if there is enough space to show tooltip without truncation', async function() { |