]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Add missing feature for disabling plugins in TyeScript (#11403)
authorstockiNail <stocki.nail@gmail.com>
Mon, 24 Jul 2023 19:39:38 +0000 (21:39 +0200)
committerGitHub <noreply@github.com>
Mon, 24 Jul 2023 19:39:38 +0000 (15:39 -0400)
* Add  missing feature for disabling plugins in TyeScript

* apply review

* remove empty line

docs/developers/plugins.md
src/types/index.d.ts
test/types/defaults.ts
test/types/overrides.ts
test/types/plugins/defaults.ts
test/types/plugins/disable.ts [new file with mode: 0644]
test/types/plugins/plugin.tooltip/tooltip_parsed_data_chart_defaults.ts

index 60747416b919f7b4c2ac1b0ef405735c3d10f591..4468a58f52e0c2bc54334f6c81b625c349070f22 100644 (file)
@@ -197,7 +197,7 @@ declare module 'chart.js' {
   interface PluginOptionsByType<TType extends ChartType> {
     customCanvasBackgroundColor?: {
       color?: string
-    }
+    } | false
   }
 }
 ```
index 83ad302e30ac82a84255fd2b8d9554d0dd801343..65e1213dcd7fb5945c89d4675a5ec67e4d5a2e65 100644 (file)
@@ -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>;
index 8407184924545afcf6e31f03dbedb8e2a206c762..d20415df1cae2dea909ffec7c7a2fca713ebb5d1 100644 (file)
@@ -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';
 
@@ -27,4 +27,4 @@ Chart.defaults.layout = {
   },
 };
 
-Chart.defaults.plugins.tooltip.boxPadding = 3;
+(Chart.defaults.plugins.tooltip as TooltipOptions).boxPadding = 3;
index b4da296a32274d84879809719ba00c5af9c07dca..9085dd983569e8f9df1d93919af3e466477d0e33 100644 (file)
@@ -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';
 
index 55a08ac7ad8d23b1fc2cac352989e596093972b0..a11c4dce09adf9c689d5fc7bd5d2d330bd30f277 100644 (file)
@@ -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,
diff --git a/test/types/plugins/disable.ts b/test/types/plugins/disable.ts
new file mode 100644 (file)
index 0000000..d6ed066
--- /dev/null
@@ -0,0 +1,16 @@
+import { Chart } from '../../../src/types.js';\r
+\r
+const chart = new Chart('id', {\r
+  type: 'bubble',\r
+  data: {\r
+    labels: [],\r
+    datasets: [{\r
+      data: []\r
+    }]\r
+  },\r
+  options: {\r
+    plugins: {\r
+      legend: false\r
+    }\r
+  }\r
+});
\ No newline at end of file
index 5072824bcf9f8d38053a61593179174ec3ac7e20..b5584adeb43d398565fad6b4a9c610f353e1af37 100644 (file)
@@ -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})`;
 };