javascript - 将 X 轴值的颜色更改为多颜色值 - Chart.js
问题描述
我正在使用 Chart.js ( http://www.chartjs.org/docs/ ) 绘制图表。我的图表类型是条形图。
X 轴的标签有 4 行。I 更改 X 轴值的颜色。值的颜色是一种颜色。但我希望每种颜色有一条线,颜色与条形颜色相同。
var barChartData = {
labels: [["Injection", 10, 20], // here I want to change the color
["Electronics", 5, 15],
["TOTAL", 15, 35]
],
datasets: [{
label: "2018",
backgroundColor: window.chartColors.orange,
yAxisID: 'A',
data: [10, 5, 15]
}, {
label: "2017",
backgroundColor: window.chartColors.green,
yAxisID: 'A',
data: [20, 15, 35]
}]
};
var canvas = document.getElementById('canvas').getContext('2d');
new Chart(canvas, {
type: 'bar',
data: barChartData,
options: {
scales: {
yAxes: [{
id: 'A',
type: 'linear',
position: 'left',
}, {
id: 'B',
type: 'linear',
position: 'right',
ticks: {
max: 100,
min: 0
}
}],
xAxes: [{
ticks: {
fontColor: "#222", // This here that I changed.
},
}]
}
}
})
我想改变标签的颜色是 10、5、15 是橙色,20、15、35 是绿色,而 Injection、Electronics、TOTAL 是黑色
我可以这样做吗?如何?
解决方案
var myData = [
["id1","test 11111","AA",1.95],
["id2","test 2","BB",1.94],
["id3","test 3","CC",1.93],
["id4","test 4","DD",1.93],
["id5","test 5","EE",1.91],
["id6","test 6","FF",1.90],
["id7","test 7","GG",1.82],
["id8","test 8","HH",1.85],
["id9","test 9","II",1.83],
["id10","test 10","JJ",1.79]
];
var ctx = $("#c");
var myChart = new Chart(ctx, {
type: 'bar',
data: {
datasets: [{
label: '# of Votes',
xAxisID:'modelAxis',
data: myData.map((entry)=>entry[3])
}]
},
options:{
scales:{
xAxes:[
{
id:'modelAxis',
type:"category",
ticks:{
//maxRotation:0,
autoSkip: false,
callback:function(label, x2, x3, x4){
return label;
}
},
labels:myData.map((entry=>entry[1]))
},
{
id:'groupAxis',
type:"category",
gridLines: {
display: false,
drawBorder: false,
drawOnChartArea: false,
offsetGridLines: false
},
ticks:{
padding:0,
maxRotation:0,
fontSize: 10,
fontColor: '#FF9090',
autoSkip: false,
callback:function(label){
return label;
}
},
offset: true,
labels:myData.map((entry=>entry[1]))
},{
id:'groupAxis2',
type:"category",
gridLines: {
display: false,
drawBorder: false,
drawOnChartArea: false,
offsetGridLines: false
},
scaleLabel:{
padding: 10,
},
ticks:{
padding:0,
fontSize: 10,
fontColor: '#AB64F4',
maxRotation:0,
autoSkip: false,
callback:function(label){
return label;
}
},
offset: true,
labels:myData.map((entry=>entry[1]))
}
],
yAxes:[{
ticks:{
beginAtZero:true
}
}]
}
}
});
推荐阅读
- zabbix - Zabbix 5.4 - 覆盖模板宏
- python - 从一个字典中获取值并将另一个字典的值添加到它
- python - 导入 CSV 文件
- conv-neural-network - 如何将 Galaxy Zoo(或其他天体数据集)的数据用于带有卷积神经网络的星系分类器
- uat - 在 UAT 中部署后收到错误消息“匹配 [上下文/管理器] 无法设置属性 [随机类]”。在 Java8 中使用 Spingboot 2.5.1 版本
- c++ - 删除向量问题内的向量
- javascript - 选择特定下拉菜单的值时获取它的值
- api - 如何将 JSON 正文从 Slack 发送到 API Gateway
- php - IIS10 和 PHP 8 上的 opendir
- python - dcc.Loading 仅在首次加载时 (Python)