这是我从chart.js网站的教程中获得的代码https://www.chartjs.org/docs/master/getting-started/usage.html 我也看了这个视频https://www.youtube.com/watch?v=zcyiuccto20 没错,所以我不知道我哪里出错了。做了很多研究,但没有找到我的问题。提前感谢您的帮助。
import { Component, OnInit } from '@angular/core';
import { Chart } from '../../../node_modules/chart.js'
@Component({
selector: 'app-recent-graph',
templateUrl: './recent-graph.component.html',
styleUrls: ['./recent-graph.component.css']
})
export class RecentGraphComponent implements OnInit {
constructor() { }
ngOnInit() {
var myChart = new Chart("myChart", {
type: 'bar',
data: {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
});
}
}
'''
HTML:
'''
<div id="divChart">
<canvas id="myChart"></canvas>
</div>
'''
1条答案
按热度按时间x33g5p2x1#
您的图表导入错误,如果使用括号,您必须导入所有要使用的组件并注册它们,或者如果您这样导入,您可以忽略树状结构并让图表处理
import Chart from 'chart.js/auto'
有关树形图导入和注册的信息,请参阅文档