Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unbind instance config from chart type #10963

Merged
merged 1 commit into from Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 9 additions & 7 deletions types/index.d.ts
Expand Up @@ -488,7 +488,7 @@ export declare class Chart<
readonly id: string;
readonly canvas: HTMLCanvasElement;
readonly ctx: CanvasRenderingContext2D;
readonly config: ChartConfiguration<TType, TData, TLabel> | ChartConfigurationCustomTypesPerDataset<TType, TData, TLabel>;
readonly config: ChartConfigurationInstance;
readonly width: number;
readonly height: number;
readonly aspectRatio: number;
Expand All @@ -498,11 +498,11 @@ export declare class Chart<
readonly scales: { [key: string]: Scale };
readonly attached: boolean;

readonly legend?: LegendElement<TType>; // Only available if legend plugin is registered and enabled
readonly tooltip?: TooltipModel<TType>; // Only available if tooltip plugin is registered and enabled
readonly legend?: LegendElement; // Only available if legend plugin is registered and enabled
readonly tooltip?: TooltipModel; // Only available if tooltip plugin is registered and enabled

data: ChartData<TType, TData, TLabel>;
options: ChartOptions<TType>;
data: ChartData;
options: ChartOptions;

constructor(item: ChartItem, config: ChartConfiguration<TType, TData, TLabel> | ChartConfigurationCustomTypesPerDataset<TType, TData, TLabel>);

Expand Down Expand Up @@ -2186,7 +2186,7 @@ export interface LegendItem {
textAlign?: TextAlign;
}

export interface LegendElement<TType extends ChartType> extends Element<AnyObject, LegendOptions<TType>>, LayoutItem {
export interface LegendElement<TType extends ChartType = ChartType> extends Element<AnyObject, LegendOptions<TType>>, LayoutItem {
chart: Chart<TType>;
ctx: CanvasRenderingContext2D;
legendItems?: LegendItem[];
Expand Down Expand Up @@ -2420,7 +2420,7 @@ export interface TooltipLabelStyle {
*/
borderRadius?: number | BorderRadius;
}
export interface TooltipModel<TType extends ChartType> extends Element<AnyObject, TooltipOptions<TType>> {
export interface TooltipModel<TType extends ChartType = ChartType> extends Element<AnyObject, TooltipOptions<TType>> {
readonly chart: Chart<TType>;

// The items that we are rendering in the tooltip. See Tooltip Item Interface section
Expand Down Expand Up @@ -3660,3 +3660,5 @@ export interface ChartConfigurationCustomTypesPerDataset<
options?: ChartOptions<TType>;
plugins?: Plugin<TType>[];
}

export type ChartConfigurationInstance = ChartConfiguration | ChartConfigurationCustomTypesPerDataset & { type?: undefined }
65 changes: 65 additions & 0 deletions types/tests/config_types.ts
@@ -0,0 +1,65 @@
import { Chart } from '../../src/types.js';

const chart = new Chart('chart', {
type: 'bar',
data: {
labels: ['1', '2', '3'],
datasets: [{
data: [1, 2, 3]
},
{
data: [1, 2, 3]
}],
}
});

chart.config.type = 'line';

const chart2 = new Chart('chart', {
type: 'bar',
data: {
labels: ['1', '2', '3'],
datasets: [{
type: 'line',
data: [1, 2, 3]
},
{
type: 'line',
data: [1, 2, 3]
}],
}
});

chart2.config.type = 'line';

const chart3 = new Chart('chart', {
data: {
labels: ['1', '2', '3'],
datasets: [{
type: 'bar',
data: [1, 2, 3]
},
{
type: 'bar',
data: [1, 2, 3],
categoryPercentage: 10
}],
}
});

chart3.config.type = 'line';

const chart4 = new Chart('chart', {
data: {
labels: ['1', '2', '3'],
datasets: [{
type: 'bar',
data: [1, 2, 3]
}]
}
});

chart4.data.datasets.push({
type: 'line',
data: [1, 2, 3]
});