Vue Chrts - Charts for Vue 3
Vue Chrts (version 2.1.4, with recent releases indicating active maintenance) is a Vue 3 charting library designed to create beautiful, responsive data visualizations with minimal setup. Inspired by Tremor and built on top of Unovis, it offers a variety of chart types including Line, Bar, Area, Stacked Area, and Donut charts. Key differentiators include its intuitive API, full customizability, responsive design, and robust TypeScript support, making it well-suited for modern Vue.js and Nuxt 3 applications where aesthetic appeal and ease of integration are priorities. It handles the complexities of charting, allowing developers to focus on data presentation.
Common errors
-
[Vue warn]: Failed to resolve component: LineChart
cause The chart component (e.g., `LineChart`) was not correctly imported or registered in the Vue component where it's being used.fixEnsure you have `import { LineChart } from 'vue-chrts';` at the top of your `<script setup>` block or explicitly registered it if using the Options API. -
TypeError: Cannot read properties of undefined (reading 'month') in xFormatter
cause The `xFormatter` function is trying to access a property (`month`) from an element in the `data` array that either doesn't exist or is `undefined` due to an incorrect data structure or index.fixVerify that your `data` array objects contain the `month` property (or whatever property `xFormatter` expects) and that the `xFormatter` logic correctly handles the data structure. -
Error: Cannot find module 'vue-chrts' or 'nuxt-charts'
cause This error typically occurs if the package was installed incorrectly, the wrong package name (`vue-chrts` vs. `nuxt-charts`) was used for the project type, or the import path is incorrect.fixCheck your `package.json` for `vue-chrts` (for Vue projects) or `nuxt-charts` (for Nuxt projects). Ensure correct installation (`npm install vue-chrts` or `npm install nuxt-charts`) and that the import statement matches the installed package. For Nuxt, ensure the module is added to `nuxt.config.ts`.
Warnings
- breaking Vue Chrts requires Vue 3.5.13 or higher as a peer dependency. Using older Vue 3 versions might lead to unexpected behavior or compilation errors due to API differences.
- gotcha The package name for standard Vue.js projects is `vue-chrts`, while for Nuxt applications, it's `nuxt-charts`. Using the wrong package name for your project type will result in installation or import errors. This is part of a monorepo strategy.
- gotcha Ensure that the `data` prop's array structure and the `categories` prop's keys precisely match the chart's expectations. Mismatched property names or incorrect data shapes can lead to charts failing to render or displaying empty. For example, `xFormatter` relies on the `data` array's structure.
- gotcha Mutating props directly in a child component, such as the `data` or `categories` props, is an anti-pattern in Vue and will trigger warnings. This breaks the one-way data flow.
Install
-
npm install vue-chrts -
yarn add vue-chrts -
pnpm add vue-chrts
Imports
- LineChart
const LineChart = require('vue-chrts');import { LineChart } from 'vue-chrts'; - BarChart
import BarChart from 'vue-chrts';
import { BarChart } from 'vue-chrts'; - DonutChart
import { DonutChart } from 'vue-chrts';
Quickstart
<script setup>
import { LineChart } from 'vue-chrts';
const data = [
{ month: 'Jan', sales: 100, profit: 50 },
{ month: 'Feb', sales: 120, profit: 55 },
{ month: 'Mar', sales: 180, profit: 80 },
{ month: 'Apr', sales: 110, profit: 40 },
{ month: 'May', sales: 90, profit: 30 },
];
const categories = {
sales: {
name: 'Sales',
color: '#3b82f6'
},
profit: {
name: 'Profit',
color: '#10b981'
}
};
const xFormatter = (i) => data[i].month;
</script>
<template>
<LineChart
:data="data"
:categories="categories"
:height="300"
:xFormatter="xFormatter"
xLabel="Month"
yLabel="Amount"
/>
</template>