gpt4 book ai didi

javascript - 条形图中每个条形的不同颜色;图表JS

转载 作者:IT王子 更新时间:2023-10-29 02:51:27 25 4
gpt4 key购买 nike

我在我正在处理的项目中使用 ChartJS,我需要为条形图中的每个条使用不同的颜色。

下面是条形图数据集的示例:

    var barChartData = {
labels: ["001", "002", "003", "004", "005", "006", "007"],
datasets: [{
label: "My First dataset",
fillColor: "rgba(220,220,220,0.5)",
strokeColor: "rgba(220,220,220,0.8)",
highlightFill: "rgba(220,220,220,0.75)",
highlightStroke: "rgba(220,220,220,1)",
data: [20, 59, 80, 81, 56, 55, 40]
}]
};

有什么方法可以不同地绘制每个条形图吗?

最佳答案

从 v2 开始,您可以通过 backgroundColor 属性简单地指定一个值数组来对应每个条的颜色:

datasets: [{
label: "My First dataset",
data: [20, 59, 80, 81, 56, 55, 40],
backgroundColor: ["red", "blue", "green", "blue", "red", "blue"],
}],

这也适用于 borderColorhoverBackgroundColorhoverBorderColor

来自 Bar Chart Dataset Properties 上的文档:

Some properties can be specified as an array. If these are set to an array value, the first value applies to the first bar, the second value to the second bar, and so on.

关于javascript - 条形图中每个条形的不同颜色;图表JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25594478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com