【发布时间】:2019-04-09 14:50:39
【问题描述】:
下面的代码只能用于图表,但我必须制作多个图表。如何使以下代码成为图表数组并在 Canvas 中使用 *ngFor 显示。
@ViewChild('pr_chart') chartElementRef: ElementRef;
ngAfterViewInit() {
let ctx = this.chartElementRef.nativeElement;
ctx = ctx.getContext('2d');
this.chart = new Chart(ctx, {
type: 'line',
options: {
legend: {
display: false
},
scales: {
xAxes: [{
display: false
}],
yAxes: [{
display: false
}],
}
},
data: {
labels: [1500, 1600, 1700, 1750, 1800, 1850, 1900, 1950, 1999, 2050],
datasets: [{
data: [86, 378, 106, 306, 507, 111, 133, 221, 783, 5000],
// label: "Africa",
borderColor: '#3bd19c',
fill: false
}]
}
});
}
<canvas #pr_chart></canvas>
【问题讨论】:
-
您是否尝试使用
ViewChildren而不是ViewChild来获取匹配引用列表而不是单个引用? -
我对此不熟悉,你能分享一下这方面的例子吗?
标签: angular canvas chart.js ngfor