CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-vue-chartjs

Vue.js wrapper for Chart.js that provides reactive chart components with full Chart.js v4 support.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

Vue-ChartJS

Vue-ChartJS is a wrapper for Chart.js in Vue that enables developers to create reusable, reactive chart components. It provides pre-built chart components for all Chart.js chart types with full Vue reactivity integration, supporting both Vue 2.7+ and Vue 3 with Chart.js v4.

Package Information

  • Package Name: vue-chartjs
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install vue-chartjs chart.js

Core Imports

import { Chart, Bar, Line, Pie, Doughnut, PolarArea, Radar, Bubble, Scatter } from "vue-chartjs";
import { getDatasetAtEvent, getElementAtEvent, getElementsAtEvent } from "vue-chartjs";
import { createTypedChart } from "vue-chartjs";
import type { ChartProps, ChartComponentRef } from "vue-chartjs";

For CommonJS:

const { Chart, Bar, Line, Pie, Doughnut, PolarArea, Radar, Bubble, Scatter } = require("vue-chartjs");
const { getDatasetAtEvent, getElementAtEvent, getElementsAtEvent } = require("vue-chartjs");

Basic Usage

<template>
  <Bar 
    :data="data" 
    :options="options"
    aria-label="Sales data chart showing quarterly performance"
    aria-describedby="chart-description"
  />
  <div id="chart-description" class="sr-only">
    Bar chart displaying sales data across three quarters: January ($40k), February ($20k), and March ($12k).
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import {
  Chart as ChartJS,
  Title,
  Tooltip,
  Legend,
  BarElement,
  CategoryScale,
  LinearScale
} from 'chart.js';
import { Bar } from 'vue-chartjs';

ChartJS.register(CategoryScale, LinearScale, BarElement, Title, Tooltip, Legend);

const data = ref({
  labels: ['January', 'February', 'March'],
  datasets: [{
    label: 'Sales',
    data: [40, 20, 12],
    backgroundColor: '#f87979'
  }]
});

const options = ref({
  responsive: true,
  maintainAspectRatio: false
});
</script>

Architecture

Vue-ChartJS is built around several key components:

  • Generic Chart Component: Base Chart component accepting any Chart.js chart type via type prop
  • Typed Chart Components: Pre-configured components (Bar, Line, Pie, etc.) with automatic Chart.js registration
  • Reactivity System: Full Vue reactivity integration with intelligent update mechanisms
  • Event System: Chart.js event handling utilities for user interactions
  • Type Safety: Complete TypeScript support with generic type preservation
  • Vue Compatibility: Support for both Vue 2.7+ and Vue 3 through compatibility layers

Capabilities

Chart Components

Provides both generic and typed chart components with full Chart.js integration and Vue reactivity.

// Generic chart component
const Chart: ChartComponent;

// Typed chart components  
const Bar: TypedChartComponent<'bar'>;
const Line: TypedChartComponent<'line'>;
const Pie: TypedChartComponent<'pie'>;
const Doughnut: TypedChartComponent<'doughnut'>;
const PolarArea: TypedChartComponent<'polarArea'>;
const Radar: TypedChartComponent<'radar'>;
const Bubble: TypedChartComponent<'bubble'>;
const Scatter: TypedChartComponent<'scatter'>;

interface ChartProps<
  TType extends ChartType = ChartType,
  TData = DefaultDataPoint<TType>,
  TLabel = unknown
> {
  type: TType; // Required for Chart component only
  data: ChartData<TType, TData, TLabel>; // Required
  options?: ChartOptions<TType>; // Optional, defaults to {}
  plugins?: Plugin<TType>[]; // Optional, defaults to []
  datasetIdKey?: string; // Optional, defaults to 'label'
  updateMode?: UpdateMode; // Optional
  destroyDelay?: number; // Optional, defaults to 0 - delay before destroying chart instance
  ariaLabel?: string; // Optional - ARIA label for accessibility
  ariaDescribedby?: string; // Optional - ARIA described-by for accessibility
}

interface ChartComponentRef<
  TType extends ChartType = ChartType,
  TData = DefaultDataPoint<TType>,
  TLabel = unknown
> {
  chart: ChartJS<TType, TData, TLabel> | null;
}

type ChartComponent = DefineComponent<ChartProps>;

type TypedChartComponent<
  TType extends ChartType,
  TData = DefaultDataPoint<TType>,
  TLabel = unknown
> = DefineComponent<Omit<ChartProps<TType, TData, TLabel>, 'type'>>;

Chart Factory

Factory function for creating custom typed chart components with automatic Chart.js registration.

/**
 * Creates a typed chart component with automatic Chart.js registration
 * @param type - Chart.js chart type
 * @param registerables - Chart.js components to register
 * @returns TypedChartComponent for the specified chart type
 */
function createTypedChart<
  TType extends ChartType = ChartType,
  TData = DefaultDataPoint<TType>,
  TLabel = unknown
>(
  type: TType,
  registerables: ChartComponentLike
): TypedChartComponent<TType, TData, TLabel>;

Accessibility Features

Chart components support accessibility through ARIA attributes and chart lifecycle management.

interface AccessibilityProps {
  /** ARIA label for the chart canvas element */
  ariaLabel?: string;
  /** ARIA described-by reference for detailed chart description */
  ariaDescribedby?: string;
  /** Delay in milliseconds before destroying chart instance (prevents memory leaks) */
  destroyDelay?: number; // defaults to 0
}

Event Handling Utilities

Utilities for extracting chart elements and datasets from user interaction events.

/**
 * Get dataset from mouse click event
 * @param chart - Chart.js instance
 * @param event - Mouse click event
 * @returns Dataset elements at event location
 */
function getDatasetAtEvent(chart: Chart, event: MouseEvent): InteractionItem[];

/**
 * Get single dataset element from mouse click event
 * @param chart - Chart.js instance  
 * @param event - Mouse click event
 * @returns Single element nearest to event location
 */
function getElementAtEvent(chart: Chart, event: MouseEvent): InteractionItem[];

/**
 * Get all dataset elements from mouse click event
 * @param chart - Chart.js instance
 * @param event - Mouse click event  
 * @returns All elements at the same index as event location
 */
function getElementsAtEvent(chart: Chart, event: MouseEvent): InteractionItem[];

Event Utilities

Types

// Extended data point interface for complex data structures (used in typed chart components like Bar)
interface ExtendedDataPoint {
  [key: string]: string | number | null | ExtendedDataPoint;
}

// Re-exported Chart.js types
import type {
  Chart,
  ChartType,
  ChartData,
  ChartDataset,
  ChartOptions,
  DefaultDataPoint,
  Plugin,
  UpdateMode,
  InteractionItem,
  ChartComponentLike
} from 'chart.js';
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/vue-chartjs@5.3.x
Publish Source
CLI
Badge
tessl/npm-vue-chartjs badge