]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Enable scriptable element chart options (#9012)
authorEvert Timberg <evert.timberg+github@gmail.com>
Sat, 1 May 2021 17:30:14 +0000 (13:30 -0400)
committerGitHub <noreply@github.com>
Sat, 1 May 2021 17:30:14 +0000 (13:30 -0400)
types/index.esm.d.ts
types/tests/elements/scriptable_element_options.ts [new file with mode: 0644]

index dbb34fefb2287d81dbde9c09feeacfffa58e9e90..3066472c0b87d83d26dd3eedc52b0d991b966cf9 100644 (file)
@@ -604,7 +604,7 @@ export interface DatasetControllerChartComponent extends ChartComponent {
   };
 }
 
-export interface Defaults extends CoreChartOptions<ChartType>, ElementChartOptions, PluginChartOptions<ChartType> {
+export interface Defaults extends CoreChartOptions<ChartType>, ElementChartOptions<ChartType>, PluginChartOptions<ChartType> {
 
   scale: ScaleOptionsByType;
   scales: {
@@ -641,7 +641,7 @@ export interface Defaults extends CoreChartOptions<ChartType>, ElementChartOptio
 export type Overrides = {
   [key in ChartType]:
     CoreChartOptions<key> &
-    ElementChartOptions &
+    ElementChartOptions<key> &
     PluginChartOptions<key> &
     DatasetChartOptions<ChartType> &
     ScaleChartOptions<key> &
@@ -1887,16 +1887,17 @@ export const BarElement: ChartComponent & {
   new (cfg: AnyObject): BarElement;
 };
 
-export interface ElementOptionsByType {
-  arc: ArcOptions & ArcHoverOptions;
-  bar: BarOptions & BarHoverOptions;
-  line: LineOptions & LineHoverOptions;
-  point: PointOptions & PointHoverOptions;
-}
-export interface ElementChartOptions {
-  elements: Partial<ElementOptionsByType>;
+export interface ElementOptionsByType<TType extends ChartType> {
+  arc: ScriptableAndArrayOptions<ArcOptions & ArcHoverOptions, ScriptableContext<TType>>;
+  bar: ScriptableAndArrayOptions<BarOptions & BarHoverOptions, ScriptableContext<TType>>;
+  line: ScriptableAndArrayOptions<LineOptions & LineHoverOptions, ScriptableContext<TType>>;
+  point: ScriptableAndArrayOptions<PointOptions & PointHoverOptions, ScriptableContext<TType>>;
 }
 
+export type ElementChartOptions<TType extends ChartType = ChartType> = {
+  elements: ElementOptionsByType<TType>
+};
+
 export class BasePlatform {
   /**
    * Called at chart construction time, returns a context2d instance implementing
@@ -3321,7 +3322,7 @@ export type ScaleChartOptions<TType extends ChartType = ChartType> = {
 
 export type ChartOptions<TType extends ChartType = ChartType> = DeepPartial<
   CoreChartOptions<TType> &
-  ElementChartOptions &
+  ElementChartOptions<TType> &
   PluginChartOptions<TType> &
   DatasetChartOptions<TType> &
   ScaleChartOptions<TType> &
diff --git a/types/tests/elements/scriptable_element_options.ts b/types/tests/elements/scriptable_element_options.ts
new file mode 100644 (file)
index 0000000..0776ec0
--- /dev/null
@@ -0,0 +1,49 @@
+import { Chart } from '../../index.esm';
+
+const chart = new Chart('id', {
+  type: 'line',
+  data: {
+    labels: [],
+    datasets: []
+  },
+  options: {
+    elements: {
+      line: {
+        borderWidth: () => 2,
+      },
+      point: {
+        pointStyle: (ctx) => 'star',
+      }
+    }
+  }
+});
+
+const chart2 = new Chart('id', {
+  type: 'bar',
+  data: {
+    labels: [],
+    datasets: []
+  },
+  options: {
+    elements: {
+      bar: {
+        borderWidth: (ctx) => 2,
+      }
+    }
+  }
+});
+
+const chart3 = new Chart('id', {
+  type: 'doughnut',
+  data: {
+    labels: [],
+    datasets: []
+  },
+  options: {
+    elements: {
+      arc: {
+        borderWidth: (ctx) => 3,
+      }
+    }
+  }
+});