在Angular 4上使用Chart.js

Gus*_*avo 15 javascript typescript chart.js angular

我正在尝试将Chart.js与Angular 4一起使用,我在chart.js文档中看到了一个示例,但它使用<script>标记来拉取脚本,因此它不适用于组件.这就是我试图融入Angular的方式:

TS

export class GraphicsComponent implements OnInit {

  ctx = document.getElementById("myChart");
  myChart = new Chart(this.ctx, {
    type: 'pie',
    data: {
        labels: ["New", "In Progress", "On Hold"],
        datasets: [{
            label: '# of Votes',
            data: [1,2,3],
            backgroundColor: [
                'rgba(255, 99, 132, 1)',
                'rgba(54, 162, 235, 1)',
                'rgba(255, 206, 86, 1)'
            ],
            borderWidth: 1
        }]
    },
    options: {
      responsive: false,
      display:true
    }
  });

  constructor() { }

  ngOnInit() {
  }

}
Run Code Online (Sandbox Code Playgroud)

HTML

<canvas id="myChart" width="700" height="400"></canvas>
Run Code Online (Sandbox Code Playgroud)

知道如何让它工作吗?

编辑:我已经导入更新我的代码,我现在收到一个错误.

进口代码:

import * as Chart from 'chart.js'
Run Code Online (Sandbox Code Playgroud)

chrome控制台出错:

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'length' of null
TypeError: Cannot read property 'length' of null
    at Object.acquireContext (platform.dom.js:333)
    at Chart.construct (core.controller.js:74)
    at new Chart (core.js:42)
    at new GraphicsComponent (graphics.component.ts:12)
    at createClass (core.es5.js:10922)
    at createDirectiveInstance (core.es5.js:10756)
    at createViewNodes (core.es5.js:12197)
    at createRootView (core.es5.js:12092)
    at callWithDebugContext (core.es5.js:13475)
    at Object.debugCreateRootView [as createRootView] (core.es5.js:12792)
    at Object.acquireContext (platform.dom.js:333)
    at Chart.construct (core.controller.js:74)
    at new Chart (core.js:42)
    at new GraphicsComponent (graphics.component.ts:12)
    at createClass (core.es5.js:10922)
    at createDirectiveInstance (core.es5.js:10756)
    at createViewNodes (core.es5.js:12197)
    at createRootView (core.es5.js:12092)
    at callWithDebugContext (core.es5.js:13475)
    at Object.debugCreateRootView [as createRootView] (core.es5.js:12792)
    at resolvePromise (zone.js:795)
    at resolvePromise (zone.js:766)
    at zone.js:844
    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:425)
    at Object.onInvokeTask (core.es5.js:3881)
    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:424)
    at Zone.webpackJsonp.../../../../zone.js/dist/zone.js.Zone.runTask (zone.js:192)
    at drainMicroTaskQueue (zone.js:602)
    at <anonymous>
Run Code Online (Sandbox Code Playgroud)

Z. *_*ley 44

您可以在类型脚本环境(如Angular)中安装软件包以及完整功能的类型:

npm install --save chart.js && npm install --save-dev @types/chart.js

然后在您的组件中,您现在import * as Chart from 'chart.js'可以在打字稿环境中使用它.查看此示例以了解使用typescript的实现方法.

因为您需要从DOM获取画布,所以在尝试访问它之前需要确保它已呈现.这可以通过以下方式实现AfterViewInit.

import { Component, AfterViewInit } from '@angular/core';
import * as Chart from 'chart.js'

export class MyChartComponent implements AfterViewInit {

  canvas: any;
  ctx: any;

  ngAfterViewInit() {
    this.canvas = document.getElementById('myChart');
    this.ctx = this.canvas.getContext('2d');
    let myChart = new Chart(this.ctx, {
      type: 'pie',
      data: {
          labels: ["New", "In Progress", "On Hold"],
          datasets: [{
              label: '# of Votes',
              data: [1,2,3],
              backgroundColor: [
                  'rgba(255, 99, 132, 1)',
                  'rgba(54, 162, 235, 1)',
                  'rgba(255, 206, 86, 1)'
              ],
              borderWidth: 1
          }]
      },
      options: {}
    });
  }

}
Run Code Online (Sandbox Code Playgroud)

  • 类型包现在是`@ types/chart.js` (3认同)
  • @Ahmed *chart.js* 已更新其 API,不再支持该选项。我已将其从答案中删除,现在应该可以使用了。如果您需要隐藏项目,我建议使用 `[hidden]="isHidden"` 直接设置 HTML DOM 的样式。 (2认同)