vue-apexcharts Documentation

repository·master·Indexed 23 days ago

https://github.com/apexcharts/vue-apexcharts

A Vue.js wrapper for ApexCharts.js (version 1.7.0) that enables the creation of interactive, responsive visualizations using component props and methods. It provides the <apexchart> component and an optional Vue plugin to access the core ApexCharts library via the $apexcharts property.

Tokens
2.2K
Snippets
7
Records
10
Agent score
30%

What's inside vue-apexcharts

  1. How to update charts reactively

    master

    The chart automatically re-renders when the series or options props change.

    Important: When updating nested properties within the options object, you must replace the entire options object (or the outermost property being changed) to ensure Vue's reactivity system detects the change. Do not attempt to mutate a nested property directly.

    // ✅ Do this: Replace the whole object to trigger Vue watch
    this.chartOptions = {
      ...this.chartOptions,
      xaxis: {
        labels: {
           style: {
             colors: ['red']
           }
        }
      }
    }
    
    // ❌ Not this: Direct mutation of nested property will not trigger update
    this.chartOptions.xaxis = {
        labels: {
          style: {
            colors: ['red']
          }
        }
    }
  2. Register vue-apexcharts in your Vue application

    master

    To use the component, import VueApexCharts and register it globally using Vue.use() or by registering it as a component.

    import VueApexCharts from 'vue-apexcharts'
    Vue.use(VueApexCharts)
    
    Vue.component('apexchart', VueApexCharts)
  3. Install the vue-apexcharts plugin

    master
    You can install vue-apexcharts as a Vue plugin. This makes the ApexCharts library available globally on the Vue constructor and adds a $apexcharts property to the Vue.prototype, allowing you to access the core library from any component instance.
  4. Register the ApexCharts component locally

    master
    Instead of installing the plugin globally, you can import the ApexChartsComponent and register it locally within a specific Vue component. This is useful for tree-shaking or when you only need charts in specific parts of your application.
  5. Use the apexchart component

    master

    Create a chart by using the <apexchart> component. You must provide type, series, and options props. You can also specify width and height.

    <template>
       <div>
         <apexchart width="500" type="bar" :options="chartOptions" :series="series"></apexchart>
       </div>
    </template>
    
    <script>
    export default {
        data: function() {
          return {
            chartOptions: {
              chart: {
                id: 'vuechart-example'
              },
              xaxis: {
                categories: [1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998]
              }
            },
            series: [{
              name: 'series-1',
              data: [30, 40, 35, 50, 49, 60, 70, 91]
            }]
          }
        },
    };
    </script>
  6. Call ApexCharts methods via global $apexcharts variable

    master

    If you need to call ApexCharts methods from outside the component context (without using a ref), you can use the global this.$apexcharts.exec() method. You must target the specific chart instance using its chart.id.

    // Syntax: this.$apexcharts.exec(chartId, methodName, params)
    this.$apexcharts.exec('vuechart-example', 'updateSeries', [{
      data: [40, 55, 65, 11, 23, 44, 54, 33]
    }])
  7. Reference: apexchart Props

    master

    The following props are available on the <apexchart> component:

    | Prop        | Type           | Description  |
    | ------------- |-------------| -----|
    | **series***| Array | The series is an array which accepts an object in the following format. To know more about the format of dataSeries, checkout [Series](https://apexcharts.com/docs/series/) docs on the website. |
    | **type*** | String  | `line`, `area`, `bar`, `pie`, `donut`, `scatter`, `bubble`, `heatmap`, `radialBar`, `candlestick`  |
    | **width** | Number/String  | Possible values for width can be `100%` or `400px` or `400` |
    | **height** | Number/String | Possible values for height can be `100%` or `300px` or `300` |
    | **options** | Object | The configuration object, see options on [API (Reference)](https://apexcharts.com/docs/options/chart/type/) |
  8. Reference: apexchart Methods

    master

    While props handle automatic updates, you can use these methods to forcefully update the chart or perform specific actions. To call these, use a ref on the component.

    | Method        | Description    |
    | ------------- |-------------| <a href="https://apexcharts.com/docs/methods/#updateSeries">updateSeries</a> | Allows you to update the series array overriding the existing one |
    | <a href="https://apexcharts.com/docs/methods/#updateOptions">updateOptions</a> | Allows you to update the configuration object |
    | <a href="https://apexcharts.com/docs/methods/#toggleSeries">toggleSeries</a> | Allows you to toggle the visibility of series programatically. Useful when you have custom legend. |
    | <a href="https://apexcharts.com/docs/methods/#appendData">appendData</a> | Allows you to append new data to the series array. |
    | <a href="https://apexcharts.com/docs/methods/#addtext">addText</a> | The addText() method can be used to draw text after chart is rendered. |
    | <a href="https://apexcharts.com/docs/methods/#addxaxisannotation">addXaxisAnnotation</a> | Draw x-axis annotations after chart is rendered. |
    | <a href="https://apexcharts.com/docs/methods/#addyaxisannotation">addYaxisAnnotation</a> | Draw y-axis annotations after chart is rendered. |
    | <a href="https://apexcharts.com/docs/methods/#addpointannotation">addPointAnnotation</a> | Draw point (xy) annotations after chart is rendered. |