I'm using Vue.js and Chart.js to draw some charts.
Each time I call the function generateChart()
, the chart is not updated automatically. When I check the data in Vue Devtools, they are correct but the chart does not reflect the data. However, the chart does update when I resize the window.
- What is wrong with what I'm doing?
- How do I update the chart each time I call
generateChart()
?
I feel this is going to be something related with object
and array
change detection caveats, but I'm not sure what to do.
https://codepen.io/anon/pen/bWRVKB?editors=1010
<template>
<el-dialog title="Chart" v-model="showGeneratedChart">
<line-chart :chartData="dataChart"></line-chart>
</el-dialog>
</template>
<script>
export default {
data() {
const self = this;
return {
dataChart: {
labels: [],
datasets: [
{
label: "label",
backgroundColor: "#FC2525",
data: [0, 1, 2, 3, 4],
},
],
},
};
},
methods: {
generateChart() {
this.dataChart["labels"] = [];
this.dataChart["datasets"] = [];
// ... compute datasets and formattedLabels
this.dataChart["labels"] = formattedLabels;
this.dataChart["datasets"] = datasets;
},
},
};
</script>
LineChart.js
import { Line, mixins } from 'vue-chartjs'
export default Line.extend({
mixins: [mixins.reactiveProp],
props: ["options"],
mounted () {
this.renderChart(this.chartData, this.options)
}
})