]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Add a convenience alias for scale options (#8732)
authorJosh Kelley <joshkel@gmail.com>
Fri, 26 Mar 2021 20:20:59 +0000 (16:20 -0400)
committerGitHub <noreply@github.com>
Fri, 26 Mar 2021 20:20:59 +0000 (16:20 -0400)
* Add a convenience alias for scale options

Closes #8731

* Add an automated test

* Use parameter for a more realistic test

types/index.esm.d.ts
types/tests/scales/options.ts

index 421c4ac8b85512437a2295aa8b9a7cad44409259..fea930603fce42c599a46ef273333ed6f7f3bd05 100644 (file)
@@ -3248,6 +3248,9 @@ export type ScaleOptionsByType<TScale extends ScaleType = ScaleType> =
   { [key in ScaleType]: { type: key } & ScaleTypeRegistry[key]['options'] }[TScale]
 ;
 
+// Convenience alias for creating and manipulating scale options in user code
+export type ScaleOptions<TScale extends ScaleType = ScaleType> = DeepPartial<ScaleOptionsByType<TScale>>;
+
 export type DatasetChartOptions<TType extends ChartType = ChartType> = {
   [key in TType]: {
     datasets: ChartTypeRegistry[key]['datasetOptions'];
index cf4f4dcb8546b61c60516ff1ca8cd9cb15efaf64..968d89031cfcd4b9dc4ce399fbdd91a8d890f7a9 100644 (file)
@@ -1,4 +1,4 @@
-import { Chart } from '../../index.esm';
+import { Chart, ScaleOptions } from '../../index.esm';
 
 const chart = new Chart('test', {
   type: 'bar',
@@ -30,3 +30,29 @@ const chart = new Chart('test', {
     }
   }
 });
+
+function makeChartScale(range: number): ScaleOptions<'linear'> {
+  return {
+    type: 'linear',
+    min: 0,
+    suggestedMax: range,
+  };
+}
+
+const composedChart = new Chart('test2', {
+  type: 'bar',
+  data: {
+    labels: ['a'],
+    datasets: [{
+      data: [1],
+    }, {
+      type: 'line',
+      data: [{ x: 1, y: 1 }]
+    }]
+  },
+  options: {
+    scales: {
+      x: makeChartScale(10)
+    }
+  }
+});