I'm using vue.js and vue-chartjs for the first time. In the simplified code example I want to add a label to the chart by pressing the add Label button, but the chart is not updating. My code is based on this tutorial.

I tried calling the render method from the child component, but that does not work.

Edit Vue Chart.js add label

I expected that when I click the add Label button that the label 13 will be added to the chart.

1 Answers

User 28 On Best Solutions

You have a few mistakes in your code. First if you want to call a method from child component you have to use special attribute ref. Please see more about ref.

So in your code it should be:

  <chart ref='chart' :chart-data="datacollection"></chart>

  this.$refs.chart.render() // Not Chart.render()

Second you have a typo in your Chart.js:

this.chartData // Not this.chartdata

So this means yours render method in mounted is not working and unnecessary. Your chart is already drawn by reactiveProp after you set chartData prop.

But unfortunately this:


still not work. If you looking at the source code here you will see the library use watcher on chartData only. It means if you change entire of chartData this will work fine (as it works at first drawn). Please see more about watcher.

So if you want to rely on reactiveProp then when you want update labels you should set:

this.datacollection = {
  labels: this.datacollection.labels.concat(13) // avoid to mutate data

Edit Vue Chart.js add label