]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Fix some typings issues (#9680)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Fri, 24 Sep 2021 22:13:53 +0000 (01:13 +0300)
committerGitHub <noreply@github.com>
Fri, 24 Sep 2021 22:13:53 +0000 (18:13 -0400)
* Types: Add scope to ticks.callback

* Fix some typings issues

docs/samples/advanced/linear-gradient.md
docs/samples/advanced/radial-gradient.md
types/index.esm.d.ts
types/tests/scales/options.ts
types/tests/scriptable_core_chart_options.ts [new file with mode: 0644]

index 47fa7b50bb94b3e86a286a30920b05d4b17fa24b..4b0896353c85a2bea8cb2596a05e40759918a3f3 100644 (file)
@@ -47,7 +47,7 @@ let width, height, gradient;
 function getGradient(ctx, chartArea) {
   const chartWidth = chartArea.right - chartArea.left;
   const chartHeight = chartArea.bottom - chartArea.top;
-  if (gradient === null || width !== chartWidth || height !== chartHeight) {
+  if (!gradient || width !== chartWidth || height !== chartHeight) {
     // Create the gradient because this is either the first render
     // or the size of the chart has changed
     width = chartWidth;
@@ -79,7 +79,7 @@ const data = {
 
         if (!chartArea) {
           // This case happens on initial chart load
-          return null;
+          return;
         }
         return getGradient(ctx, chartArea);
       },
index cf8d0d63d423735c20d677c096d82aa1c46f8523..c38833efc082eb8f89ce7fab57a9209c827fa19e 100644 (file)
@@ -30,7 +30,7 @@ function createRadialGradient3(context, c1, c2, c3) {
   const chartArea = context.chart.chartArea;
   if (!chartArea) {
     // This case happens on initial chart load
-    return null;
+    return;
   }
 
   const chartWidth = chartArea.right - chartArea.left;
index 19a7009fb97eed2770104487a599890aea28ee57..548720c57ca9bf78daf0095cd527efcf38bfca28 100644 (file)
@@ -34,7 +34,7 @@ export interface ScriptableLineSegmentContext {
   datasetIndex: number
 }
 
-export type Scriptable<T, TContext> = T | ((ctx: TContext, options: AnyObject) => T);
+export type Scriptable<T, TContext> = T | ((ctx: TContext, options: AnyObject) => T | undefined);
 export type ScriptableOptions<T, TContext> = { [P in keyof T]: Scriptable<T[P], TContext> };
 export type ScriptableAndArray<T, TContext> = readonly T[] | Scriptable<T, TContext>;
 export type ScriptableAndArrayOptions<T, TContext> = { [P in keyof T]: ScriptableAndArray<T[P], TContext> };
@@ -1402,22 +1402,22 @@ export interface CoreChartOptions<TType extends ChartType> extends ParsingOption
    * base color
    * @see Defaults.color
    */
-  color: Color;
+  color: Scriptable<Color, ScriptableContext<TType>>;
   /**
    * base background color
    * @see Defaults.backgroundColor
    */
-  backgroundColor: Color;
+  backgroundColor: Scriptable<Color, ScriptableContext<TType>>;
   /**
    * base border color
    * @see Defaults.borderColor
    */
-  borderColor: Color;
+  borderColor: Scriptable<Color, ScriptableContext<TType>>;
   /**
    * base font
    * @see Defaults.font
    */
-  font: FontSpec;
+  font: Scriptable<Partial<FontSpec>, ScriptableContext<TType>>;
   /**
    * Resizes the chart canvas when its container does (important note...).
    * @default true
@@ -2805,7 +2805,7 @@ export interface TickOptions {
   /**
    * Returns the string representation of the tick value as it should be displayed on the chart. See callback.
    */
-  callback: (tickValue: number | string, index: number, ticks: Tick[]) => string | number | null | undefined;
+  callback: (this: Scale, tickValue: number | string, index: number, ticks: Tick[]) => string | number | null | undefined;
   /**
    * If true, show tick labels.
    * @default true
index 968d89031cfcd4b9dc4ce399fbdd91a8d890f7a9..cc1dc9015d962800f5fababa3200b881e3bb6921 100644 (file)
@@ -26,6 +26,14 @@ const chart = new Chart('test', {
           // @ts-expect-error Type 'string' is not assignable to type 'false | "millisecond" | "second" | "minute" | "hour" | "day" | "week" | "month" | "quarter" | "year" | undefined'.
           unit: 'year'
         }
+      },
+      y: {
+        ticks: {
+          callback(tickValue) {
+            const value = this.getLabelForValue(tickValue as number);
+            return '$' + value;
+          }
+        }
       }
     }
   }
diff --git a/types/tests/scriptable_core_chart_options.ts b/types/tests/scriptable_core_chart_options.ts
new file mode 100644 (file)
index 0000000..8039f3c
--- /dev/null
@@ -0,0 +1,15 @@
+import { ChartConfiguration } from '../index.esm';
+
+const getConfig = (): ChartConfiguration<'bar'> => {
+  return {
+    type: 'bar',
+    data: {
+      datasets: []
+    },
+    options: {
+      backgroundColor: (context) => context.active ? '#fff' : undefined,
+      font: (context) => context.datasetIndex === 1 ? { size: 10 } : { size: 12, family: 'arial' }
+    }
+  };
+};
+