Search code examples
vue.jsapexcharts

How do I add dynamic data to apexcharts using vue.js?


I am trying to use dynamic data in my donut apexchart, but I don't know the correct way to do it.

I tried setting "test: 5" under data and then change the static number in series: [this.test, 4, 1]. Did not output anything, I am new to programming.

<script>
import VueApexCharts from 'vue-apexcharts'
export default {
name: 'Dashboard',
components: {
apexcharts: VueApexCharts
},
data () {
return {
  test: 6,
  series: [this.test, 5, 4],
  chartOptions: {
    plotOptions: {
      pie: {
        donut: {
          labels: {
            show: true,
            name: {
              fontSize: '24px'
            },
            value: {
              fontSize: '34px',
              color: '#fff'
            }
          }
        }
      }
    },
    tooltip: {
      enabled: false
    },
    legend: {
      show: false
    },
    fill: {
      colors: ['#4b367c', '#2aa5ed', '#db2828']
    },
    dataLabels: {
      enabled: false
    },
    labels: ['Utkast', 'Öppen', 'Förfallen'],
    colors: ['#4b367c', '#2aa5ed', '#db2828']
  }
}
},
mounted () {
this.$store.commit('setPageTitle', { title: 'Översikt' })
}
}
</script>

My goal is to reach in to firebase and use the data from there, but to start with I just wanted to test out how to add any data and failed here already.


Solution

  • You should not use one variable in another directly in data. series: [this.test, 5, 4], This will return null in this.test. You can set series empty intially and set values later. series:[] And then populate series in some function. Or else use computed() as follows:

    computed: {
        series() {
            return this.test;
        }
    }