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.js v2 not rendering in Angular 2

See original GitHub issue

I was able to render charts with v1.0.2 in Angular 2, but with v2.0.0 it doesn’t render anything in the screen and doesn’t log anything on the console.

I’m using the example provided in v2 docs:

constructor(el: ElementRef) {}

ngAfterViewInit() {
    var ctx = this.el.nativeElement.firstChild.getContext("2d");
    var myChart = new Chart(ctx, {
        type: 'bar',
        data: {
            labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
            datasets: [{
                    label: '# of Votes',
                    data: [12, 19, 3, 5, 2, 3]
            }]
        },
        options:{
            scales:{
                yAxes:[{
                        ticks:{
                            beginAtZero:true
                        }
                    }]
            }
        }
      });
  }

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

8reactions
etimbergcommented, Apr 2, 2016

@hertz1 this issue seems to be that the <chart> component has no size. V2 generally sizes to fit the parent. I added a container div around the canvas that is 100% width and height. Updated the part that gets the canvas context.

The result is: https://plnkr.co/edit/RbhYj4uEeSC8MSZZHBe3?p=preview

0reactions
jhiemercommented, Mar 22, 2016

@hertz1 works for me, take a look here:

https://github.com/evoila/angular2-chartjs2

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chart.js chart doesn't render when using Angular 2 [duplicate]
I am attempting to use Chart.js 2.0 in an Angular2 application that is written in typescript. To start with I've just been trying...
Read more >
chart.js not getting rendered inside ng-template in angular ...
It seems like that because you are using trying to plot the chart in a sub template it is not available known in...
Read more >
Line Chart - Chart.js
Line Chart. A line chart is a way of plotting data points on a line. Often, it is used to show trend data,...
Read more >
Chart.js
At the same time, canvas rendering disallows CSS styling, so you will have to use built-in options for that, or create a custom...
Read more >
Updating Charts - Chart.js
Adding and removing data is supported by changing the data array. To add data, just add data into the data array as seen...
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