javascript - chart.js 标签颜色根据值变化
问题描述
我有一个chart.js 的水平图表,看起来像这样。我的条形图
我正在寻找一种方法来更改左侧“Boozman John、Eldridge Conner 等”的标签。基于我从 ajax 调用中获得的列表中的值。
我尝试过类似的东西
labels {
fontColor: labelcolors
},
使用 for 循环获取列表“labelcolors”中的颜色但没有用。
这是我的代码
`var ctx = document.getElementById("GeneralChart");
var GeneralChart = new Chart(ctx, {
type: "horizontalBar",
data: {
labels: gen_candidate,
datasets: [
{
label: "Committee expenditure",
backgroundColor: "orange",
borderColor: "yellow",
borderWidth: 1,
xAxisID: "expenditure",
data: gen_expenditure
},
{
label: "General votes",
backgroundColor: "green",
borderColor: "green",
borderWidth: 1,
xAxisID: "votes",
data: general_votes
},
]
},
options: {
responsive: true,
legend: {
position: "top"
},
title: {
display: true,
text: "Money and Votes for General Election "+ state
},
scales: {
yAxes: [{
position:"left",
id: "y-axis",
ticks: {
autoSkip: false,
}
}],
xAxes: [{
type: 'linear', //type: 'linear', is needed for horizontal vote
position: "top",
id: "expenditure",
}, {
type: 'linear',
position: "bottom",
id: "votes",
ticks: {
beginAtZero: true
}
}]
}
}
});`
任何想法?
解决方案
利用:
ticks: {
fontColor: "#377216",
beginAtZero: true
}
推荐阅读
- python - 需要函数在类中分配对象名称
- flutter - Flutter:组合图表和动画包时出错
- python - OR-Tools Python - AddNoOverlap2D 不工作 - 库存切割问题
- python - 通过所有 10 项测试的嵌套列表 HackerRank 问题
- amazon-cloudformation - 具有复杂列类型的 Athena (Presto) 视图
- java - JavascriptExecutor 类型中的方法 executeScript(String, Object[]) 不适用于 Selenium 的参数 (String, WebElement) 错误
- flutter-dependencies - 我正在尝试在颤振中安装 firebase_storage 但出现错误 pod can not install 发生错误
- c# - 绑定到对象的 ListView 内的 TextBox,两种方式绑定不起作用
- php - 如何在 mongodb 中使用 laravel 7 护照?
- c# - Roslyn 中是否有方法或方式来枚举给定 C# 项目在编译时使用的所有程序集