Skip to content

Commit efbaf2c

Browse files
authored
TypeScript updates (#8190)
* Update type definitions and docs for legends * Fix types for onHover and onClick callbacks core.controller.js's implementation also passes the Chart instance as `this`. However, that isn't documented, and it's my impression that Chart.js is moving away from passing items as `this`, so I didn't declare it in the type definitions. * Allow multi-line ticks * Stricter DeepPartial definition The previous definition resolved to `{}` (which can allow primitives) if it was given a function, so it was far too broad for any `Scriptable<>` properties. * Grammar and writing style * Updates to animation docs Document the `fn` option, since it's in the type definitions. Fix callback usage to match example code. * Fix AnimationEvent parameter The onProgress and onComplete events were mistakenly declared as taking the standard DOM AnimationEvent. (Should Chart.js's AnimationEvent be renamed to ChartAnimationEvent to avoid any possible ambiguity?) * Allow false for disabling animations * Add comments explaining the layout and usage of Rollup
1 parent 04c45aa commit efbaf2c

File tree

7 files changed

+68
-38
lines changed

7 files changed

+68
-38
lines changed

docs/docs/configuration/animations.mdx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ The default configuration is defined here: <a href="https://github.com/chartjs/C
123123
| [[property]](#animation-property-configuration) | `object` | `undefined` | Option overrides for a single element `[property]`. These have precedence over `[collection]`. See **Looping tension [property]** example above.
124124
| [[collection]](#animation-properties-collection-configuration) | `object` | [defaults...](#default-collections) | Option overrides for multiple properties, identified by `properties` array.
125125

126-
These defaults can be overridden in `options.animation` or `dataset.animation` and `tooltip.animation`. These keys are also [Scriptable](../general/options.md#scriptable-options)
126+
These defaults can be overridden in `options.animation` or `dataset.animation` and `tooltip.animation`. These keys are also [Scriptable](../general/options.md#scriptable-options).
127127

128128
## Animation mode configuration
129129

@@ -153,6 +153,7 @@ A property option is defined by the same options of the main [animation configur
153153
| `type` | `string` | `typeof property` | Type of property, determines the interpolator used. Possible values: `'number'`, `'color'` and `'boolean'`. Only really needed for `'color'`, because `typeof` does not get that right.
154154
| `from` | <code>`number`&#124;`Color`&#124;`boolean`</code> | `undefined` | Start value for the animation. Current value is used when `undefined`
155155
| `to` | <code>`number`&#124;`Color`&#124;`boolean`</code> | `undefined` | End value for the animation. Updated value is used when `undefined`
156+
| `fn` | <code>&lt;T&gt;(from: T, to: T, factor: number) => T;</code> | `undefined` | Optional custom interpolator, instead of using a predefined interpolator from `type` |
156157

157158
## Animation properties collection configuration
158159

@@ -183,7 +184,7 @@ These default collections are overridden by most dataset controllers.
183184

184185
## Disabling animation
185186

186-
To disable an animation configuration, the animation node must be set to `false`, with the exception for animation modes which can be disable setting the `duration` to `0`.
187+
To disable an animation configuration, the animation node must be set to `false`, with the exception for animation modes which can be disabled by setting the `duration` to `0`.
187188

188189
```javascript
189190
chart.options.animation = false; // disables the whole animation
@@ -232,15 +233,15 @@ See [Robert Penner's easing equations](http://robertpenner.com/easing/).
232233

233234
## Animation Callbacks
234235

235-
The animation configuration enables the callbacks which are useful for synchronizing an external draw to the chart animation.
236+
The animation configuration provides callbacks which are useful for synchronizing an external draw to the chart animation.
236237
The callbacks can be set only at main [animation configuration](#animation-configuration).
237238

238239
| Name | Type | Default | Description
239240
| ---- | ---- | ------- | -----------
240241
| `onProgress` | `function` | `null` | Callback called on each step of an animation.
241242
| `onComplete` | `function` | `null` | Callback called when all animations are completed.
242243

243-
The callback is passed following object:
244+
The callback is passed the following object:
244245

245246
```javascript
246247
{
@@ -264,7 +265,7 @@ var chart = new Chart(ctx, {
264265
options: {
265266
animation: {
266267
onProgress: function(animation) {
267-
progress.value = animation.animationObject.currentStep / animation.animationObject.numSteps;
268+
progress.value = animation.currentStep / animation.numSteps;
268269
}
269270
}
270271
}

docs/docs/configuration/legend.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ Items passed to the legend `onClick` function are the ones returned from `labels
8282
// Label that will be displayed
8383
text: string,
8484

85+
// Index of the associated dataset
86+
datasetIndex: number,
87+
8588
// Fill style of the legend box
8689
fillStyle: Color,
8790

types/core/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ export const registry: Registry;
874874

875875
export interface Tick {
876876
value: number;
877-
label?: string;
877+
label?: string | string[];
878878
major?: boolean;
879879
}
880880

types/core/interfaces.d.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Chart, Element, InteractionMode } from '.';
1+
import { ActiveElement, AnimationEvent, Chart, InteractionMode } from '.';
22
import { ChartDataset } from '../interfaces';
33
import { ParsingOptions } from '../controllers';
44
import { PluginOptions } from '../plugins';
@@ -85,14 +85,14 @@ export interface HoverInteractionOptions extends CoreInteractionOptions {
8585
/**
8686
* Called when any of the events fire. Passed the event, an array of active elements (bars, points, etc), and the chart.
8787
*/
88-
onHover(event: ChartEvent, elements: Element[]): void;
88+
onHover(event: ChartEvent, elements: ActiveElement[], chart: Chart): void;
8989
}
9090

9191
export interface CoreChartOptions extends ParsingOptions {
92-
animation: Scriptable<AnimationOptions>;
92+
animation: Scriptable<AnimationOptions | false>;
9393

9494
datasets: {
95-
animation: Scriptable<AnimationOptions>;
95+
animation: Scriptable<AnimationOptions | false>;
9696
};
9797

9898
/**
@@ -131,7 +131,7 @@ export interface CoreChartOptions extends ParsingOptions {
131131
* @default 2
132132
*/
133133
aspectRatio: number;
134-
134+
135135
/**
136136
* Locale used for number formatting (using `Intl.NumberFormat`).
137137
* @default user's browser setting
@@ -162,12 +162,12 @@ export interface CoreChartOptions extends ParsingOptions {
162162
/**
163163
* Called when any of the events fire. Passed the event, an array of active elements (bars, points, etc), and the chart.
164164
*/
165-
onHover(event: ChartEvent, elements: Element[]): void;
165+
onHover(event: ChartEvent, elements: ActiveElement[], chart: Chart): void;
166166

167167
/**
168168
* Called if the event is of type 'mouseup' or 'click'. Passed the event, an array of active elements, and the chart.
169169
*/
170-
onClick(event: ChartEvent, elements: Element[]): void;
170+
onClick(event: ChartEvent, elements: ActiveElement[], chart: Chart): void;
171171

172172
layout: {
173173
padding: Scriptable<number | ChartArea>;
@@ -261,7 +261,7 @@ export interface AnimationPropertySpec extends AnimationCommonSpec {
261261
}
262262

263263
export type AnimationSpecContainer = AnimationCommonSpec & {
264-
[prop: string]: AnimationPropertySpec;
264+
[prop: string]: AnimationPropertySpec | false;
265265
};
266266

267267
export type AnimationOptions = AnimationSpecContainer & {
@@ -270,15 +270,15 @@ export type AnimationOptions = AnimationSpecContainer & {
270270
*/
271271
onProgress: (this: Chart, event: AnimationEvent) => void;
272272
/**
273-
*Callback called when all animations are completed.
273+
* Callback called when all animations are completed.
274274
*/
275275
onComplete: (this: Chart, event: AnimationEvent) => void;
276276

277-
active: AnimationSpecContainer;
278-
hide: AnimationSpecContainer;
279-
reset: AnimationSpecContainer;
280-
resize: AnimationSpecContainer;
281-
show: AnimationSpecContainer;
277+
active: AnimationSpecContainer | false;
278+
hide: AnimationSpecContainer | false;
279+
reset: AnimationSpecContainer | false;
280+
resize: AnimationSpecContainer | false;
281+
show: AnimationSpecContainer | false;
282282
};
283283

284284
export interface FontSpec {

types/index.esm.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
/**
2+
* Top-level type definitions. These are processed by Rollup and rollup-plugin-dts
3+
* to make a combined .d.ts file under dist; that way, all of the type definitions
4+
* appear directly within the "chart.js" module; that matches the layout of the
5+
* distributed chart.esm.js bundle and means that users of Chart.js can easily use
6+
* module augmentation to extend Chart.js's types and plugins within their own
7+
* code, like so:
8+
*
9+
* @example
10+
* declare module "chart.js" {
11+
* // Add types here
12+
* }
13+
*/
14+
115
export * from './controllers';
216
export * from './core';
317
export * from './elements';

types/interfaces.d.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,18 @@ import {
3232
TimeScaleOptions,
3333
} from './scales';
3434

35-
export type DeepPartial<T> = T extends {}
36-
? {
37-
[K in keyof T]?: DeepPartial<T[K]>;
38-
}
39-
: T;
35+
// DeepPartial implementation taken from the utility-types NPM package, which is
36+
// Copyright (c) 2016 Piotr Witek <piotrek.witek@gmail.com> (http://piotrwitek.github.io)
37+
// and used under the terms of the MIT license
38+
export type DeepPartial<T> = T extends Function
39+
? T
40+
: T extends Array<infer U>
41+
? _DeepPartialArray<U>
42+
: T extends object
43+
? _DeepPartialObject<T>
44+
: T | undefined;
45+
interface _DeepPartialArray<T> extends Array<DeepPartial<T>> {}
46+
type _DeepPartialObject<T> = { [P in keyof T]?: DeepPartial<T[P]> };
4047

4148
export type DistributiveArray<T> = T extends unknown ? T[] : never
4249

types/plugins/index.d.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,59 +41,64 @@ export interface LegendItem {
4141
*/
4242
text: string;
4343

44+
/**
45+
* Index of the associated dataset
46+
*/
47+
datasetIndex: number;
48+
4449
/**
4550
* Fill style of the legend box
4651
*/
47-
fillStyle: CanvasLineCap;
52+
fillStyle?: Color;
4853

4954
/**
5055
* If true, this item represents a hidden dataset. Label will be rendered with a strike-through effect
5156
*/
52-
hidden: boolean;
57+
hidden?: boolean;
5358

5459
/**
5560
* For box border.
5661
* @see https://developer.mozilla.org/en/docs/Web/API/CanvasRenderingContext2D/lineCap
5762
*/
58-
lineCap: CanvasLineCap;
63+
lineCap?: CanvasLineCap;
5964

6065
/**
6166
* For box border.
6267
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setLineDash
6368
*/
64-
lineDash: number[];
69+
lineDash?: number[];
6570

6671
/**
6772
* For box border.
6873
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineDashOffset
6974
*/
70-
lineDashOffset: number;
75+
lineDashOffset?: number;
7176

7277
/**
7378
* For box border.
7479
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineJoin
7580
*/
76-
lineJoin: CanvasLineJoin;
81+
lineJoin?: CanvasLineJoin;
7782

7883
/**
7984
* Width of box border
8085
*/
81-
lineWidth: number;
86+
lineWidth?: number;
8287

8388
/**
8489
* Stroke style of the legend box
8590
*/
86-
strokeStyle: Color;
91+
strokeStyle?: Color;
8792

8893
/**
8994
* Point style of the legend box (only used if usePointStyle is true)
9095
*/
91-
pointStyle: PointStyle;
96+
pointStyle?: PointStyle;
9297

9398
/**
9499
* Rotation of the point in degrees (only used if usePointStyle is true)
95100
*/
96-
rotation: number;
101+
rotation?: number;
97102
}
98103

99104
export interface LegendElement extends Element {}
@@ -129,11 +134,11 @@ export interface LegendOptions {
129134
*/
130135
onClick(this: LegendElement, e: ChartEvent, legendItem: LegendItem, legend: LegendElement): void;
131136
/**
132-
* A callback that is called when a 'mousemove' event is registered on top of a label item
137+
* A callback that is called when a 'mousemove' event is registered on top of a label item
133138
*/
134139
onHover(this: LegendElement, e: ChartEvent, legendItem: LegendItem, legend: LegendElement): void;
135140
/**
136-
* A callback that is called when a 'mousemove' event is registered outside of a previously hovered label item.
141+
* A callback that is called when a 'mousemove' event is registered outside of a previously hovered label item.
137142
*/
138143
onLeave(this: LegendElement, e: ChartEvent, legendItem: LegendItem, legend: LegendElement): void;
139144

@@ -176,7 +181,7 @@ export interface LegendOptions {
176181
pointStyle: PointStyle;
177182

178183
/**
179-
* Label style will match corresponding point style (size is based on the mimimum value between boxWidth and font.size).
184+
* Label style will match corresponding point style (size is based on the minimum value between boxWidth and font.size).
180185
* @default false
181186
*/
182187
usePointStyle: boolean;

0 commit comments

Comments
 (0)