mirror of
https://github.com/chartjs/Chart.js
synced 2025-04-29 15:57:22 +08:00
Require 'this' when calling tick formatters (#12064)
The `numeric` and `logarithmic` tick formatters require that `this` be provided. That happens automatically if they're used directly as a tick callback but not if they're invoked manually. Failing to pass `this` results in runtime errors similar to the following: ``` TypeError: Cannot read properties of undefined (reading 'chart') ```
This commit is contained in:
parent
f46572e19a
commit
3361a63705
4
src/types/index.d.ts
vendored
4
src/types/index.d.ts
vendored
@ -1519,7 +1519,7 @@ export declare const Ticks: {
|
||||
* @param ticks the list of ticks being converted
|
||||
* @return string representation of the tickValue parameter
|
||||
*/
|
||||
numeric(tickValue: number, index: number, ticks: { value: number }[]): string;
|
||||
numeric(this: Scale, tickValue: number, index: number, ticks: { value: number }[]): string;
|
||||
/**
|
||||
* Formatter for logarithmic ticks
|
||||
* @param tickValue the value to be formatted
|
||||
@ -1527,7 +1527,7 @@ export declare const Ticks: {
|
||||
* @param ticks the list of ticks being converted
|
||||
* @return string representation of the tickValue parameter
|
||||
*/
|
||||
logarithmic(tickValue: number, index: number, ticks: { value: number }[]): string;
|
||||
logarithmic(this: Scale, tickValue: number, index: number, ticks: { value: number }[]): string;
|
||||
};
|
||||
};
|
||||
|
||||
|
15
test/types/ticks/ticks.ts
Normal file
15
test/types/ticks/ticks.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import { Chart, Ticks } from '../../../src/types.js';
|
||||
|
||||
// @ts-expect-error The 'this' context... is not assignable to method's 'this' of type 'Scale<CoreScaleOptions>'.
|
||||
Ticks.formatters.numeric(0, 0, [{ value: 0 }]);
|
||||
|
||||
const chart = new Chart('test', {
|
||||
type: 'line',
|
||||
data: {
|
||||
datasets: [{
|
||||
data: [{ x: 1, y: 1 }]
|
||||
}]
|
||||
},
|
||||
});
|
||||
|
||||
Ticks.formatters.numeric.call(chart.scales.x, 0, 0, [{ value: 0 }]);
|
Loading…
x
Reference in New Issue
Block a user