javascript - ChartJS 如何将颜色设置为一个栏
问题描述
我有这个条形图,我很难通过脚本将颜色设置为索引的一个条形。贝娄是我的图表条码:
var ctx = document.getElementById("myBarChart");
var myBarChart = new Chart(ctx, {
type: 'bar',
data: {
labels: [],
datasets: [{
label: "Valor",
backgroundColor: "rgba(2,117,216,1)",
borderColor: "rgba(2,117,216,1)",
data: [],
}],
},
options: {
plugins: {
datalabels: {
anchor: 'end',
align: 'top',
formatter: function (value, context) {
if (value != 0) {
return new Date(value * 1000).toISOString().substr(11, 8);
} else {
value = " "
return value;
}
},
font: {
weight: 'bold'
}
}
},
responsive: true,
tooltips: {
/* intersect: false, */
callbacks: {
title: function (tooltipItem, data) {
return data['labels'][tooltipItem[0]['index']];
},
label: function (tooltipItem, data) {
return new Date(parseInt(data['datasets'][0]['data'][tooltipItem[
'index']]) * 1000).toISOString().substr(11,
8);
},
},
},
scales: {
xAxes: [{
time: {
unit: 'month'
},
gridLines: {
display: false
},
}],
yAxes: [{
ticks: {
min: 0,
suggestedMax: 36000,
stepSize: 3600,
beginAtZero: true,
},
}],
},
}
});
我试过这个:myBarChart.data.datasets[0].backgroundColor[0]
但它不起作用。如果有人知道如何在最新版本的 chartjs 中执行此操作,请帮助我。
解决方案
您可以为该backgroundColor
属性提供一个数组。在此数组中,您可以使用默认颜色填充它,但您希望条形图为不同颜色的索引除外
推荐阅读
- javascript - 页面加载时在 Javascript/Jquery 中对引导模态体元素进行排序
- c++ - 位摆弄:否定一个int,只有当它是负数时
- javascript - 鼠标按下附加项目
- c# - 批量重命名重构
- python - Cython 函数中高度可变的执行时间
- python-3.x - 如何从 Web 应用程序 WindGuru 中检索 html 源代码
- puppeteer - 无头 Chrome 节点 API 和 Puppeteer 安装
- gpu - PGI 18.1 与 PGI 18.4
- tensorflow - 从 Fast-RCNN 获取特征向量
- python - 在查找导数时,如何在 python 3 中使用 filter 函数仅返回导数未乘以零的项?