Utilities

Custom Chart Configuration

TypeScript types for custom Chart.js configurations.

Install external dependencies

npm i chart.js ng2-charts

Source Code

File: src/utils/types/custom-chart-configuration.ts

import {
  ChartConfiguration,
  ChartOptions,
  ChartType,
  DefaultDataPoint,
} from 'chart.js';

export type VerticalHoverLineOptions = {
  color?: string;
};

export type HoverSegmentOptions = {
  color?: string;
  indexAxis?: 'x' | 'y';
};

type PluginsOf<TType extends ChartType> =
  ChartOptions<TType> extends { plugins?: infer P } ? NonNullable<P> : never;

export type CustomPluginOptionsByType<TType extends ChartType> =
  PluginsOf<TType> & {
    verticalHoverLine?: VerticalHoverLineOptions;
    hoverSegment?: HoverSegmentOptions;
  };

export type CustomChartOptions<TType extends ChartType = ChartType> = Omit<
  ChartOptions<TType>,
  'plugins'
> & {
  plugins?: CustomPluginOptionsByType<TType>;
};

export type CustomChartConfiguration<
  TType extends ChartType = ChartType,
  TData = DefaultDataPoint<TType>,
  TLabel = unknown,
> = Omit<ChartConfiguration<TType, TData, TLabel>, 'options'> & {
  options?: CustomChartOptions<TType>;
};

Description

This module extends Chart.js type definitions to support custom plugins with proper TypeScript typing.

Type Definitions

VerticalHoverLineOptions: Configuration for the vertical hover line plugin

  • color: Optional color string for the hover line

HoverSegmentOptions: Configuration for the hover segment plugin

  • color: Optional color string for the segment highlight
  • indexAxis: Chart orientation ('x' for vertical, 'y' for horizontal)

CustomPluginOptionsByType: Extends Chart.js plugin options with custom plugin configurations

CustomChartOptions: Chart.js options type extended with custom plugin support

CustomChartConfiguration: Complete chart configuration type with custom plugin typing, maintaining full Chart.js type safety

These types ensure proper TypeScript intellisense and type checking when using custom Chart.js plugins.

Dependencies

  • chart.js: Chart.js type definitions

Found a bug? Let us know →

Angular Material Blocks Logo

Angular Material Dev UI (or Angular Material Blocks) is one place stop for developers to explore components and blocks for their Angular Material and Tailwind CSS based applications.

Find us on X (Twitter), LinkedIn, Instagram & Threads