readonly id: string;
readonly canvas: HTMLCanvasElement;
readonly ctx: CanvasRenderingContext2D;
- readonly config: ChartConfiguration<TType, TData, TLabel>;
+ readonly config: ChartConfiguration<TType, TData, TLabel> | ChartConfigurationCustomTypesPerDataset<TType, TData, TLabel>;
readonly width: number;
readonly height: number;
readonly aspectRatio: number;
data: ChartData<TType, TData, TLabel>;
options: ChartOptions<TType>;
- constructor(item: ChartItem, config: ChartConfiguration<TType, TData, TLabel>);
+ constructor(item: ChartItem, config: ChartConfiguration<TType, TData, TLabel> | ChartConfigurationCustomTypesPerDataset<TType, TData, TLabel>);
clear(): this;
stop(): this;
isAttached(canvas: HTMLCanvasElement): boolean;
/**
* Updates config with platform specific requirements
- * @param {ChartConfiguration} config
+ * @param {ChartConfiguration | ChartConfigurationCustomTypes} config
*/
- updateConfig(config: ChartConfiguration): void;
+ updateConfig(config: ChartConfiguration | ChartConfigurationCustomTypesPerDataset): void;
}
export class BasicPlatform extends BasePlatform {}
data: TData;
}
+export interface ChartDatasetPropertiesCustomTypesPerDataset<TType extends ChartType, TData> {
+ type: TType;
+ data: TData;
+}
+
export type ChartDataset<
TType extends ChartType = ChartType,
TData = DefaultDataPoint<TType>
{ [key in ChartType]: { type: key } & ChartTypeRegistry[key]['datasetOptions'] }[TType]
> & ChartDatasetProperties<TType, TData>;
+export type ChartDatasetCustomTypesPerDataset<
+ TType extends ChartType = ChartType,
+ TData = DefaultDataPoint<TType>
+> = DeepPartial<
+{ [key in ChartType]: { type: key } & ChartTypeRegistry[key]['datasetOptions'] }[TType]
+> & ChartDatasetPropertiesCustomTypesPerDataset<TType, TData>;
+
/**
* TData represents the data point type. If unspecified, a default is provided
* based on the chart type.
datasets: ChartDataset<TType, TData>[];
}
+export interface ChartDataCustomTypesPerDataset<
+ TType extends ChartType = ChartType,
+ TData = DefaultDataPoint<TType>,
+ TLabel = unknown
+> {
+ labels?: TLabel[];
+ datasets: ChartDatasetCustomTypesPerDataset<TType, TData>[];
+}
+
export interface ChartConfiguration<
TType extends ChartType = ChartType,
TData = DefaultDataPoint<TType>,
options?: ChartOptions<TType>;
plugins?: Plugin<TType>[];
}
+
+export interface ChartConfigurationCustomTypesPerDataset<
+ TType extends ChartType = ChartType,
+ TData = DefaultDataPoint<TType>,
+ TLabel = unknown
+> {
+ data: ChartDataCustomTypesPerDataset<TType, TData, TLabel>;
+ options?: ChartOptions<TType>;
+ plugins?: Plugin<TType>[];
+}
--- /dev/null
+import { Chart } from '../index.esm';
+
+const chart = new Chart('chart', {
+ type: 'bar',
+ data: {
+ labels: ['1', '2', '3'],
+ datasets: [{
+ data: [1, 2, 3]
+ },
+ {
+ data: [1, 2, 3],
+ categoryPercentage: 10
+ }],
+ }
+});
+
+const chart2 = new Chart('chart', {
+ type: 'bar',
+ data: {
+ labels: ['1', '2', '3'],
+ datasets: [{
+ type: 'line',
+ data: [1, 2, 3],
+ // @ts-expect-error should not allow bar properties to be defined in a line dataset
+ categoryPercentage: 10
+ },
+ {
+ type: 'line',
+ pointBackgroundColor: 'red',
+ data: [1, 2, 3]
+ },
+ {
+ data: [1, 2, 3],
+ categoryPercentage: 10
+ }],
+ }
+});
+
+const chart3 = new Chart('chart', {
+ data: {
+ labels: ['1', '2', '3'],
+ datasets: [{
+ type: 'bar',
+ data: [1, 2, 3],
+ categoryPercentage: 10
+ },
+ {
+ type: 'bar',
+ data: [1, 2, 3],
+ // @ts-expect-error should not allow line properties to be defined in a bar dataset
+ pointBackgroundColor: 'red',
+ }],
+ }
+});
+
+// @ts-expect-error all datasets should have a type property or a default fallback type should be set
+const chart4 = new Chart('chart', {
+ data: {
+ labels: ['1', '2', '3'],
+ datasets: [{
+ type: 'bar',
+ data: [1, 2, 3],
+ categoryPercentage: 10
+ },
+ {
+ data: [1, 2, 3]
+ }],
+ }
+});