score: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

score:1

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

score: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
}
...

score: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 ;)

score: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.

score: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
                    }
                }
            }
        );
    }
}

Related Query

More Query from same tag