[Chartjs]-Error : "Uncaught TypeError: Cannot read property 'length' of null" Chart.js

7๐Ÿ‘

โœ…

the problem is this line:

var ctx = document.getElementById('sexe');

you do not have any element with id โ€œsexeโ€ on the page

0๐Ÿ‘

The error was due to the element not being present in the HTML when Chart.js was trying to attach the chart on it.

0๐Ÿ‘

If u are using Angular 2+, try to build the options and data chart in. I needed to do this because I used an @Input() attribute to get the data shown in the chart:

    ngOnChanges(changes: SimpleChanges){
      this.options = {
       responsive: true,
       maintainAspectRatio: false,
       ...
      }
    } 

Thatโ€™s works for me. Hope it helps someone.

Leave a comment