gpt4 book ai didi

javascript - Flot 类别条形图的不同颜色条

转载 作者:可可西里 更新时间:2023-11-01 01:36:32 24 4
gpt4 key购买 nike

在 Flot 中使用“类别”模式时,如何为每个条设置不同的颜色?

此代码使每个条形成为 colors 数组中的第一个颜色。我希望每个条形都是 colors 数组中的相应颜色。

当前代码:

var data = [["Red",1],["Yellow",2],["Green",3]];

$.plot("#placeholder1div",[data], {
series: {
bars: {
show: true,
barWidth: 0.3,
align: "center",
lineWidth: 0,
fill:.75
}
},
xaxis: {
mode: "categories",
},
colors: ["#FF0000","#FFFF00","#00FF00"]
});

最佳答案

正如我对 Flot 的经常建议,删除插件并自行配置。

// separate your 3 bars into 3 series, color is a series level option
var data = [{data: [[0,1]], color: "red"},
{data: [[1,2]], color: "yellow"},
{data: [[2,3]], color: "green"}];

$.plot("#placeholder",data, {
series: {
bars: {
show: true,
barWidth: 0.3,
align: "center",
lineWidth: 0,
fill:.75
}
},
xaxis: {
// drop the categories plugin and label the ticks yourself
// you'll thank me in the long run
ticks: [[0,"Red"],[1,"Yellow"],[2,"Green"]]
}
});

enter image description here

运行代码:

var data = [{data: [[0,1]], color: "red"},
{data: [[1,2]], color: "yellow"},
{data: [[2,3]], color: "green"}];

$.plot("#placeholder",data, {
series: {
bars: {
show: true,
barWidth: 0.3,
align: "center",
lineWidth: 0,
fill:.75
}
},
xaxis: {
ticks: [[0,"Red"],[1,"Yellow"],[2,"Green"]]
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flot/0.8.3/jquery.flot.js"></script>
<div id="placeholder" style="width:400px; height: 300px"></div>

关于javascript - Flot 类别条形图的不同颜色条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23273055/

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