18

I'm using Angular2-rc4 with angular-cli webpack and would like to implement a chart.js library.

I've installed chart.js to my project using:

npm install chart.js --save

Then I've tried to import the chart.js in my component:

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

@Component({
  selector: 'app-dashboard',
  templateUrl: 'dashboard.component.html',
  styleUrls: ['dashboard.component.scss']
})
export class DashboardComponent {

  chart: Chart;

}

But I get an error in console log:

[default] /Applications/MAMP/htdocs/bridge/src/app/dashboard/dashboard.component.ts:12:9 
Cannot find name 'Chart'.

What am I doing wrong?

1
  • have you got solution? Commented Dec 26, 2016 at 12:40

6 Answers 6

32

I had a similar issue, it turned out I was referencing an old example.

First, as you've already correctly done, install the library using NPM:

npm install chart.js --save

Then, in your component, import the library:

import Chart from 'chart.js';

To get up and running with a quick example, have a look at the example code in the Chart.js documentation or see my example below.


dashboard.component.ts

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

@Component({
    selector: 'app-dashboard',
    template: '<canvas #donut></canvas>'
})

export class DashboardComponent implements OnInit {
    @ViewChild('donut') donut: ElementRef;

    constructor(
    ) { }

    ngOnInit() {
        let donutCtx = this.donut.nativeElement.getContext('2d');

        var data = {
            labels: [
                "Value A",
                "Value B"
            ],
            datasets: [
                {
                    "data": [101342, 55342],   // Example data
                    "backgroundColor": [
                        "#1fc8f8",
                        "#76a346"
                    ]
                }]
        };

        var chart = new Chart(
            donutCtx,
            {
                "type": 'doughnut',
                "data": data,
                "options": {
                    "cutoutPercentage": 50,
                    "animation": {
                        "animateScale": true,
                        "animateRotate": false
                    }
                }
            }
        );
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I added a second parameter of null to @ViewChild: @ViewChild('donut', null)
22

In the template don't forget to enclose canvas in a div. If canvas is a direct child of custom directive in dom, then chart might not load.

<div><canvas id="myChart"></canvas></div>

I have wasted lot of time in finding this issue.

1 Comment

Do you know why that is the case?
5

Here is the best module out of few on npmjs:

angular2-chartjs

Then you can use it like this in your module:

import { ChartModule } from 'angular2-chartjs';

@NgModule({
  imports: [ ChartModule ]
  // ...
})
export class AppModule {
}

And in html template:

<chart [type]="type" [data]="data" [options]="options"></chart>

Don't forget to fill it with data ;)

Comments

2

If you are using webpack you may try adding the chart.js file (or the minified version) to the entry property as follows:

...
entry: {
      'chartJS': './node_modules/chart.js/dist/Chart.bundle.min.js',
      // Other mappings
}
...

Comments

1

I had this problem using <p-chart> primeNG.

Just add this in your index.html:

<script src= "https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.bundle.min.js" charset="utf-8"></script>

And that´s it, you don´t have to import in your typescript.

I found that in chart documentation

-then use a Chart.js CDN.

https://cdnjs.com/libraries/Chart.js

Comments

1

I tried. this works for me easy to implements https://valor-software.com/ng2-charts/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.