Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chart crashes when reentering page #223

Closed
aleksrs opened this issue Oct 14, 2017 · 4 comments
Closed

Chart crashes when reentering page #223

aleksrs opened this issue Oct 14, 2017 · 4 comments

Comments

@aleksrs
Copy link

aleksrs commented Oct 14, 2017

Expected Behavior

Chart renders upon loading the page, and works fine. Go to another page, and come back and the chart should render and still work fine.

Actual Behavior

When leaving the page and come back the chart component crashes with the console error
Uncaught TypeError: Cannot read property 'clearRect' of null

Chart component:

export default {
    extends: Line,
    props: {
      chartData: {
        type: Array | Object,
        required: false
      }
    },
    data () {
      return {
        options: { // Chart.js options
          scales: {
            yAxes: [{
              ticks: {
                beginAtZero: true
              },
              gridLines: {
                display: true
              }
            }],
            xAxes: [ {
              gridLines: {
                display: false
              }
            }]
          },
          legend: {
            display: true
          },
          responsive: true,
          maintainAspectRatio: false
        }
      }
    },
    mounted () {
      this.renderChart({
        labels: ['Januar', 'Februar', 'Mars', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Desember'],
        datasets: [
          {
            label: 'Salg per måned',
            borderColor: 'rgba(66, 134, 244, 1)',
            pointBackgroundColor: 'white',
            borderWidth: 1,
            pointBorderColor: '#249EBF',
            backgroundColor: 'rgba(66, 134, 244, 0.2)',
            data: this.chartData
          }
        ]
      }, this.options)
      Event.$on('updateLineChart', () => {
        this._chart.update()
      })
    }
  }

Environment

  • vue.js version: 2.4.4
  • vue-chart.js version: 2.7.8
  • npm version: 5.4.2
@apertureless
Copy link
Owner

Your component looks okay. Please provide a reproduction.
Are you using vue-router ?

@aleksrs
Copy link
Author

aleksrs commented Oct 14, 2017

Can´t seem to be able to replicate the error. Yes i´m using vue-router. Do you think that might be the cause of error? I´ve also looked trough the issues in chart.js github. The closest i came here is this issue https://github.com/chartjs/Chart.js/issues/1377.

@apertureless
Copy link
Owner

Hm thats weird. I don't think that there is a problem with vue-router. It should work without problems because the chart.js instance get properly destroyed. https://github.com/apertureless/vue-chartjs/blob/develop/src/BaseCharts/Bar.js#L88

I will see if I can reproduce it.

@apertureless
Copy link
Owner

Well I could not reproduce it. I guess it rather an error in your code.
Could be related to

Event.$on('updateLineChart', () => {
        this._chart.update()
      })

I don't know when the event get triggered. Maybe the chart is not initialized.
You could try to wrap it in

if(this._chart) {...} to be sure the object is available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants