Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

Cannot read property 'transition' of null

Open shehan360 opened this issue 8 years ago • 20 comments

Im trying to draw a line chart using ChartJs and Agular4 that takes data from @Input.

  ngOnChanges(){
		if(this.labelY  && this.dataSet){
			console.log(this.dataSet);
			console.log(this.labelY);
			setTimeout(()=>{
				this.draw();
			},500);
		}
		
	}

Without the timeout, chartjserr error is generated. Any idea on resolving this issue?

Im using ChartJs 2.7.1 And Angular 4

draw(){
		if (this.mychart) {
			this.mychart.destroy();
		}
		let ctx = this.mycanvas.nativeElement.getContext('2d');
    this.mychart=new Chart(ctx,
      {type: 'line',
			data: {
				labels:this.labels,
				datasets: this.dataSet
			},
			options: {
				responsive: true,
				elements: {
					line: {
							tension: 0 // disables bezier curves
					}
				},
				
				scales: {
					xAxes: [{
						type: "time",
						display: true,
						scaleLabel: {
							display: true,
							labelString: 'Time'
						},
                        ticks: {
                            major: {
                                fontStyle: "bold",
                                fontColor: "#FF0000"
                            }
                        }
					}],
					yAxes: [{
						display: true,					
						scaleLabel: {
							display: true,
							labelString: this.labelY
						},
						ticks: {
							min: 0, // it is for ignoring negative step.
							beginAtZero: true,
							stepSize: 1  // if i use this it always set it '1', which look very awkward if it have high value  e.g. '100'.
					}
					}]
				}
			}
          
        });    
  }

shehan360 avatar Jan 14 '18 19:01 shehan360

@shehan360 do you have a fiddle that reproduces this issue that we can use for debugging?

etimberg avatar Jan 14 '18 21:01 etimberg

Guys the problem arose from Angular input binding problem. Not related to ChartJs.

shehan360 avatar Jan 17 '18 18:01 shehan360

I think this is a chart.js problem. If you do quick dataset replacement (i.e. remove then add new) during animation / transition, the exception will be raised. Occurs also for animation duration: 0

marcinn avatar Feb 08 '18 13:02 marcinn

Anyone solved for Cannot read property 'transition' of null issue? I also getting that issue when chart data source change quickly.

MinChanSike avatar Apr 26 '18 06:04 MinChanSike

@MinChanSike are you getting the problem on Angular?

shehan360 avatar Apr 26 '18 06:04 shehan360

no, I got in sample html page when I try to refresh data quickly.

MinChanSike avatar Apr 26 '18 16:04 MinChanSike

I am also getting that error when I'm trying to push data into the charts.

AwaisKazi avatar May 29 '18 13:05 AwaisKazi

Try to call mychart.update() after setting the new data... that solved the problem for me.

soumas avatar Jun 13 '18 19:06 soumas

This error occurs when you just initialized the graph and overwrite the dataset almost immediately after you have initialized the graph (e.g., initializing the graph with empty dataset and sending an HTTP request to get a dataset from the server).

I was able to solve this error by completely disabling the animation in the options. My graph options:

var options = {
    responsive: true,
    maintainAspectRatio: false,
    animation: {
        duration: 0
    },
    hover: {
        animationDuration: 0
    },
    responsiveAnimationDuration: 0
};

heartz66 avatar Jun 17 '18 22:06 heartz66

chartInstance.update() solved the problem in my case.

binDongKim avatar Jun 19 '18 08:06 binDongKim

I'm using Angular and had the same problem. I solved it by adding a ngDoCheck() to my chart and using chart.update() in the DoCheck hope it helps!

KimiaBashiran avatar Jul 25 '18 11:07 KimiaBashiran

@KimiaBashiran isn't ngDoCheck lifecycle called too many times?

binDongKim avatar Jul 25 '18 13:07 binDongKim

The "Uncaught TypeError" is still an issue, WHat @Heartz66 suggests does not work for me. The only workaround that consistently works for me is to include chart.update() - it does not seem to matter if animation is disabled. Fiddle here: https://jsfiddle.net/hdahle/z18ovxnk/6/

hdahle avatar Jan 22 '19 03:01 hdahle

I just encountered this problem too. I've opened a new issue since this one was closed. See https://github.com/chartjs/Chart.js/issues/6207

PaulWieland avatar Apr 15 '19 14:04 PaulWieland

as discussed in #6207, calling upate() on the chart after modifying the data solves this issue.

henon avatar Oct 15 '20 17:10 henon

Try to call mychart.update() after setting the new data... that solved the problem for me.

mine was solved by this

DanteDX avatar Nov 01 '20 07:11 DanteDX

mt too. update() is solved

masayomitan avatar Feb 24 '21 03:02 masayomitan

i had similar issue, after reading the source code adding datasetIndex to the Chart.LegendItem did the trick

ctrl-d avatar May 16 '22 05:05 ctrl-d

Try to call mychart.update() after setting the new data... that solved the problem for me.

Thank you so much, seems like my data had not arrived in time and updating the chart fixed the issue

HackintoshwithUbuntu avatar Oct 09 '22 09:10 HackintoshwithUbuntu

i had similar issue, after reading the source code adding datasetIndex to the Chart.LegendItem did the trick

Thanks @ctrl-d this solves my issue.

haseebahmad95 avatar Sep 25 '24 11:09 haseebahmad95