Nuxt Charts
Nuxt Charts is a Nuxt 3 module (current stable version 2.1.4) that provides an effortless way to integrate beautiful and responsive charting components into Nuxt applications. It acts as a wrapper around the `vue-chrts` library, which is inspired by Tremor and built on top of Unovis, offering a variety of chart types including Line, Bar, Area, Stacked Area, and Donut charts. The module leverages Nuxt's auto-import feature for its components and types, simplifying the development experience. It is designed with Vue 3 and TypeScript, focusing on customizability and an intuitive API. While `nuxt-charts` doesn't have a fixed public release cadence, it typically aligns with the Nuxt ecosystem's rapid development. Its key differentiator lies in providing a 'Nuxt-native' charting solution, abstracting away direct component imports from `vue-chrts` and integrating seamlessly with the Nuxt development workflow, which differentiates it from general Vue charting libraries like `vue-chartjs` or `vue-chartkick`.
Common errors
-
ReferenceError: LineChart is not defined
cause The `nuxt-charts` module was not correctly added to the `modules` array in `nuxt.config.ts`.fixEnsure `modules: ['nuxt-charts']` is present in your `nuxt.config.ts` file. -
Error: Cannot find module 'vue-chrts' or its corresponding type declarations.
cause The underlying `vue-chrts` package, which `nuxt-charts` depends on, was not installed.fixInstall `vue-chrts` manually: `npm install vue-chrts` or `pnpm add vue-chrts` or `yarn add vue-chrts`. -
Error: 'to-px' error in console or during build.
cause This issue typically arises when using pnpm due to its stricter dependency hoisting, preventing `nuxt-charts` from accessing its internal `@unovis/ts` dependency correctly.fixAdd `shamefully-hoist=true` to your `.npmrc` file (create if it doesn't exist) or explicitly install `@unovis/ts` as a direct dependency: `pnpm add @unovis/ts`.
Warnings
- gotcha When using pnpm as a package manager, you might encounter a 'to-px' error due to how pnpm hoists dependencies.
- gotcha Although `nuxt-charts` auto-imports its components, some documentation examples or older patterns might show explicit imports from `vue-chrts` (e.g., `import { LineChart } from 'vue-chrts';`). This can lead to confusion and unnecessary imports.
- gotcha Nuxt 4 introduces shallow reactivity by default for `useAsyncData` and `useFetch`. If chart data is fetched using these composables without `.value` or deep reactivity enabled, chart updates might not be fully reactive.
Install
-
npm install nuxt-charts -
yarn add nuxt-charts -
pnpm add nuxt-charts
Imports
- defineNuxtConfig
const defineNuxtConfig = require('nuxt/config')import { defineNuxtConfig } from 'nuxt/config' - LineChart
import { LineChart } from 'vue-chrts'; // If using the Nuxt module, components are auto-imported<LineChart ... />
- LegendPosition
import { LegendPosition } from 'nuxt-charts'import type { LegendPosition } from '#nuxt-charts/types'
Quickstart
// nuxt.config.ts
export default defineNuxtConfig({
modules: [
'nuxt-charts'
]
});
// app.vue (or any Nuxt page/component)
<template>
<div class="chart-wrapper">
<LineChart
:data="data"
:categories="categories"
:height="300"
:xFormatter="xFormatter"
xLabel="Month"
yLabel="Amount"
/>
<BarChart
:data="barData"
:categories="barCategories"
:height="250"
xLabel="Product"
yLabel="Revenue"
/>
</div>
</template>
<script setup lang="ts">
// Chart components like LineChart and BarChart are auto-imported by the Nuxt module
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: number) => data[i].month;
const barData = [
{ product: 'A', revenue: 200 },
{ product: 'B', revenue: 300 },
{ product: 'C', revenue: 150 }
];
const barCategories = {
revenue: {
name: 'Total Revenue',
color: '#fbbf24'
}
};
</script>
<style scoped>
.chart-wrapper {
max-width: 800px;
margin: 2rem auto;
padding: 1rem;
border: 1px solid #eee;
border-radius: 8px;
display: flex;
flex-direction: column;
gap: 2rem;
}
</style>