Skip to content
Advertisement

Vue Chart.js – Chart is not updating when data is changing

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

JavaScript

LineChart.js

JavaScript

Advertisement

Answer

Use a computed property for the chart data. And instead of calling this.renderChart on watch wrap it in a method and reuse that method on mounted and in watch.

JavaScript
JavaScript

You could also make the options a computed property, and if option not going to change much you can setup default props. https://v2.vuejs.org/v2/guide/components.html#Prop-Validation

Here is a working codepen https://codepen.io/azs06/pen/KmqyaN?editors=1010

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement