Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't my Angular 4 ChartJS component rendering?

I'm trying to figure out why why angular 4 ChartJS component isn't rendering.

I've installed the package using yarn, and it exists and imports correctly. I can see the Chart object in the chrome console, but the canvas element remains blank.

import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import Chart from 'chart.js';

@Component({
  selector: 'app-root',
  template: '<canvas #aChart></canvas>',
  styleUrls: ['./app.component.css']
})

export class AppComponent implements OnInit {
  @ViewChild('aChart') htmlChart: ElementRef;
  public data = {
    labels: [
      'Value A',
      'Value B'
    ],
    datasets: [
      {
        'data': [101342, 55342],   // Example data
        'backgroundColor': [
          '#1fc8f8',
          '#76a346'
        ]
      }]
  };

  constructor(public myElement: ElementRef) {}

  ngOnInit() {
    const ctx: CanvasRenderingContext2D = this.htmlChart.nativeElement.getContext('2d');
    const newChart: Chart = new Chart(ctx, {
      'type': 'doughnut',
      'data': this.data,
      'options': {
        'cutoutPercentage': 50,
        'animation': {
          'animateScale': true,
          'animateRotate': false
        }
      }
    });
    console.log('chart', newChart);
  }
}
like image 680
Jack Murphy Avatar asked Jul 30 '17 01:07

Jack Murphy


People also ask

What is the use of angular chartjs?

Chartjs is a very popular pure javascript library for making graphs and charts in our application. Angular chartjs or using chartjs in our angular we can create different types of charts with animate charts from our data.

What is chart JS?

Chart js is written in pure javaScript and html5 using canvas. Chart and graph are popular visuals ways of showing relationships between data. We can easily understand and remember data in chart form. We often use charts and graphs to demonstrate trends, patterns, and relationships between sets of data.

What is Highcharts angular library?

HighCharts Angular library is the official minimal Highcharts wrapper for Angular. We have a lot of other libraries’ options to add a chart in Angular. We already have articles on these angular charting libraries with an example. How to use Highcharts in an Angular?

What is react intro component not rendering?

REACT – Simple Intro Component Not Rendering? One of the great things about React is its flexible component system. Once you get a hang of it, you can break up your application into reusable components and include them all over your project.


2 Answers

You need to enclose canvas element inside div

template: '<div><canvas #aChart></canvas></div>',
like image 166
Aleksandr Petrovskij Avatar answered Sep 29 '22 08:09

Aleksandr Petrovskij


You need to ensure the containing element has a display type of `block'. As pointed out in earlier thread it can either be wrapped in a div, or in angular4, simply set the :host css element to:

:host { 
  display: block 
}
like image 38
Jack Murphy Avatar answered Sep 29 '22 09:09

Jack Murphy