Home > Net >  Highcarts won't redraw after fetching data from axios request in VueJS
Highcarts won't redraw after fetching data from axios request in VueJS

Time:03-25

[enter image description here][1]> I am using Laravel Vue JS. I am able to get the data and assign it in my chartOptions.series. the main problem is that the chart won't redraw. I declared my highcharts globally and this is a child component that will be imported in my dashboard. Below is my code.

<template>
    <div>
        <highcharts :options="chartOptions"></highcharts>
    </div>
</template>

<script>

    export default {
        data(){
            return{
                data:{
                    males: '',
                    females: '',
                },
                chartOptions: {
                    title:{
                        text: 'Applicants Account by Gender'
                    },
                    chart:{
                        type: 'column'
                    },
                    yAxis: {
                        title: {
                            text: 'Number of Applicant Accounts'
                        },
                        tickInterval: 2,
                        crosshair: true,
                    },
                    plotOptions: {
                        series: {
                            dataLabels: {
                                enabled: true,
                                inside:true,
                            }
                        }
                    },
                    series: [{
                        name: 'Male',
                        data: [],
                    },
                    {
                        name: 'Female',
                        data: [],
                        color: 'pink',
                    }]
                }
            }
        },
        methods:{
            async loadApplicantsPerGender(){
                await axios.get("/api/dashboard/applicantsPerGender").then(res =>{
                    this.data.males = res.data.males;
                    this.data.females = res.data.females;
                })
            },
        },
        created(){
            this.loadApplicantsPerGender();
            setTimeout(function(){ 
                this.chartOptions.series[0].data = this.data.males;
                this.chartOptions.series[1].data = this.data.females;
            }.bind(this), 3000);
        }
    }
</script>

The output is just an empty chart, like this. [1]: https://i.stack.imgur.com/Vgxma.png

I already tried adding the :updatedArgs in the highchart tag but still it won't work, tried using watch() to update the series data.

CodePudding user response:

Did you tried the high chart redraw API

https://api.highcharts.com/class-reference/Highcharts.Chart#redraw

CodePudding user response:

You need to adapt your data to the format required by Highcharts. In your case it will be:

this.chartOptions.series[0].data = [this.data.males];
this.chartOptions.series[1].data = [this.data.females];

Live demo: https://codesandbox.io/s/highcharts-vue-demo-fork-chudkw?file=/src/components/Chart.vue

API Reference: https://api.highcharts.com/highcharts/series.column.data

  • Related