Chart.js/types/interfaces.d.ts
Josh Kelley efbaf2c082
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
2020-12-18 12:46:54 -05:00

191 lines
5.5 KiB
TypeScript

import {
BarControllerDatasetOptions,
LineControllerDatasetOptions,
LineControllerChartOptions,
ScatterDataPoint,
ScatterControllerDatasetOptions,
ScatterControllerChartOptions,
BubbleControllerDatasetOptions,
BubbleDataPoint,
DoughnutControllerChartOptions,
DoughnutControllerDatasetOptions,
DoughnutDataPoint,
PieControllerChartOptions,
PieControllerDatasetOptions,
PieDataPoint,
ControllerDatasetOptions,
BarControllerChartOptions,
PolarAreaControllerChartOptions,
PolarAreaControllerDatasetOptions,
RadarControllerChartOptions,
RadarControllerDatasetOptions,
} from './controllers';
import { CoreChartOptions } from './core/interfaces';
import { ElementChartOptions } from './elements';
import { FillerControllerDatasetOptions } from './plugins';
import { Plugin } from './core';
import {
LinearScaleOptions,
LogarithmicScaleOptions,
CategoryScaleOptions,
RadialLinearScaleOptions as RadialLinearScaleOptions,
TimeScaleOptions,
} from './scales';
// DeepPartial implementation taken from the utility-types NPM package, which is
// Copyright (c) 2016 Piotr Witek <piotrek.witek@gmail.com> (http://piotrwitek.github.io)
// and used under the terms of the MIT license
export type DeepPartial<T> = T extends Function
? T
: T extends Array<infer U>
? _DeepPartialArray<U>
: T extends object
? _DeepPartialObject<T>
: T | undefined;
interface _DeepPartialArray<T> extends Array<DeepPartial<T>> {}
type _DeepPartialObject<T> = { [P in keyof T]?: DeepPartial<T[P]> };
export type DistributiveArray<T> = T extends unknown ? T[] : never
export interface CartesianScaleTypeRegistry {
linear: {
options: LinearScaleOptions;
};
logarithmic: {
options: LogarithmicScaleOptions;
};
category: {
options: CategoryScaleOptions;
};
time: {
options: TimeScaleOptions;
};
timeseries: {
options: TimeScaleOptions;
};
}
export interface RadialScaleTypeRegistry {
radialLinear: {
options: RadialLinearScaleOptions;
};
}
export interface ScaleTypeRegistry extends CartesianScaleTypeRegistry, RadialScaleTypeRegistry {
}
export type ScaleType = keyof ScaleTypeRegistry;
export interface ChartTypeRegistry {
bar: {
chartOptions: BarControllerChartOptions;
datasetOptions: BarControllerDatasetOptions;
defaultDataPoint: number;
scales: keyof CartesianScaleTypeRegistry;
};
line: {
chartOptions: LineControllerChartOptions;
datasetOptions: LineControllerDatasetOptions & FillerControllerDatasetOptions;
defaultDataPoint: ScatterDataPoint;
scales: keyof CartesianScaleTypeRegistry;
};
scatter: {
chartOptions: ScatterControllerChartOptions;
datasetOptions: ScatterControllerDatasetOptions;
defaultDataPoint: ScatterDataPoint;
scales: keyof CartesianScaleTypeRegistry;
};
bubble: {
chartOptions: {};
datasetOptions: BubbleControllerDatasetOptions;
defaultDataPoint: BubbleDataPoint;
scales: keyof CartesianScaleTypeRegistry;
};
pie: {
chartOptions: PieControllerChartOptions;
datasetOptions: PieControllerDatasetOptions;
defaultDataPoint: PieDataPoint;
scales: keyof CartesianScaleTypeRegistry;
};
doughnut: {
chartOptions: DoughnutControllerChartOptions;
datasetOptions: DoughnutControllerDatasetOptions;
defaultDataPoint: DoughnutDataPoint;
scales: keyof CartesianScaleTypeRegistry;
};
polarArea: {
chartOptions: PolarAreaControllerChartOptions;
datasetOptions: PolarAreaControllerDatasetOptions;
defaultDataPoint: number;
scales: keyof RadialScaleTypeRegistry;
};
radar: {
chartOptions: RadarControllerChartOptions;
datasetOptions: RadarControllerDatasetOptions;
defaultDataPoint: number;
scales: keyof RadialScaleTypeRegistry;
};
}
export type ChartType = keyof ChartTypeRegistry;
export type ScaleOptions<TScale extends ScaleType = ScaleType> = DeepPartial<
{ [key in ScaleType]: { type: key } & ScaleTypeRegistry[key]['options'] }[TScale]
>;
export type DatasetChartOptions<TType extends ChartType = ChartType> = {
[key in TType]: {
datasets: ChartTypeRegistry[key]['datasetOptions'];
};
};
export type ScaleChartOptions<TType extends ChartType = ChartType> = {
scales: {
[key: string]: ScaleOptions<ChartTypeRegistry[TType]['scales']>;
};
};
export type ChartOptions<TType extends ChartType = ChartType> = DeepPartial<
CoreChartOptions &
ElementChartOptions &
DatasetChartOptions<TType> &
ScaleChartOptions<TType> &
ChartTypeRegistry[TType]['chartOptions']
>;
export type DefaultDataPoint<TType extends ChartType> = ChartType extends TType ? unknown[] : DistributiveArray<
ChartTypeRegistry[TType]['defaultDataPoint']
>;
export interface ChartDatasetProperties<TType extends ChartType, TData extends unknown[]> {
type?: TType;
data: TData;
}
export type ChartDataset<
TType extends ChartType = ChartType,
TData extends unknown[] = DefaultDataPoint<TType>
> = DeepPartial<
{ [key in ChartType]: { type: key } & ChartTypeRegistry[key]['datasetOptions'] }[TType]
> & ChartDatasetProperties<TType, TData>;
export interface ChartData<
TType extends ChartType = ChartType,
TData extends unknown[] = DefaultDataPoint<TType>,
TLabel = unknown
> {
labels: TLabel[];
datasets: ChartDataset<TType, TData>[];
}
export interface ChartConfiguration<
TType extends ChartType = ChartType,
TData extends unknown[] = DefaultDataPoint<TType>,
TLabel = unknown
> {
type: TType;
data: ChartData<TType, TData, TLabel>;
options?: ChartOptions<TType>;
plugins?: Plugin[];
}