Vue Chart.js 4 Wrapper

4.0.1 · active · verified Sun Apr 19

Vue-Chart-3 is a modern, TypeScript-first wrapper for Chart.js 4, specifically designed for use with Vue 3's Composition API. It provides a suite of reactive Vue components that streamline the integration of various Chart.js chart types, such as Bar, Line, and Doughnut, into Vue applications. Currently at version 4.0.1, the package is actively maintained with a focus on seamless compatibility with Chart.js 4, Vue 3.5+, and contemporary build tools like Vite.js and Nuxt 3. Its key differentiators include comprehensive TypeScript support, leveraging the Composition API for efficient reactivity, and ensuring robust functionality within the latest Vue and Chart.js ecosystems.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up and render a reactive Bar Chart using `vue-chart-3` and Chart.js in a Vue 3 Composition API component, including global Chart.js registration and dynamic data updates.

import { defineComponent, ref, computed } from 'vue';
import { Chart, registerables } from 'chart.js';
import { BarChart } from 'vue-chart-3';

// Globally register Chart.js components once
Chart.register(...registerables);

export default defineComponent({
  name: 'SimpleBarChart',
  components: {
    BarChart,
  },
  setup() {
    const dataValues = ref([65, 59, 80, 81, 56, 55, 40]);

    const chartData = computed(() => ({
      labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
      datasets: [
        {
          label: 'Data One',
          backgroundColor: '#42b983',
          data: dataValues.value,
        },
        {
          label: 'Data Two',
          backgroundColor: '#ff6384',
          data: [28, 48, 40, 19, 86, 27, 90],
        },
      ],
    }));

    const chartOptions = ref({
      responsive: true,
      maintainAspectRatio: false,
      plugins: {
        title: {
          display: true,
          text: 'Monthly Sales Data',
        },
      },
    });

    const updateData = () => {
      dataValues.value = dataValues.value.map(value => Math.floor(Math.random() * 100));
    };

    return {
      chartData,
      chartOptions,
      updateData,
    };
  },
  template: `
    <div :style="{ height: '400px', width: '600px' }">
      <BarChart :chartData="chartData" :options="chartOptions" />
      <button @click="updateData" style="margin-top: 20px; padding: 10px 20px; background-color: #42b983; color: white; border: none; border-radius: 5px; cursor: pointer;">Update Data</button>
    </div>
  `,
});

view raw JSON →