【发布时间】:2018-01-01 00:02:28
【问题描述】:
这真的是我第一次使用 Chart.js,我将它导入到一个 Angular 组件中。我试图在这一点上创建一个简单的条形图。我在控制台中收到以下错误:
core.controller.js:118 Failed to create chart: can't acquire context from the given item
不知道我在这里做错了什么!
这是我的组件的 TS 文件:
import { Component, OnInit } from '@angular/core';
import { Chart } from 'chart.js';
import { data } from './data';
@Component({
selector: 'app-propel-factor',
templateUrl: './propel-factor.component.html'
})
export class PropelFactorComponent implements OnInit {
chart = [];
labels: any = [];
data: any = [];
constructor() { }
ngOnInit() {
data.forEach(item => {
this.labels.push(item.name);
this.data.push(item.value);
});
this.chart = new Chart('canvas', {
type: 'bar',
labels: this.labels,
data: {
labels: this.labels,
data: this.data
},
options: {
responsive: true
}
});
}
}
然后我的模板就是:
<div *ngIf="chart">
<canvas id="canvas">{{ chart }}</canvas>
</div>
我还想公开表示我尝试了此修复 chart.js Failed to create chart: can't acquire context from the given item 并且仍然出现相同的错误!
【问题讨论】:
-
这个答案可能会帮助您开始使用 chart.js:stackoverflow.com/questions/46328575/…
-
@Z.Bagley 非常感谢。
-
你能解决这个问题吗...?如果您已解决此问题,请添加您的答案。
标签: javascript angular