Vue 3 ApexCharts Component
vue3-apexcharts is the official Vue 3 wrapper component for ApexCharts.js, a modern open-source charting library used for building interactive data visualizations. Currently at version 1.11.1, it provides seamless integration of ApexCharts within Vue 3 applications, offering features like reactive chart updates where changes to data or options automatically re-render the chart. The library supports tree-shaking, allowing developers to import only necessary chart types to optimize bundle size, and includes robust Server-Side Rendering (SSR) capabilities with dedicated `<apexchart-server>` and `<apexchart-hydrate>` components. It also offers full TypeScript support, enhancing developer experience with type safety. This package is specifically designed for Vue 3; users requiring Vue 2 compatibility should instead use the `vue-apexcharts` package. The release cadence typically follows updates to the core ApexCharts.js library, ensuring compatibility and access to new features. Its key differentiator is being the official, fully-featured, and actively maintained Vue 3 component for ApexCharts.js.
Common errors
-
Failed to resolve component: apexchart
cause The `<apexchart>` component was not properly registered either globally or locally.fixIf using globally, ensure `import VueApexCharts from "vue3-apexcharts"; createApp(App).use(VueApexCharts).mount('#app');` is called. If locally, add `components: { apexchart: VueApexCharts }` to your component's definition. -
Cannot read properties of undefined (reading 'render')
cause The underlying `apexcharts` core library is not installed or not accessible to `vue3-apexcharts`.fixInstall `apexcharts` as a peer dependency: `npm install apexcharts` or `yarn add apexcharts`. -
Property '$apexcharts' does not exist on type 'ComponentPublicInstance<...>' (TypeScript error)
cause The TypeScript type declaration for the global `$apexcharts` property is missing from your project.fixAdd the provided `declare module "@vue/runtime-core" {...}` block to your TypeScript environment (e.g., in `shims-vue.d.ts` or `main.ts`) after importing `ApexCharts`.
Warnings
- breaking vue3-apexcharts is exclusively for Vue 3.x applications. Attempting to use it in a Vue 2.x project will result in compatibility errors and runtime failures.
- gotcha The `apexcharts` core library is a peer dependency and must be installed separately. The `vue3-apexcharts` component will not render or function correctly if `apexcharts` is missing.
- gotcha For global component registration, `app.use(VueApexCharts)` must be called *before* mounting the Vue application. If you register locally, ensure `apexchart: VueApexCharts` is correctly declared in your component's `components` option.
- gotcha When using TypeScript and attempting to access `$apexcharts` on the Vue instance (e.g., `this.$apexcharts`), you need to augment the `ComponentCustomProperties` interface to provide type declarations, otherwise TypeScript will report an error.
Install
-
npm install vue3-apexcharts -
yarn add vue3-apexcharts -
pnpm add vue3-apexcharts
Imports
- VueApexCharts
import { VueApexCharts } from 'vue3-apexcharts';import VueApexCharts from "vue3-apexcharts"; const app = createApp(App); app.use(VueApexCharts);
- apexchart (component)
const apexchart = require('vue3-apexcharts');import VueApexCharts from "vue3-apexcharts"; export default { components: { apexchart: VueApexCharts }, }; - $apexcharts (global property)
app.config.globalProperties.$apexcharts = ApexCharts; // (without TS declaration)
import ApexCharts from "apexcharts"; app.config.globalProperties.$apexcharts = ApexCharts; declare module "@vue/runtime-core" { interface ComponentCustomProperties { $apexcharts: typeof ApexCharts; } }
Quickstart
<template>
<div class="app">
<apexchart
width="550"
type="bar"
:options="chartOptions"
:series="series"
></apexchart>
<div>
<button @click="updateChart">Update Chart Data</button>
</div>
</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
import VueApexCharts from "vue3-apexcharts";
import ApexCharts from "apexcharts"; // For global property typing
// Add this when into a TypeScript codebase to type $apexcharts
declare module "@vue/runtime-core" {
interface ComponentCustomProperties {
$apexcharts: typeof ApexCharts;
}
}
export default defineComponent({
components: {
apexchart: VueApexCharts, // Local registration, or use app.use(VueApexCharts) globally in main.ts
},
data() {
return {
chartOptions: {
chart: {
id: "vuechart-example",
toolbar: {
show: false
}
},
xaxis: {
categories: [1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998]
},
title: {
text: 'Yearly Data Series',
align: 'left'
}
},
series: [
{
name: "Series A",
data: [30, 40, 35, 50, 49, 60, 70, 91]
}
]
};
},
methods: {
updateChart() {
// Simulate data update by generating new random data
const newSeriesData = this.series[0].data.map(() => Math.floor(Math.random() * (100 - 20 + 1)) + 20);
this.series = [{ name: "Series A", data: newSeriesData }];
}
}
});
</script>
<style>
.app {
font-family: Avenir, Helvetica, Arial, sans-serif;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
button {
padding: 10px 20px;
margin-top: 20px;
cursor: pointer;
background-color: #42b983;
color: white;
border: none;
border-radius: 5px;
}
</style>