Skip to content

Commit

Permalink
Add missing feature for disabling plugins in TyeScript (#11403)
Browse files Browse the repository at this point in the history
* Add  missing feature for disabling plugins in TyeScript

* apply review

* remove empty line
  • Loading branch information
stockiNail authored Jul 24, 2023
1 parent cc7ee8a commit f287be4
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 17 deletions.
2 changes: 1 addition & 1 deletion docs/developers/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ declare module 'chart.js' {
interface PluginOptionsByType<TType extends ChartType> {
customCanvasBackgroundColor?: {
color?: string
}
} | false
}
}
```
14 changes: 7 additions & 7 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2913,13 +2913,13 @@ export interface TooltipItem<TType extends ChartType> {
}

export interface PluginOptionsByType<TType extends ChartType> {
colors: ColorsPluginOptions;
decimation: DecimationOptions;
filler: FillerOptions;
legend: LegendOptions<TType>;
subtitle: TitleOptions;
title: TitleOptions;
tooltip: TooltipOptions<TType>;
colors: ColorsPluginOptions | false;
decimation: DecimationOptions | false;
filler: FillerOptions | false;
legend: LegendOptions<TType> | false;
subtitle: TitleOptions | false;
title: TitleOptions | false;
tooltip: TooltipOptions<TType> | false;
}
export interface PluginChartOptions<TType extends ChartType> {
plugins: PluginOptionsByType<TType>;
Expand Down
6 changes: 3 additions & 3 deletions test/types/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Chart } from '../../src/types.js';
import { Chart, TitleOptions, TooltipOptions } from '../../src/types.js';

Chart.defaults.scales.time.time.minUnit = 'day';

Chart.defaults.plugins.title.display = false;
(Chart.defaults.plugins.title as TitleOptions).display = false;

Chart.defaults.datasets.bar.backgroundColor = 'red';

Expand All @@ -27,4 +27,4 @@ Chart.defaults.layout = {
},
};

Chart.defaults.plugins.tooltip.boxPadding = 3;
(Chart.defaults.plugins.tooltip as TooltipOptions).boxPadding = 3;
4 changes: 2 additions & 2 deletions test/types/overrides.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Chart } from '../../src/types.js';
import { Chart, TitleOptions } from '../../src/types.js';

Chart.overrides.bar.scales.x.type = 'time';

Chart.overrides.bar.plugins.title.display = false;
(Chart.overrides.bar.plugins.title as TitleOptions).display = false;

Chart.overrides.line.datasets.bar.backgroundColor = 'red';

Expand Down
5 changes: 3 additions & 2 deletions test/types/plugins/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { defaults } from '../../../src/types.js';
import { defaults, LegendOptions } from '../../../src/types.js';

// https://github.com/chartjs/Chart.js/issues/8711
const original = defaults.plugins.legend.labels.generateLabels;
const original = (defaults.plugins.legend as LegendOptions<"line">).labels.generateLabels;

// @ts-ignore
defaults.plugins.legend.labels.generateLabels = function(chart) {
return [{
datasetIndex: 0,
Expand Down
16 changes: 16 additions & 0 deletions test/types/plugins/disable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Chart } from '../../../src/types.js';

const chart = new Chart('id', {
type: 'bubble',
data: {
labels: [],
datasets: [{
data: []
}]
},
options: {
plugins: {
legend: false
}
}
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Chart } from '../../../../src/types.js';
import { Chart, TooltipOptions } from '../../../../src/types.js';

Chart.overrides.bubble.plugins.tooltip.callbacks.label = (item) => {
(Chart.overrides.bubble.plugins.tooltip as TooltipOptions<'bubble'>).callbacks.label = (item) => {
const { x, y, _custom: r } = item.parsed;
return `${item.label}: (${x}, ${y}, ${r})`;
};
Expand Down

0 comments on commit f287be4

Please sign in to comment.