[Vuejs]-Rendering vuechart inside v-for

0👍

Check Vue Chart Guide, the first parameter for this.renderChart() is one object instead one array.

For example which is copied from above guide:

{
  labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
  datasets: [
    {
      label: 'GitHub Commits',
      backgroundColor: '#f87979',
      data: [40, 20, 12, 39, 10, 40, 39, 80, 40, 20, 12, 11]
    }
  ]
}

Fix it then the charts should work fine.

Below is one simple demo:

Vue.config.productionTip = false
let RawLineChart = window.VueChartJs.Bar
Vue.component('line-chart', {
  extends: RawLineChart,
  props: ['data', 'options', 'name'],
  mounted () {
    this.renderChart({
      labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
      datasets: [
        {
          label: this.name,
          backgroundColor: '#f87979',
          data: this.data
        }
      ]
    }, this.options)
  }
})

new Vue({
  el: '#app',
  data() {
    return {
      results: [
        {name:'Chart A', trends: [40, 39, 20, 40, 39, 80, 40]},
        {name:'Chart B', trends: [140, 139, 110, 140, 139, 80, 140]},
        {name:'Chart C', trends: [240, 139, 210, 140, 239, 80, 240]}
      ]
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.min.js"></script>
<script src="https://unpkg.com/vue-chartjs/dist/vue-chartjs.min.js"></script>
<div id="app">
<table>
  <tr v-for="result in results">
  <td>{{result.name}}</td>
  <td>
    <line-chart :data="result.trends" :name="result.name" :options="{responsive: false, maintainAspectRatio: false}">
    </line-chart>
    </td>
    </tr>
</table>
</div>

Leave a comment