如何在Angular2中实现chart.js

18

我正在使用带有angular-cli webpack的Angular2-rc4,并想要实现chart.js库。

我已经使用以下命令将chart.js安装到我的项目中:

npm install chart.js --save

然后我尝试在我的组件中导入chart.js:

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;

}

但是我在控制台日志中得到了一个错误:

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

我做错了什么?


你有解决方案吗? - Sandip - Frontend Developer
6个回答

32

我遇到了类似的问题,后来发现是我引用了一个旧的示例。

首先,如您已经正确地完成了,使用NPM安装库:

npm install chart.js --save

然后,在你的组件中导入该库:

import Chart from 'chart.js';

要快速上手并了解一个示例,请查看Chart.js文档中的示例代码(链接)或看下面的示例。


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

我在@ViewChild中添加了第二个参数null:@ViewChild('donut', null) - Heiner

22
在模板中不要忘记将canvas包含在一个div中。 如果canvas是dom中自定义指令的直接子元素,则图表可能无法加载。
<div><canvas id="myChart"></canvas></div>

我在寻找这个问题上浪费了很多时间。


你知道为什么会这样吗? - platzhersh

5

以下是npmjs上最好的几个模块之一:

angular2-chartjs

然后在你的模块中可以像这样使用它:

import { ChartModule } from 'angular2-chartjs';

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

在HTML模板中:

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

不要忘记填写数据 ;)

2
如果您正在使用Webpack,您可以尝试将chart.js文件(或缩小版)添加到entry属性中,如下所示:
...
entry: {
      'chartJS': './node_modules/chart.js/dist/Chart.bundle.min.js',
      // Other mappings
}
...

1

我在使用primeNG的<p-chart>时遇到问题。

只需将以下内容添加到您的index.html中:

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

就是这样,您不需要在typescript中导入。

我在图表文档中发现了这个信息。

-然后使用Chart.js CDN。

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


1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接