javascript - 图表js背景颜色
问题描述
我正在做一些图表,我需要帮助。我需要创建一个水平条形图,它具有一些与背景颜色相关的颜色值,最大数据为 5 条。
例如,我需要为以下 x 轴值设置背景颜色:
0 - 10 ->白色
10 - 16 ->黄色
16 - 20 ->绿色
20 - 30 ->橙色
> 30 ->红色
我有以下代码:
new Chart.Chart(($("#grahe")).getContext('2d'), <any>{
type: 'horizontalBar',
data: data,
plugins: [{
beforeDraw: function (chartInstance: any, easing) {
var ctx = chartInstance.chart.ctx;
var meta = chartInstance.getDatasetMeta(0);
var start = meta.data[3]._model.x;
var stop = meta.data[1]._model.x;
console.log(start, stop);
ctx.save();
ctx.fillStyle = "red";
console.log(chartArea);
ctx.fillRect(start, chartArea.top, stop - start, chartArea.bottom - chartArea.top);
ctx.restore();
}
}],
options: {
plugins: {
datalabels: {
color: (context) => {
return "white";
},
font: {
family: 'DINPro',
size: 0
}
},
},
tooltips: {
mode: 'index',
bodyFontSize: 14,
titleFontSize: 14
},
responsive: true,
legend: {
display: false
},
scales: {
xAxes: [
{
stacked: true,
ticks: {
min: 0
},
gridLines: {
color: "#555",
zeroLineColor: "#666"
}
}
],
yAxes: [
{
barThickness: 20,
stacked: true,
ticks: {
min: 0,
callback: (value, index, values) => {
return value;
}
},
gridLines: {
display: false,
color: "#555",
zeroLineColor: "#666"
}
}
]
}
}
});
但这是根据图表的数据填充背景,而不是 x 轴。
有什么想法吗?
谢谢
解决方案
对于寻找解决方案的人。我找到了一种使用 chartjs-plugin-annotation 的方法。
如果你想看看那里:)
推荐阅读
- javascript - 如何像另一个事件一样改变一个事件的行为?
- python - 如何在使用 pipenv 和 VS Code 时调试 Django?
- azure - privacySetting 参数值不是允许值的一部分 - Azure LogicApps 的 SQL 连接
- powershell - 您可以直接在 PowerShell 扩展字符串中使用格式说明符吗?
- java - iText 文档获取 URL
- php - MVC:如何将数组和变量从模型传递到视图?
- flutter - 布局问题:带有 TextField 键盘溢出的 TabBarView
- sql-server - Jmeter JDBC - 传递表值参数
- c - 堆栈中每个存储单元的大小是多少,是否可以拆分一个单元
- ruby-on-rails - 如何配置 database.yml 以便 rake db:create 创建多个数据库?