]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Improved types for defaults (#8025) 3.0.0-beta.6
authoremmcbd <42184090+emmcbd@users.noreply.github.com>
Sun, 8 Nov 2020 14:52:41 +0000 (15:52 +0100)
committerGitHub <noreply@github.com>
Sun, 8 Nov 2020 14:52:41 +0000 (09:52 -0500)
src/core/core.defaults.js
types/controllers/index.d.ts
types/core/index.d.ts
types/core/interfaces.d.ts
types/interfaces.d.ts
types/plugins/index.d.ts

index 9793a880d09352dc0d3e7e9ad9845aacce1beae0..ac088cc8d63c0fb4d4edad8d128c19f16fa30b32 100644 (file)
@@ -56,9 +56,8 @@ export class Defaults {
                this.showLine = true;
                this.plugins = {};
                this.scale = undefined;
-               this.doughnut = undefined;
                this.scales = {};
-               this.controllers = undefined;
+               this.controllers = {};
        }
        /**
         * @param {string} scope
index ebcc5e70f4e4640fd1c9e21f5d3ffad280ca276a..0c3da62d79a6c5f53e69bf1247939a8be4e2db59 100644 (file)
@@ -13,9 +13,25 @@ import {
   BarOptions,
 } from '../elements';
 
-export interface ControllerDatasetOptions {
+export interface ParsingOptions {
   /**
-   *   How to clip relative to chartArea. Positive value allows overflow, negative value clips that many pixels inside chartArea. 0 = clip at chartArea. Clipping can also be configured per side: clip: {left: 5, top: false, right: -2, bottom: 0}
+   * How to parse the dataset. The parsing can be disabled by specifying parsing: false at chart options or dataset. If parsing is disabled, data must be sorted and in the formats the associated chart type and scales use internally.
+   */
+  parsing:
+  {
+    [key: string]: string;
+  }
+  | false;
+
+  /**
+   * Chart.js is fastest if you provide data with indices that are unique, sorted, and consistent across datasets and provide the normalized: true option to let Chart.js know that you have done so.
+   */
+  normalized: boolean;
+}
+
+export interface ControllerDatasetOptions extends ParsingOptions {
+  /**
+   * How to clip relative to chartArea. Positive value allows overflow, negative value clips that many pixels inside chartArea. 0 = clip at chartArea. Clipping can also be configured per side: clip: {left: 5, top: false, right: -2, bottom: 0}
    */
   clip: number | ChartArea;
   /**
index 8986f241bf0037417441b1f0aad051da547a2346..a1e036c5df263e932057984cfa7b458538fe6af8 100644 (file)
@@ -1,7 +1,7 @@
 import { BasePlatform } from '../platform';
 import {
        Color,
-       EasingFunction,
+       CoreChartOptions,
        ChartArea,
        ChartComponent,
        FontSpec,
@@ -11,14 +11,21 @@ import {
        ChartEvent,
 } from './interfaces';
 import {
+       DeepPartial,
        DefaultDataPoint,
        ChartConfiguration,
        ChartData,
        ChartDataset,
        ChartOptions,
        ChartType,
-       ScaleOptions
+       ChartTypeRegistry,
+       DatasetChartOptions,
+       ScaleChartOptions,
+       ScaleOptions,
+       ScaleType
 } from '../interfaces';
+import { ElementChartOptions } from '../elements';
+import { PluginOptions, PluginChartOptions } from '../plugins';
 
 export interface DateAdapterBase {
        /**
@@ -118,85 +125,6 @@ export class Animations {
        update(target: any, values: any): undefined | boolean;
 }
 
-export interface AnimationCommonSpec {
-       /**
-        * The number of milliseconds an animation takes.
-        * @default 1000
-        */
-       duration: number;
-       /**
-        * Easing function to use
-        * @default 'easeOutQuart'
-        */
-       easing: EasingFunction;
-
-       /**
-        * Running animation count + FPS display in upper left corner of the chart.
-        * @default false
-        */
-       debug: boolean;
-
-       /**
-        * Delay before starting the animations.
-        * @default 0
-        */
-       delay: number;
-
-       /**
-        *      If set to true, the animations loop endlessly.
-        * @default false
-        */
-       loop: boolean;
-}
-
-export interface AnimationPropertySpec extends AnimationCommonSpec {
-       properties: string[];
-
-       /**
-        * Type of property, determines the interpolator used. Possible values: 'number', 'color' and 'boolean'. Only really needed for 'color', because typeof does not get that right.
-        */
-       type: 'color' | 'number' | 'boolean';
-
-       fn: <T>(from: T, to: T, factor: number) => T;
-
-       /**
-        * Start value for the animation. Current value is used when undefined
-        */
-       from: Color | number | boolean;
-       /**
-        *
-        */
-       to: Color | number | boolean;
-}
-
-export type AnimationSpecContainer = AnimationCommonSpec & {
-       [prop: string]: AnimationPropertySpec;
-};
-
-export type AnimationOptions = AnimationSpecContainer & {
-       /**
-        * Callback called on each step of an animation.
-        */
-       onProgress: (this: Chart, event: AnimationEvent) => void;
-       /**
-        *Callback called when all animations are completed.
-        */
-       onComplete: (this: Chart, event: AnimationEvent) => void;
-
-       active: AnimationSpecContainer;
-       hide: AnimationSpecContainer;
-       reset: AnimationSpecContainer;
-       resize: AnimationSpecContainer;
-       show: AnimationSpecContainer;
-};
-
-export interface ChartAnimationOptions {
-       animation: Scriptable<AnimationOptions>;
-       datasets: {
-               animation: Scriptable<AnimationOptions>;
-       };
-}
-
 export interface ChartMeta<E extends Element = Element, DSE extends Element = Element> {
        type: string;
        controller: DatasetController;
@@ -232,14 +160,6 @@ export interface ChartMeta<E extends Element = Element, DSE extends Element = El
        _parsed: any[];
 }
 
-export interface ParsingOptions {
-       parsing:
-       | {
-               [key: string]: string;
-       }
-       | false;
-}
-
 export interface ActiveDataPoint {
        datasetIndex: number;
        index: number;
@@ -413,32 +333,28 @@ export interface DatasetControllerChartComponent extends ChartComponent {
        };
 }
 
-export interface Defaults {
-       readonly color: string;
-       readonly events: ('mousemove' | 'mouseout' | 'click' | 'touchstart' | 'touchmove')[];
-       readonly font: FontSpec;
-       readonly interaction: {
-               mode: InteractionMode | string;
-               intersect: boolean;
+export interface Defaults extends CoreChartOptions, ElementChartOptions {
+       controllers: {
+               [key in ChartType]: DeepPartial<
+                       CoreChartOptions &
+                       PluginChartOptions &
+                       ElementChartOptions &
+                       DatasetChartOptions<key>[key] &
+                       ScaleChartOptions<key> &
+                       ChartTypeRegistry[key]['chartOptions']
+                       >;
        };
-       readonly hover: {
-               onHover?: () => void;
-               mode?: InteractionMode | string;
-               intersect?: boolean;
+
+       scale: ScaleOptions;
+       scales: {
+               [key in ScaleType]: ScaleOptions<key>;
        };
-       readonly maintainAspectRatio: boolean;
-       readonly onClick?: () => void;
-       readonly onHover?: () => void;
-       readonly responsive: boolean;
 
-       readonly plugins: { [key: string]: any };
-       readonly scale?: ScaleOptions;
-       readonly doughnut: any;
-       readonly scales: { [key: string]: ScaleOptions };
-       readonly controllers: { [key: string]: any };
+       plugins: PluginOptions;
 
        set(scope: string, values: any): any;
        get(scope: string): any;
+
        /**
         * Routes the named defaults to fallback to another scope/name.
         * This routing is useful when those target values, like defaults.color, are changed runtime.
@@ -459,7 +375,7 @@ export interface Defaults {
        route(scope: string, name: string, targetScope: string, targetName: string): void;
 }
 
-export const defaults: Defaults;
+export const defaults: Defaults & DeepPartial<PluginChartOptions>;
 
 export interface Element<T = {}, O = {}> {
        readonly x: number;
@@ -545,7 +461,7 @@ export const Interaction: {
        modes: InteractionModeMap;
 };
 
-export type LayoutPosition = 'left' | 'top' | 'right' | 'chartArea';
+export type LayoutPosition = 'left' | 'top' | 'right' | 'bottom' | 'chartArea';
 
 export interface LayoutItem {
        /**
index 244698c65149fd2397e2e2a86c747166b2c25dff..5edf7ad76d0353917ce18de42a99dc85d18bef9f 100644 (file)
@@ -1,5 +1,6 @@
 import { Chart, Element, InteractionMode } from '.';
 import { ChartDataset } from '../interfaces';
+import { ParsingOptions } from '../controllers';
 
 export type Color = string | CanvasGradient | CanvasPattern;
 
@@ -47,13 +48,6 @@ export interface ChartArea {
   bottom: number;
 }
 
-export interface Padding {
-  top: number;
-  left: number;
-  right: number;
-  bottom: number;
-}
-
 export interface ScriptableContext {
   chart: Chart;
   dataPoint: any;
@@ -68,7 +62,7 @@ export type ScriptableOptions<T> = { [P in keyof T]: Scriptable<T[P]> };
 export type ScriptableAndArray<T> = readonly T[] | Scriptable<T>;
 export type ScriptableAndArrayOptions<T> = { [P in keyof T]: ScriptableAndArray<T[P]> };
 
-export interface HoverInteractionOptions {
+export interface CoreInteractionOptions {
   /**
    * Sets which elements appear in the tooltip. See Interaction Modes for details.
    * @default 'nearest'
@@ -86,11 +80,20 @@ export interface HoverInteractionOptions {
   axis: 'x' | 'y' | 'xy';
 }
 
-export interface ElementOptions {
-  // TODO
+export interface HoverInteractionOptions extends CoreInteractionOptions {
+  /**
+   * Called when any of the events fire. Passed the event, an array of active elements (bars, points, etc), and the chart.
+   */
+  onHover(event: ChartEvent, elements: Element[]): void;
 }
 
-export interface CoreChartOptions {
+export interface CoreChartOptions extends ParsingOptions {
+  animation: Scriptable<AnimationOptions>;
+
+  datasets: {
+    animation: Scriptable<AnimationOptions>;
+  };
+
   /**
    * base color
    * @see Defaults.color
@@ -129,6 +132,8 @@ export interface CoreChartOptions {
    */
   devicePixelRatio: number;
 
+  interaction: CoreInteractionOptions;
+
   hover: HoverInteractionOptions;
 
   /**
@@ -141,13 +146,12 @@ export interface CoreChartOptions {
    * Called when any of the events fire. Passed the event, an array of active elements (bars, points, etc), and the chart.
    */
   onHover(event: ChartEvent, elements: Element[]): void;
+
   /**
    * Called if the event is of type 'mouseup' or 'click'. Passed the event, an array of active elements, and the chart.
    */
   onClick(event: ChartEvent, elements: Element[]): void;
 
-  elements: { [key: string]: ElementOptions };
-
   layout: {
     padding: Scriptable<number | ChartArea>;
   };
@@ -186,6 +190,78 @@ export type EasingFunction =
   | 'easeOutBounce'
   | 'easeInOutBounce';
 
+export interface AnimationCommonSpec {
+       /**
+        * The number of milliseconds an animation takes.
+        * @default 1000
+        */
+       duration: number;
+       /**
+        * Easing function to use
+        * @default 'easeOutQuart'
+        */
+       easing: EasingFunction;
+
+       /**
+        * Running animation count + FPS display in upper left corner of the chart.
+        * @default false
+        */
+       debug: boolean;
+
+       /**
+        * Delay before starting the animations.
+        * @default 0
+        */
+       delay: number;
+
+       /**
+        *      If set to true, the animations loop endlessly.
+        * @default false
+        */
+       loop: boolean;
+}
+
+export interface AnimationPropertySpec extends AnimationCommonSpec {
+       properties: string[];
+
+       /**
+        * Type of property, determines the interpolator used. Possible values: 'number', 'color' and 'boolean'. Only really needed for 'color', because typeof does not get that right.
+        */
+       type: 'color' | 'number' | 'boolean';
+
+       fn: <T>(from: T, to: T, factor: number) => T;
+
+       /**
+        * Start value for the animation. Current value is used when undefined
+        */
+       from: Color | number | boolean;
+       /**
+        *
+        */
+       to: Color | number | boolean;
+}
+
+export type AnimationSpecContainer = AnimationCommonSpec & {
+       [prop: string]: AnimationPropertySpec;
+};
+
+export type AnimationOptions = AnimationSpecContainer & {
+       /**
+        * Callback called on each step of an animation.
+        */
+       onProgress: (this: Chart, event: AnimationEvent) => void;
+       /**
+        *Callback called when all animations are completed.
+        */
+       onComplete: (this: Chart, event: AnimationEvent) => void;
+
+       active: AnimationSpecContainer;
+       hide: AnimationSpecContainer;
+       reset: AnimationSpecContainer;
+       resize: AnimationSpecContainer;
+       show: AnimationSpecContainer;
+};
+
 export interface FontSpec {
   /**
    * Default font color for all text.
index 3630c0d6bf6b9e23f1c5e3998301d0663fe862c2..7d2ff4dbd201bdf9373fca9591d153f9710b74e3 100644 (file)
@@ -22,13 +22,8 @@ import {
 } from './controllers';
 import { CoreChartOptions } from './core/interfaces';
 import { ElementChartOptions } from './elements';
-import {
-  TooltipChartOptions,
-  FillerControllerDatasetOptions,
-  LegendChartOptions,
-  TitleChartOptions,
-} from './plugins';
-import { ChartAnimationOptions, ParsingOptions as ParsingOptions, Plugin } from './core';
+import { FillerControllerDatasetOptions, PluginChartOptions } from './plugins';
+import { Plugin } from './core';
 import {
   LinearScaleOptions,
   LogarithmicScaleOptions,
@@ -145,11 +140,7 @@ export type ScaleChartOptions<TYPE extends ChartType = ChartType> = {
 
 export type ChartOptions<TYPE extends ChartType = ChartType> = DeepPartial<
   CoreChartOptions &
-  ParsingOptions &
-  TooltipChartOptions &
-  LegendChartOptions &
-  TitleChartOptions &
-  ChartAnimationOptions &
+  PluginChartOptions &
   ElementChartOptions &
   DatasetChartOptions<TYPE> &
   ScaleChartOptions<TYPE> &
@@ -169,7 +160,6 @@ export type ChartDataset<
   TYPE extends ChartType = ChartType,
   DATA extends unknown[] = DefaultDataPoint<TYPE>
 > = DeepPartial<
-  ParsingOptions &
   { [key in ChartType]: { type: key } & ChartTypeRegistry[key]['datasetOptions'] }[TYPE]
 > & ChartDatasetProperties<TYPE, DATA>;
 
index 24a59b5cfb01c6b25fdad31186b8fb3409a91ee1..3d458df3fe41527c76e6059c2369cadd8815b1d4 100644 (file)
@@ -1,5 +1,5 @@
-import { ActiveDataPoint, ActiveElement, Chart, Element, AnimationSpecContainer, InteractionMode, LayoutPosition, Plugin } from '../core';
-import { Color, ChartArea, FontSpec, Scriptable, TextAlign, ChartEvent, HoverInteractionOptions } from '../core/interfaces';
+import { ActiveDataPoint, ActiveElement, Chart, Element, InteractionMode, LayoutPosition, Plugin } from '../core';
+import { AnimationSpecContainer, Color, ChartArea, FontSpec, Scriptable, TextAlign, ChartEvent, CoreInteractionOptions } from '../core/interfaces';
 import { PointStyle } from '../elements';
 import { ChartData, ChartDataset } from '../interfaces';
 
@@ -342,7 +342,7 @@ export interface TooltipPlugin<O = {}> {
   afterTooltipDraw?(chart: Chart, args: { tooltip: TooltipModel }, options: O): void;
 }
 
-export interface TooltipOptions extends HoverInteractionOptions {
+export interface TooltipOptions extends CoreInteractionOptions {
   /**
    * Are on-canvas tooltips enabled?
    * @default true
@@ -551,3 +551,13 @@ export interface TooltipItem {
    */
   element: Element;
 }
+
+export interface PluginOptions {
+  filler: FillerOptions;
+  legend: LegendOptions;
+  title: TitleOptions;
+  tooltip: TooltipOptions;
+}
+
+export interface PluginChartOptions extends LegendChartOptions, TitleChartOptions, TooltipChartOptions {
+}