question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rerendering doughtnut chart not working

See original GitHub issue

Expected Behavior

When datasets[0].data is changed, the doughnut should rerender.

Actual Behavior

It isn’t rerendering the doughnut. If you put your mouse over, you can see that data is changed, but still there is no visual change.

Component:

import { Doughnut, mixins } from 'vue-chartjs'

export default Doughnut.extend({
    mixins: [mixins.reactiveProp],
    props: ["chartData", "options"],
    mounted () {
        this.renderChart(this.chartData, this.options)
    },
    methods: {},
    events: {},
})

Then I’m just changing the data: this.chart.datasets[0] = newData

Environment

  • OS: Ubuntu
  • NPM Version: 4.1.2

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
aperturelesscommented, Feb 11, 2017

@bertobc

Oh I guess I know whats wrong with your code. However I can only speculate.

What is this.chart.datasets[0] in your case? The chartjs instance should be a private var and only accessible over this._chart

However, it seems that you’re changing the chart dataset in the chartjs instance object. Thats wrong, and does not make sense, since chartjs does not have an automatic detection for data changes.

Thats why I made the mixins.

You only need to change the prop!

// BasicChart.js

import {Bar} from 'vue-chartjs'
import reactiveProp from './mixin.js'

export default Bar.extend({
  mixins: [reactiveProp],
  mounted () {
    this.renderChart(this.chartData)
  }
})

So, I don’t know your environment but then you have in your main vue instance or antoher vue file something like this:

<template>
  <div class="small">
    <bug :chart-data="datacollection"></bug>
    <button @click="fillData()">Randomize</button>
  </div>
</template>

<script>
  import Bug from './Bug.js'

  export default {
    components: {
      Bug
    },

    data () {
      return {
        datacollection: {}
      }
    },

    mounted () {
      this.fillData()
    },

    methods: {
      fillData () {
        this.datacollection = {
          labels: ['January' + this.getRandomInt(), 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
          datasets: [
            {
              label: 'Data One',
              backgroundColor: '#f87979',
              data: [this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt()]
            }
          ]
        }
      },

      getRandomInt () {
        return Math.floor(Math.random() * (50 - 5 + 1)) + 5
      }
    }
  }
</script>

If you change your data with this.chart.datasets[0] = newData the mixin can’t work. Because the mixin is watching the chartData prop.

So you only need to change the data you passing in the :chart-data=<your data here> prop.

0reactions
XavierAgostinicommented, Mar 27, 2017

webpack with the latest version

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chart.js pie chart not rendering after update - Stack Overflow
After a lot of testing and researching, I found the solution. First, the chart must be initialised and then the data must be...
Read more >
Render Piechart - Microsoft Community Hub
Hi All,. I want to create a pie chart that can populate the count of two types VMs that has either security or...
Read more >
Reusable react component with Canvas doesn't render ...
Coding example for the question Reusable react component with Canvas doesn't render canvas with its props-Chart.js.
Read more >
Using Chart.js in React - LogRocket Blog
If you're working from the terminal, you can install these ... can then be passed to a PieChart component, which will render a...
Read more >
Visualization: Pie Chart - Google Developers
A pie chart that is rendered within the browser using SVG or VML. ... The format for id isn't yet documented (they're the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found