如何为chart.js饼图数据集指定不同的背景色?
我可以在下面创建chart.js饼图:如何为chart.js饼图数据集指定不同的背景色?,chart.js,Chart.js,我可以在下面创建chart.js饼图: this.httpClient.get(this.url).subscribe((res: Stock[]) => { res.forEach(holding => { this.company.push(holding.company); this.price.push(holding.price); }); this.chart = new Chart('canvas',
this.httpClient.get(this.url).subscribe((res: Stock[]) => {
res.forEach(holding => {
this.company.push(holding.company);
this.price.push(holding.price);
});
this.chart = new Chart('canvas', {
type: 'pie',
data: {
labels: this.company,
datasets: [
{
data: this.price,
borderColor: '#3cba9f',
backgroundColor: 'rgba(35, 206, 107, 0.5)',
fill: false
}
]
},
options: {
legend: {
display: true
}
}
});
});
目前,饼图的每个部分都有相同的backgroundColor
,正如我在上面指定的那样
我的问题是,我希望饼图的每个部分都有不同的背景色。有人能告诉我如何使用上述代码进行此更改吗?您可以将一组字符串(颜色)传递到
backgroundColor
:
backgroundColor: ['red', 'green', 'blue']
例如:
新图表(document.getElementById(“图表”){
键入:“馅饼”,
数据:{
数据集:[{
数据:[1,2,3],
背景颜色:[“红色”、“绿色”、“蓝色”]
}]
}
});代码>