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.

Chart does not update on this.$data._chart.update()

See original GitHub issue

Expected Behavior

Chart should update on this.$data._chart.update()

Actual Behavior

Chart does not update.

Am I missing something? dataPoints is reactive and receives new values which the watcher successfully spots and trigger this.$data._chart.update() but nothing happens.

<script lang="ts">
  import { Component, Mixins, Prop, Watch } from 'vue-property-decorator'
  import { Line } from 'vue-chartjs'
  import { ChartOptions } from 'chart.js'

  @Component({
    components: {}
  })
  export default class ChartLineNonReactive extends Mixins(Line) {
    @Prop({ default: () => [] }) dataPoints!: number[]
    @Prop() scaleMin!: number
    @Prop() scaleMax!: number
    @Prop() reverse?: boolean

    $refs!: {
      canvas: any
    }

    gradient: CanvasGradient | null = null

    get chartData() {
      return {
        labels: this.dataPoints.map((d, i) => i.toString()),
        datasets: [
          {
            data: this.dataPoints,
            backgroundColor: 'transparent',
            borderWidth: 4,
            borderColor: this.gradient!,
            pointBorderWidth: 2
          }
        ]
      }
    }

    get options(): ChartOptions {
      return {
        responsive: true,
        maintainAspectRatio: false,
        layout: { padding: 10 },
        legend: { display: false },
        scales: {
          display: false,
          xAxes: [{
            display: false,
            ticks: {}
          }],
          yAxes:
            [{
              ticks: {
                min: this.scaleMin,
                max: this.scaleMax
              },
              display: false
            }]
        }
      }
    }

    mounted() {
      this.gradient = this.$refs.canvas.getContext('2d').createLinearGradient(0, 0, 0, this.$refs.canvas.height)

      const positive = '#4CD287'
      const warning = '#FFBE60'
      const negative = '#FF8062'

      this.gradient!.addColorStop(this.reverse ? 0 : 1, negative)
      this.gradient!.addColorStop(0.5, warning)
      this.gradient!.addColorStop(this.reverse ? 1 : 0, positive)

      this.renderChart(
        this.chartData,
        this.options
      )
    }

    @Watch('chartData')
    changed(newVal: any, oldVal: any) {
      this.$data._chart.update()
    }
  }
</script>

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
aperturelesscommented, Nov 29, 2019

This is indeed weird.

Can you check if instead of calling update() calling this.renderChart(this.chartData,this.options) solves this?

How are you getting your data? Are you using an api?

1reaction
artiomnistcommented, Jun 30, 2020

I have encountered the same issue. with this.chartOptions and my own watcher (as advised by the docs) triggering this.$data._chart.update() it does not update the chart.

My temporary solution was to disable animation and just re-render the chart on every update. Not great, or performant but it is working.

Any idea when this will be fixed?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't update chart - Stack Overflow
Therefore, try to change your updateConfigByMutating from... function updateConfigByMutating() { chart.options.plugins.title.text = 'new title'; ...
Read more >
Updating Charts - Chart.js
It's pretty common to want to update charts after they've been created. When the chart data or options are changed, Chart.js will animate...
Read more >
Charts not updating when data is updated
Charts not updating when data is updated. Hello,. I have built an Excel dashboard template that has 46 charts referencing cells in a...
Read more >
Overview – Updating chart options - CanvasJS.com
Tutorial on Updating Chart Data and Options Dynamically | CanvasJS JavaScript Charts. ... though such an update rate will not be required in...
Read more >
Updating charts | AnyLogic Help
Automatic update · Select the chart in the graphical editor by clicking it. · Go to the Properties. · In the Data update...
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