UNPKG

chart.js

Version:

Simple HTML5 charts using the canvas element.

1,406 lines (1,259 loc) 118 kB
/* eslint-disable @typescript-eslint/ban-types */ import {DeepPartial, DistributiveArray, UnionToIntersection} from './utils.js'; import {TimeUnit} from '../core/core.adapters.js'; import PointElement from '../elements/element.point.js'; import {EasingFunction} from '../helpers/helpers.easing.js'; import {AnimationEvent} from './animation.js'; import {AnyObject, EmptyObject} from './basic.js'; import {Color} from './color.js'; import Element from '../core/core.element.js'; import {ChartArea, Padding, Point} from './geometric.js'; import {LayoutItem, LayoutPosition} from './layout.js'; import {ColorsPluginOptions} from '../plugins/plugin.colors.js'; export {EasingFunction} from '../helpers/helpers.easing.js'; export {default as ArcElement, ArcProps} from '../elements/element.arc.js'; export {default as PointElement, PointProps} from '../elements/element.point.js'; export {Animation, Animations, Animator, AnimationEvent} from './animation.js'; export {Color} from './color.js'; export {ChartArea, Point, TRBL} from './geometric.js'; export {LayoutItem, LayoutPosition} from './layout.js'; export interface ScriptableContext<TType extends ChartType> { active: boolean; chart: Chart; dataIndex: number; dataset: UnionToIntersection<ChartDataset<TType>>; datasetIndex: number; type: string; mode: string; parsed: UnionToIntersection<ParsedDataType<TType>>; raw: unknown; } export interface ScriptableLineSegmentContext { type: 'segment', p0: PointElement, p1: PointElement, p0DataIndex: number, p1DataIndex: number, datasetIndex: number } 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 ScriptableAndScriptableOptions<T, TContext> = Scriptable<T, TContext> | ScriptableOptions<T, TContext>; export type ScriptableAndArray<T, TContext> = readonly T[] | Scriptable<T, TContext>; export type ScriptableAndArrayOptions<T, TContext> = { [P in keyof T]: ScriptableAndArray<T[P], TContext> }; export interface ParsingOptions { /** * 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 { /** * The base axis of the chart. 'x' for vertical charts and 'y' for horizontal charts. * @default 'x' */ indexAxis: 'x' | 'y'; /** * 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 | false; /** * The label for the dataset which appears in the legend and tooltips. */ label: string; /** * The drawing order of dataset. Also affects order for stacking, tooltip and legend. */ order: number; /** * The ID of the group to which this dataset belongs to (when stacked, each group will be a separate stack). */ stack: string; /** * Configures the visibility state of the dataset. Set it to true, to hide the dataset from the chart. * @default false */ hidden: boolean; } export interface BarControllerDatasetOptions extends ControllerDatasetOptions, ScriptableAndArrayOptions<BarOptions, ScriptableContext<'bar'>>, ScriptableAndArrayOptions<CommonHoverOptions, ScriptableContext<'bar'>>, AnimationOptions<'bar'> { /** * The ID of the x axis to plot this dataset on. */ xAxisID: string; /** * The ID of the y axis to plot this dataset on. */ yAxisID: string; /** * Percent (0-1) of the available width each bar should be within the category width. 1.0 will take the whole category width and put the bars right next to each other. * @default 0.9 */ barPercentage: number; /** * Percent (0-1) of the available width each category should be within the sample width. * @default 0.8 */ categoryPercentage: number; /** * Manually set width of each bar in pixels. If set to 'flex', it computes "optimal" sample widths that globally arrange bars side by side. If not set (default), bars are equally sized based on the smallest interval. */ barThickness: number | 'flex'; /** * Set this to ensure that bars are not sized thicker than this. */ maxBarThickness: number; /** * Set this to ensure that bars have a minimum length in pixels. */ minBarLength: number; /** * Point style for the legend * @default 'circle; */ pointStyle: PointStyle; /** * Should the bars be grouped on index axis * @default true */ grouped: boolean; } export interface BarControllerChartOptions { /** * Should null or undefined values be omitted from drawing */ skipNull?: boolean; } export type BarController = DatasetController export declare const BarController: ChartComponent & { prototype: BarController; new (chart: Chart, datasetIndex: number): BarController; }; export interface BubbleControllerDatasetOptions extends ControllerDatasetOptions, ScriptableAndArrayOptions<PointOptions, ScriptableContext<'bubble'>>, ScriptableAndArrayOptions<PointHoverOptions, ScriptableContext<'bubble'>> { /** * The ID of the x axis to plot this dataset on. */ xAxisID: string; /** * The ID of the y axis to plot this dataset on. */ yAxisID: string; } export interface BubbleDataPoint extends Point { /** * Bubble radius in pixels (not scaled). */ r?: number; } export type BubbleController = DatasetController export declare const BubbleController: ChartComponent & { prototype: BubbleController; new (chart: Chart, datasetIndex: number): BubbleController; }; export interface LineControllerDatasetOptions extends ControllerDatasetOptions, ScriptableAndArrayOptions<PointPrefixedOptions, ScriptableContext<'line'>>, ScriptableAndArrayOptions<PointPrefixedHoverOptions, ScriptableContext<'line'>>, ScriptableOptions<Omit<LineOptions, keyof CommonElementOptions>, ScriptableContext<'line'>>, ScriptableAndArrayOptions<CommonElementOptions, ScriptableContext<'line'>>, ScriptableOptions<Omit<LineHoverOptions, keyof CommonHoverOptions>, ScriptableContext<'line'>>, ScriptableAndArrayOptions<CommonHoverOptions, ScriptableContext<'line'>>, AnimationOptions<'line'> { /** * The ID of the x axis to plot this dataset on. */ xAxisID: string; /** * The ID of the y axis to plot this dataset on. */ yAxisID: string; /** * If true, lines will be drawn between points with no or null data. If false, points with NaN data will create a break in the line. Can also be a number specifying the maximum gap length to span. The unit of the value depends on the scale used. * @default false */ spanGaps: boolean | number; showLine: boolean; } export interface LineControllerChartOptions { /** * If true, lines will be drawn between points with no or null data. If false, points with NaN data will create a break in the line. Can also be a number specifying the maximum gap length to span. The unit of the value depends on the scale used. * @default false */ spanGaps: boolean | number; /** * If false, the lines between points are not drawn. * @default true */ showLine: boolean; } export type LineController = DatasetController export declare const LineController: ChartComponent & { prototype: LineController; new (chart: Chart, datasetIndex: number): LineController; }; export type ScatterControllerDatasetOptions = LineControllerDatasetOptions; export type ScatterDataPoint = Point export type ScatterControllerChartOptions = LineControllerChartOptions; export type ScatterController = LineController export declare const ScatterController: ChartComponent & { prototype: ScatterController; new (chart: Chart, datasetIndex: number): ScatterController; }; export interface DoughnutControllerDatasetOptions extends ControllerDatasetOptions, ScriptableAndArrayOptions<ArcOptions, ScriptableContext<'doughnut'>>, ScriptableAndArrayOptions<ArcHoverOptions, ScriptableContext<'doughnut'>>, AnimationOptions<'doughnut'> { /** * Sweep to allow arcs to cover. * @default 360 */ circumference: number; /** * Arc offset (in pixels). */ offset: number | number[]; /** * Starting angle to draw this dataset from. * @default 0 */ rotation: number; /** * The relative thickness of the dataset. Providing a value for weight will cause the pie or doughnut dataset to be drawn with a thickness relative to the sum of all the dataset weight values. * @default 1 */ weight: number; /** * Similar to the `offset` option, but applies to all arcs. This can be used to to add spaces * between arcs * @default 0 */ spacing: number; } export interface DoughnutAnimationOptions extends AnimationSpec<'doughnut'> { /** * If true, the chart will animate in with a rotation animation. This property is in the options.animation object. * @default true */ animateRotate: boolean; /** * If true, will animate scaling the chart from the center outwards. * @default false */ animateScale: boolean; } export interface DoughnutControllerChartOptions { /** * Sweep to allow arcs to cover. * @default 360 */ circumference: number; /** * The portion of the chart that is cut out of the middle. ('50%' - for doughnut, 0 - for pie) * String ending with '%' means percentage, number means pixels. * @default 50 */ cutout: Scriptable<number | string, ScriptableContext<'doughnut'>>; /** * Arc offset (in pixels). */ offset: number | number[]; /** * The outer radius of the chart. String ending with '%' means percentage of maximum radius, number means pixels. * @default '100%' */ radius: Scriptable<number | string, ScriptableContext<'doughnut'>>; /** * Starting angle to draw arcs from. * @default 0 */ rotation: number; /** * Spacing between the arcs * @default 0 */ spacing: number; animation: false | DoughnutAnimationOptions; } export type DoughnutDataPoint = number; export interface DoughnutController extends DatasetController { readonly innerRadius: number; readonly outerRadius: number; readonly offsetX: number; readonly offsetY: number; calculateTotal(): number; calculateCircumference(value: number): number; } export declare const DoughnutController: ChartComponent & { prototype: DoughnutController; new (chart: Chart, datasetIndex: number): DoughnutController; }; export interface DoughnutMetaExtensions { total: number; } export type PieControllerDatasetOptions = DoughnutControllerDatasetOptions; export type PieControllerChartOptions = DoughnutControllerChartOptions; export type PieAnimationOptions = DoughnutAnimationOptions; export type PieDataPoint = DoughnutDataPoint; export type PieMetaExtensions = DoughnutMetaExtensions; export type PieController = DoughnutController export declare const PieController: ChartComponent & { prototype: PieController; new (chart: Chart, datasetIndex: number): PieController; }; export interface PolarAreaControllerDatasetOptions extends DoughnutControllerDatasetOptions { /** * Arc angle to cover. - for polar only * @default circumference / (arc count) */ angle: number; } export type PolarAreaAnimationOptions = DoughnutAnimationOptions; export interface PolarAreaControllerChartOptions { /** * Starting angle to draw arcs for the first item in a dataset. In degrees, 0 is at top. * @default 0 */ startAngle: number; animation: false | PolarAreaAnimationOptions; } export interface PolarAreaController extends DoughnutController { countVisibleElements(): number; } export declare const PolarAreaController: ChartComponent & { prototype: PolarAreaController; new (chart: Chart, datasetIndex: number): PolarAreaController; }; export interface RadarControllerDatasetOptions extends ControllerDatasetOptions, ScriptableAndArrayOptions<PointOptions & PointHoverOptions & PointPrefixedOptions & PointPrefixedHoverOptions, ScriptableContext<'radar'>>, ScriptableAndArrayOptions<LineOptions & LineHoverOptions, ScriptableContext<'radar'>>, AnimationOptions<'radar'> { /** * The ID of the x axis to plot this dataset on. */ xAxisID: string; /** * The ID of the y axis to plot this dataset on. */ yAxisID: string; /** * If true, lines will be drawn between points with no or null data. If false, points with NaN data will create a break in the line. Can also be a number specifying the maximum gap length to span. The unit of the value depends on the scale used. */ spanGaps: boolean | number; /** * If false, the line is not drawn for this dataset. */ showLine: boolean; } export type RadarControllerChartOptions = LineControllerChartOptions; export type RadarController = DatasetController export declare const RadarController: ChartComponent & { prototype: RadarController; new (chart: Chart, datasetIndex: number): RadarController; }; interface ChartMetaClip { left: number | boolean; top: number | boolean; right: number | boolean; bottom: number | boolean; disabled: boolean; } interface ChartMetaCommon<TElement extends Element = Element, TDatasetElement extends Element = Element> { type: string; controller: DatasetController; order: number; label: string; index: number; visible: boolean; stack: number; indexAxis: 'x' | 'y'; data: TElement[]; dataset?: TDatasetElement; hidden: boolean; xAxisID?: string; yAxisID?: string; rAxisID?: string; iAxisID: string; vAxisID: string; xScale?: Scale; yScale?: Scale; rScale?: Scale; iScale?: Scale; vScale?: Scale; _sorted: boolean; _stacked: boolean | 'single'; _parsed: unknown[]; _clip: ChartMetaClip; } export type ChartMeta< TType extends ChartType = ChartType, TElement extends Element = Element, TDatasetElement extends Element = Element, > = DeepPartial< { [key in ChartType]: ChartTypeRegistry[key]['metaExtensions'] }[TType] > & ChartMetaCommon<TElement, TDatasetElement>; export interface ActiveDataPoint { datasetIndex: number; index: number; } export interface ActiveElement extends ActiveDataPoint { element: Element; } export declare class Chart< TType extends ChartType = ChartType, TData = DefaultDataPoint<TType>, TLabel = unknown > { readonly platform: BasePlatform; readonly id: string; readonly canvas: HTMLCanvasElement; readonly ctx: CanvasRenderingContext2D; readonly config: ChartConfiguration<TType, TData, TLabel> | ChartConfigurationCustomTypesPerDataset<TType, TData, TLabel>; readonly width: number; readonly height: number; readonly aspectRatio: number; readonly boxes: LayoutItem[]; readonly currentDevicePixelRatio: number; readonly chartArea: ChartArea; readonly scales: { [key: string]: Scale }; readonly attached: boolean; readonly legend?: LegendElement<TType>; // Only available if legend plugin is registered and enabled readonly tooltip?: TooltipModel<TType>; // Only available if tooltip plugin is registered and enabled data: ChartData<TType, TData, TLabel>; options: ChartOptions<TType>; constructor(item: ChartItem, config: ChartConfiguration<TType, TData, TLabel> | ChartConfigurationCustomTypesPerDataset<TType, TData, TLabel>); clear(): this; stop(): this; resize(width?: number, height?: number): void; ensureScalesHaveIDs(): void; buildOrUpdateScales(): void; buildOrUpdateControllers(): void; reset(): void; update(mode?: UpdateMode | ((ctx: { datasetIndex: number }) => UpdateMode)): void; render(): void; draw(): void; isPointInArea(point: Point): boolean; getElementsAtEventForMode(e: Event, mode: string, options: InteractionOptions, useFinalPosition: boolean): InteractionItem[]; getSortedVisibleDatasetMetas(): ChartMeta[]; getDatasetMeta(datasetIndex: number): ChartMeta; getVisibleDatasetCount(): number; isDatasetVisible(datasetIndex: number): boolean; setDatasetVisibility(datasetIndex: number, visible: boolean): void; toggleDataVisibility(index: number): void; getDataVisibility(index: number): boolean; hide(datasetIndex: number, dataIndex?: number): void; show(datasetIndex: number, dataIndex?: number): void; getActiveElements(): ActiveElement[]; setActiveElements(active: ActiveDataPoint[]): void; destroy(): void; toBase64Image(type?: string, quality?: unknown): string; bindEvents(): void; unbindEvents(): void; updateHoverStyle(items: InteractionItem[], mode: 'dataset', enabled: boolean): void; notifyPlugins(hook: string, args?: AnyObject): boolean | void; isPluginEnabled(pluginId: string): boolean; getContext(): { chart: Chart, type: string }; static readonly defaults: Defaults; static readonly overrides: Overrides; static readonly version: string; static readonly instances: { [key: string]: Chart }; static readonly registry: Registry; static getChart(key: string | CanvasRenderingContext2D | HTMLCanvasElement): Chart | undefined; static register(...items: ChartComponentLike[]): void; static unregister(...items: ChartComponentLike[]): void; } export declare const registerables: readonly ChartComponentLike[]; export declare type ChartItem = | string | CanvasRenderingContext2D | HTMLCanvasElement | { canvas: HTMLCanvasElement } | ArrayLike<CanvasRenderingContext2D | HTMLCanvasElement>; export declare enum UpdateModeEnum { resize = 'resize', reset = 'reset', none = 'none', hide = 'hide', show = 'show', default = 'default', active = 'active' } export type UpdateMode = keyof typeof UpdateModeEnum; export declare class DatasetController< TType extends ChartType = ChartType, TElement extends Element = Element, TDatasetElement extends Element = Element, TParsedData = ParsedDataType<TType>, > { constructor(chart: Chart, datasetIndex: number); readonly chart: Chart; readonly index: number; readonly _cachedMeta: ChartMeta<TType, TElement, TDatasetElement>; enableOptionSharing: boolean; // If true, the controller supports the decimation // plugin. Defaults to `false` for all controllers // except the LineController supportsDecimation: boolean; linkScales(): void; getAllParsedValues(scale: Scale): number[]; protected getLabelAndValue(index: number): { label: string; value: string }; updateElements(elements: TElement[], start: number, count: number, mode: UpdateMode): void; update(mode: UpdateMode): void; updateIndex(datasetIndex: number): void; protected getMaxOverflow(): boolean | number; draw(): void; reset(): void; getDataset(): ChartDataset; getMeta(): ChartMeta<TType, TElement, TDatasetElement>; getScaleForId(scaleID: string): Scale | undefined; configure(): void; initialize(): void; addElements(): void; buildOrUpdateElements(resetNewElements?: boolean): void; getStyle(index: number, active: boolean): AnyObject; protected resolveDatasetElementOptions(mode: UpdateMode): AnyObject; protected resolveDataElementOptions(index: number, mode: UpdateMode): AnyObject; /** * Utility for checking if the options are shared and should be animated separately. * @protected */ protected getSharedOptions(options: AnyObject): undefined | AnyObject; /** * Utility for determining if `options` should be included in the updated properties * @protected */ protected includeOptions(mode: UpdateMode, sharedOptions: AnyObject): boolean; /** * Utility for updating an element with new properties, using animations when appropriate. * @protected */ protected updateElement(element: TElement | TDatasetElement, index: number | undefined, properties: AnyObject, mode: UpdateMode): void; /** * Utility to animate the shared options, that are potentially affecting multiple elements. * @protected */ protected updateSharedOptions(sharedOptions: AnyObject, mode: UpdateMode, newOptions: AnyObject): void; removeHoverStyle(element: TElement, datasetIndex: number, index: number): void; setHoverStyle(element: TElement, datasetIndex: number, index: number): void; parse(start: number, count: number): void; protected parsePrimitiveData(meta: ChartMeta<TType, TElement, TDatasetElement>, data: AnyObject[], start: number, count: number): AnyObject[]; protected parseArrayData(meta: ChartMeta<TType, TElement, TDatasetElement>, data: AnyObject[], start: number, count: number): AnyObject[]; protected parseObjectData(meta: ChartMeta<TType, TElement, TDatasetElement>, data: AnyObject[], start: number, count: number): AnyObject[]; protected getParsed(index: number): TParsedData; protected applyStack(scale: Scale, parsed: unknown[]): number; protected updateRangeFromParsed( range: { min: number; max: number }, scale: Scale, parsed: unknown[], stack: boolean | string ): void; protected getMinMax(scale: Scale, canStack?: boolean): { min: number; max: number }; } export interface DatasetControllerChartComponent extends ChartComponent { defaults: { datasetElementType?: string | null | false; dataElementType?: string | null | false; }; } export interface Defaults extends CoreChartOptions<ChartType>, ElementChartOptions<ChartType>, PluginChartOptions<ChartType> { scale: ScaleOptionsByType; scales: { [key in ScaleType]: ScaleOptionsByType<key>; }; set(values: AnyObject): AnyObject; set(scope: string, values: AnyObject): AnyObject; get(scope: string): AnyObject; describe(scope: string, values: AnyObject): AnyObject; override(scope: string, values: AnyObject): AnyObject; /** * Routes the named defaults to fallback to another scope/name. * This routing is useful when those target values, like defaults.color, are changed runtime. * If the values would be copied, the runtime change would not take effect. By routing, the * fallback is evaluated at each access, so its always up to date. * * Example: * * defaults.route('elements.arc', 'backgroundColor', '', 'color') * - reads the backgroundColor from defaults.color when undefined locally * * @param scope Scope this route applies to. * @param name Property name that should be routed to different namespace when not defined here. * @param targetScope The namespace where those properties should be routed to. * Empty string ('') is the root of defaults. * @param targetName The target name in the target scope the property should be routed to. */ route(scope: string, name: string, targetScope: string, targetName: string): void; } export type Overrides = { [key in ChartType]: CoreChartOptions<key> & ElementChartOptions<key> & PluginChartOptions<key> & DatasetChartOptions<ChartType> & ScaleChartOptions<key> & ChartTypeRegistry[key]['chartOptions']; } export declare const defaults: Defaults; export interface InteractionOptions { axis?: string; intersect?: boolean; includeInvisible?: boolean; } export interface InteractionItem { element: Element; datasetIndex: number; index: number; } export type InteractionModeFunction = ( chart: Chart, e: ChartEvent, options: InteractionOptions, useFinalPosition?: boolean ) => InteractionItem[]; export interface InteractionModeMap { /** * Returns items at the same index. If the options.intersect parameter is true, we only return items if we intersect something * If the options.intersect mode is false, we find the nearest item and return the items at the same index as that item */ index: InteractionModeFunction; /** * Returns items in the same dataset. If the options.intersect parameter is true, we only return items if we intersect something * If the options.intersect is false, we find the nearest item and return the items in that dataset */ dataset: InteractionModeFunction; /** * Point mode returns all elements that hit test based on the event position * of the event */ point: InteractionModeFunction; /** * nearest mode returns the element closest to the point */ nearest: InteractionModeFunction; /** * x mode returns the elements that hit-test at the current x coordinate */ x: InteractionModeFunction; /** * y mode returns the elements that hit-test at the current y coordinate */ y: InteractionModeFunction; } export type InteractionMode = keyof InteractionModeMap; export declare const Interaction: { modes: InteractionModeMap; /** * Helper function to select candidate elements for interaction */ evaluateInteractionItems( chart: Chart, axis: InteractionAxis, position: Point, handler: (element: Element & VisualElement, datasetIndex: number, index: number) => void, intersect?: boolean ): InteractionItem[]; }; export declare const layouts: { /** * Register a box to a chart. * A box is simply a reference to an object that requires layout. eg. Scales, Legend, Title. * @param {Chart} chart - the chart to use * @param {LayoutItem} item - the item to add to be laid out */ addBox(chart: Chart, item: LayoutItem): void; /** * Remove a layoutItem from a chart * @param {Chart} chart - the chart to remove the box from * @param {LayoutItem} layoutItem - the item to remove from the layout */ removeBox(chart: Chart, layoutItem: LayoutItem): void; /** * Sets (or updates) options on the given `item`. * @param {Chart} chart - the chart in which the item lives (or will be added to) * @param {LayoutItem} item - the item to configure with the given options * @param options - the new item options. */ configure( chart: Chart, item: LayoutItem, options: { fullSize?: number; position?: LayoutPosition; weight?: number } ): void; /** * Fits boxes of the given chart into the given size by having each box measure itself * then running a fitting algorithm * @param {Chart} chart - the chart * @param {number} width - the width to fit into * @param {number} height - the height to fit into */ update(chart: Chart, width: number, height: number): void; }; export interface Plugin<TType extends ChartType = ChartType, O = AnyObject> extends ExtendedPlugin<TType, O> { id: string; /** * The events option defines the browser events that the plugin should listen. * @default ['mousemove', 'mouseout', 'click', 'touchstart', 'touchmove'] */ events?: (keyof HTMLElementEventMap)[] /** * @desc Called when plugin is installed for this chart instance. This hook is also invoked for disabled plugins (options === false). * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {object} options - The plugin options. * @since 3.0.0 */ install?(chart: Chart<TType>, args: EmptyObject, options: O): void; /** * @desc Called when a plugin is starting. This happens when chart is created or plugin is enabled. * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {object} options - The plugin options. * @since 3.0.0 */ start?(chart: Chart<TType>, args: EmptyObject, options: O): void; /** * @desc Called when a plugin stopping. This happens when chart is destroyed or plugin is disabled. * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {object} options - The plugin options. * @since 3.0.0 */ stop?(chart: Chart<TType>, args: EmptyObject, options: O): void; /** * @desc Called before initializing `chart`. * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {object} options - The plugin options. */ beforeInit?(chart: Chart<TType>, args: EmptyObject, options: O): void; /** * @desc Called after `chart` has been initialized and before the first update. * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {object} options - The plugin options. */ afterInit?(chart: Chart<TType>, args: EmptyObject, options: O): void; /** * @desc Called before updating `chart`. If any plugin returns `false`, the update * is cancelled (and thus subsequent render(s)) until another `update` is triggered. * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {UpdateMode} args.mode - The update mode * @param {object} options - The plugin options. * @returns {boolean} `false` to cancel the chart update. */ beforeUpdate?(chart: Chart<TType>, args: { mode: UpdateMode, cancelable: true }, options: O): boolean | void; /** * @desc Called after `chart` has been updated and before rendering. Note that this * hook will not be called if the chart update has been previously cancelled. * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {UpdateMode} args.mode - The update mode * @param {object} options - The plugin options. */ afterUpdate?(chart: Chart<TType>, args: { mode: UpdateMode }, options: O): void; /** * @desc Called during the update process, before any chart elements have been created. * This can be used for data decimation by changing the data array inside a dataset. * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {object} options - The plugin options. */ beforeElementsUpdate?(chart: Chart<TType>, args: EmptyObject, options: O): void; /** * @desc Called during chart reset * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {object} options - The plugin options. * @since version 3.0.0 */ reset?(chart: Chart<TType>, args: EmptyObject, options: O): void; /** * @desc Called before updating the `chart` datasets. If any plugin returns `false`, * the datasets update is cancelled until another `update` is triggered. * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {UpdateMode} args.mode - The update mode. * @param {object} options - The plugin options. * @returns {boolean} false to cancel the datasets update. * @since version 2.1.5 */ beforeDatasetsUpdate?(chart: Chart<TType>, args: { mode: UpdateMode }, options: O): boolean | void; /** * @desc Called after the `chart` datasets have been updated. Note that this hook * will not be called if the datasets update has been previously cancelled. * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {UpdateMode} args.mode - The update mode. * @param {object} options - The plugin options. * @since version 2.1.5 */ afterDatasetsUpdate?(chart: Chart<TType>, args: { mode: UpdateMode, cancelable: true }, options: O): void; /** * @desc Called before updating the `chart` dataset at the given `args.index`. If any plugin * returns `false`, the datasets update is cancelled until another `update` is triggered. * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {number} args.index - The dataset index. * @param {object} args.meta - The dataset metadata. * @param {UpdateMode} args.mode - The update mode. * @param {object} options - The plugin options. * @returns {boolean} `false` to cancel the chart datasets drawing. */ beforeDatasetUpdate?(chart: Chart<TType>, args: { index: number; meta: ChartMeta, mode: UpdateMode, cancelable: true }, options: O): boolean | void; /** * @desc Called after the `chart` datasets at the given `args.index` has been updated. Note * that this hook will not be called if the datasets update has been previously cancelled. * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {number} args.index - The dataset index. * @param {object} args.meta - The dataset metadata. * @param {UpdateMode} args.mode - The update mode. * @param {object} options - The plugin options. */ afterDatasetUpdate?(chart: Chart<TType>, args: { index: number; meta: ChartMeta, mode: UpdateMode, cancelable: false }, options: O): void; /** * @desc Called before laying out `chart`. If any plugin returns `false`, * the layout update is cancelled until another `update` is triggered. * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {object} options - The plugin options. * @returns {boolean} `false` to cancel the chart layout. */ beforeLayout?(chart: Chart<TType>, args: { cancelable: true }, options: O): boolean | void; /** * @desc Called before scale data limits are calculated. This hook is called separately for each scale in the chart. * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {Scale} args.scale - The scale. * @param {object} options - The plugin options. */ beforeDataLimits?(chart: Chart<TType>, args: { scale: Scale }, options: O): void; /** * @desc Called after scale data limits are calculated. This hook is called separately for each scale in the chart. * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {Scale} args.scale - The scale. * @param {object} options - The plugin options. */ afterDataLimits?(chart: Chart<TType>, args: { scale: Scale }, options: O): void; /** * @desc Called before scale builds its ticks. This hook is called separately for each scale in the chart. * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {Scale} args.scale - The scale. * @param {object} options - The plugin options. */ beforeBuildTicks?(chart: Chart<TType>, args: { scale: Scale }, options: O): void; /** * @desc Called after scale has build its ticks. This hook is called separately for each scale in the chart. * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {Scale} args.scale - The scale. * @param {object} options - The plugin options. */ afterBuildTicks?(chart: Chart<TType>, args: { scale: Scale }, options: O): void; /** * @desc Called after the `chart` has been laid out. Note that this hook will not * be called if the layout update has been previously cancelled. * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {object} options - The plugin options. */ afterLayout?(chart: Chart<TType>, args: EmptyObject, options: O): void; /** * @desc Called before rendering `chart`. If any plugin returns `false`, * the rendering is cancelled until another `render` is triggered. * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {object} options - The plugin options. * @returns {boolean} `false` to cancel the chart rendering. */ beforeRender?(chart: Chart<TType>, args: { cancelable: true }, options: O): boolean | void; /** * @desc Called after the `chart` has been fully rendered (and animation completed). Note * that this hook will not be called if the rendering has been previously cancelled. * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {object} options - The plugin options. */ afterRender?(chart: Chart<TType>, args: EmptyObject, options: O): void; /** * @desc Called before drawing `chart` at every animation frame. If any plugin returns `false`, * the frame drawing is cancelled untilanother `render` is triggered. * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {object} options - The plugin options. * @returns {boolean} `false` to cancel the chart drawing. */ beforeDraw?(chart: Chart<TType>, args: { cancelable: true }, options: O): boolean | void; /** * @desc Called after the `chart` has been drawn. Note that this hook will not be called * if the drawing has been previously cancelled. * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {object} options - The plugin options. */ afterDraw?(chart: Chart<TType>, args: EmptyObject, options: O): void; /** * @desc Called before drawing the `chart` datasets. If any plugin returns `false`, * the datasets drawing is cancelled until another `render` is triggered. * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {object} options - The plugin options. * @returns {boolean} `false` to cancel the chart datasets drawing. */ beforeDatasetsDraw?(chart: Chart<TType>, args: { cancelable: true }, options: O): boolean | void; /** * @desc Called after the `chart` datasets have been drawn. Note that this hook * will not be called if the datasets drawing has been previously cancelled. * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {object} options - The plugin options. */ afterDatasetsDraw?(chart: Chart<TType>, args: EmptyObject, options: O, cancelable: false): void; /** * @desc Called before drawing the `chart` dataset at the given `args.index` (datasets * are drawn in the reverse order). If any plugin returns `false`, the datasets drawing * is cancelled until another `render` is triggered. * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {number} args.index - The dataset index. * @param {object} args.meta - The dataset metadata. * @param {object} options - The plugin options. * @returns {boolean} `false` to cancel the chart datasets drawing. */ beforeDatasetDraw?(chart: Chart<TType>, args: { index: number; meta: ChartMeta }, options: O): boolean | void; /** * @desc Called after the `chart` datasets at the given `args.index` have been drawn * (datasets are drawn in the reverse order). Note that this hook will not be called * if the datasets drawing has been previously cancelled. * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {number} args.index - The dataset index. * @param {object} args.meta - The dataset metadata. * @param {object} options - The plugin options. */ afterDatasetDraw?(chart: Chart<TType>, args: { index: number; meta: ChartMeta }, options: O): void; /** * @desc Called before processing the specified `event`. If any plugin returns `false`, * the event will be discarded. * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {ChartEvent} args.event - The event object. * @param {boolean} args.replay - True if this event is replayed from `Chart.update` * @param {boolean} args.inChartArea - The event position is inside chartArea * @param {boolean} [args.changed] - Set to true if the plugin needs a render. Should only be changed to true, because this args object is passed through all plugins. * @param {object} options - The plugin options. */ beforeEvent?(chart: Chart<TType>, args: { event: ChartEvent, replay: boolean, changed?: boolean; cancelable: true, inChartArea: boolean }, options: O): boolean | void; /** * @desc Called after the `event` has been consumed. Note that this hook * will not be called if the `event` has been previously discarded. * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {ChartEvent} args.event - The event object. * @param {boolean} args.replay - True if this event is replayed from `Chart.update` * @param {boolean} args.inChartArea - The event position is inside chartArea * @param {boolean} [args.changed] - Set to true if the plugin needs a render. Should only be changed to true, because this args object is passed through all plugins. * @param {object} options - The plugin options. */ afterEvent?(chart: Chart<TType>, args: { event: ChartEvent, replay: boolean, changed?: boolean, cancelable: false, inChartArea: boolean }, options: O): void; /** * @desc Called after the chart as been resized. * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {number} args.size - The new canvas display size (eq. canvas.style width & height). * @param {object} options - The plugin options. */ resize?(chart: Chart<TType>, args: { size: { width: number, height: number } }, options: O): void; /** * Called before the chart is being destroyed. * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {object} options - The plugin options. */ beforeDestroy?(chart: Chart<TType>, args: EmptyObject, options: O): void; /** * Called after the chart has been destroyed. * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {object} options - The plugin options. */ afterDestroy?(chart: Chart<TType>, args: EmptyObject, options: O): void; /** * Called after chart is destroyed on all plugins that were installed for that chart. This hook is also invoked for disabled plugins (options === false). * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {object} options - The plugin options. * @since 3.0.0 */ uninstall?(chart: Chart<TType>, args: EmptyObject, options: O): void; /** * Default options used in the plugin */ defaults?: Partial<O>; } export declare type ChartComponentLike = ChartComponent | ChartComponent[] | { [key: string]: ChartComponent } | Plugin | Plugin[]; /** * Please use the module's default export which provides a singleton instance * Note: class is exported for typedoc */ export interface Registry { readonly controllers: TypedRegistry<DatasetController>; readonly elements: TypedRegistry<Element>; readonly plugins: TypedRegistry<Plugin>; readonly scales: TypedRegistry<Scale>; add(...args: ChartComponentLike[]): void; remove(...args: ChartComponentLike[]): void; addControllers(...args: ChartComponentLike[]): void; addElements(...args: ChartComponentLike[]): void; addPlugins(...args: ChartComponentLike[]): void; addScales(...args: ChartComponentLike[]): void; getController(id: string): DatasetController | undefined; getElement(id: string): Element | undefined; getPlugin(id: string): Plugin | undefined; getScale(id: string): Scale | undefined; } export declare const registry: Registry; export interface Tick { value: number; label?: string | string[]; major?: boolean; } export interface CoreScaleOptions { /** * Controls the axis global visibility (visible when true, hidden when false). When display: 'auto', the axis is visible only if at least one associated dataset is visible. * @default true */ display: boolean | 'auto'; /** * Align pixel values to device pixels */ alignToPixels: boolean; /** * Background color of the scale area. */ backgroundColor: Color; /** * Reverse the scale. * @default false */ reverse: boolean; /** * Clip the dataset drawing against the size of the scale instead of chart area. * @default true */ clip: boolean; /** * The weight used to sort the axis. Higher weights are further away from the chart area. * @default true */ weight: number; /** * User defined minimum value for the scale, overrides minimum value from data. */ min: unknown; /** * User defined maximum value for the scale, overrides maximum value from data. */ max: unknown; /** * Adjustment used when calculating the maximum data value. */ suggestedMin: unknown; /** * Adjustment used when calculating the minimum data value. */ suggestedMax: unknown; /** * Callback called before the update process starts. */ beforeUpdate(axis: Scale): void; /** * Callback that runs before dimensions are set. */ beforeSetDimensions(axis: Scale): void; /** * Callback that runs after dimensions are set. */ afterSetDimensions(axis: Scale): void; /** * Callback that runs before data limits are determined. */ beforeDataLimits(axis: Scale): void; /** * Callback that runs after data limits are determined. */ afterDataLimits(axis: Scale): void; /** * Callback that runs before ticks are created. */ beforeBuildTicks(axis: Scale): void; /** * Callback that runs after ticks are created. Useful for filtering ticks. */ afterBuildTicks(axis: Scale): void; /** * Callback that runs before ticks are converted into strings. */ beforeTickToLabelConversion(axis: Scale): void; /** * Callback that runs after ticks are converted into strings. */ afterTickToLabelConversion(axis: Scale): void; /** * Callback that runs before tick rotation is determined. */ beforeCalculateLabelRotation(axis: Scale): void; /** * Callback that runs after tick rotation is determined. */ afterCalculateLabelRotation(axis: Scale): void; /** * Callback that runs before the scale fits to the canvas. */ beforeFit(axis: Scale): void; /** * Callback that runs after the scale fits to the canvas. */ afterFit(axis: Scale): void; /** * Callback that runs at the end of the update process. */ afterUpdate(axis: Scale): void; } export interface Scale<O extends CoreScaleOptions = CoreScaleOptions> extends Element<unknown, O>, LayoutItem { readonly id: string; readonly type: string; readonly ctx: CanvasRenderingContext2D; readonly chart: Chart; maxWidth: number; maxHeight: number; paddingTop: number; paddingBottom: number; paddingLeft: number; paddingRight: number; axis: string; labelRotation: number; min: number; max: number; ticks: Tick[]; getMatchingVisibleMetas(type?: string): ChartMeta[]; drawTitle(chartArea: ChartArea): void; drawLabels(chartArea: ChartArea): void; drawGrid(chartArea: ChartArea): void; /** * @param {number} pixel * @return {number} */ getDecimalForPixel(pixel: number): number; /** * Utility for getting the pixel location of a percentage of scale * The coordinate (0, 0) is at the upper-left corner of the canvas * @param {number} decimal * @return {number} */ getPixelForDecimal(decimal: number): number; /** * Returns the location of the tick at the given index * The coordinate (0, 0) is at the upper-left corner of the canvas * @param {number} index * @return {number} */ getPixelForTick(index: number): number; /** * Used to get the label to display in the tooltip for the given value * @param {*} value * @return {string} */ getLabelForValue(value: number): string; /** * Returns the grid line width at given value */ getLineWidthForValue(value: number): number; /** * Returns the location of the given data point. Value can either be an index or a numerical value * The coordinate (0, 0) is at the upper-left corner of the canvas * @param {*} value * @param {number} [index] * @return {number} */ getPixelForValue(value: number, index?: number): number; /** * Used to get the data value from a given pixel. This is the inverse of getPixelForValue * The coordinate (0, 0) is at the upper-left corner of the canvas * @param {number} pixel * @return {*} */ getValueForPixel(pixel: number): number | undefined; getBaseValue(): number; /** * Returns the pixel for the minimum chart value * The coordinate (0, 0) is at the upper-left corner of the canvas * @return {number} */ getBasePixel(): number; init(options: O): void; parse(raw: unknown, index?: number): unknown; getUserBounds(): { min: number; max: number; minDefined: boolean; maxDefined: boolean }; getMinMax(canStack: boolean): { min: number; max: number }; getTicks(): Tick[]; getLabels(): string[]; getLabelItems(chartArea?: ChartArea): LabelItem[]; beforeUpdate(): void; configure(): void; afterUpdate(): void; beforeSetDimensions(): void; setDimensions(): void; afterSetDimensions(): void; beforeDataLimits(): void; determineDataLimits(): void; afterDataLimits(): void; beforeBuildTicks(): void; buildTicks(): Tick[]; afterBuildTicks(): void; beforeTickToLabelConversion(): void; generateTickLabels(ticks: Tick[]): void; afterTickToLabelConversion(): void; beforeCalculateLabelRotation(): void; calculateLabelRotation(): void; afterCalculateLabelRotation(): void; beforeFit(): void; fit(): void; afterFit(): void; isFullSize(): boolean; } export declare class Scale { constructor(cfg: {id: string, type: string, ctx: CanvasRenderingContext2D, chart: Chart}); } export interface ScriptableScaleContext { chart: Chart; scale: Scale; index: number; tick: Tick; } export interface ScriptableScalePointLabelContext { chart: Chart; scale: Scale; index: number; label: string; type: string; } export interface RenderTextOpts { /** * The fill color of the text. If unset, the