javascript - Chartjs根据Y轴值不同的行背景颜色
问题描述
如何为chartjs背景添加多种颜色?我已经添加了单色,但我想要的就像下图一样
这是我的代码
chartArea: {
backgroundColor:'rgba(251, 85, 85, 0.2)',
}
我使用插件服务如下
Chart.pluginService.register({
beforeDraw: function (chart, easing) {
if (chart.config.options.chartArea && chart.config.options.chartArea.backgroundColor) {
var helpers = Chart.helpers;
var ctx = chart.chart.ctx;
var chartArea = chart.chartArea;
ctx.save();
ctx.fillStyle = chart.config.options.chartArea.backgroundColor;
ctx.fillRect(chartArea.left, chartArea.top, chartArea.right - chartArea.left, chartArea.bottom - chartArea.top);
ctx.restore();
}
}
});
谢谢你。
解决方案
这可以通过 chartjs-plugin-annotation 的框注解功能来实现。 https://github.com/chartjs/chartjs-plugin-annotation
推荐阅读
- windows - 是否可以将 roku 设备的操作系统更改为 Windows?
- scala - 如何在 Scala 用户定义的注释中使用命名参数?
- python - 为什么在使用数组迭代和提取元素时在列表中打印数组([])?
- c++ - 修改 QDockWidget 默认捕捉位置?
- c# - 通过弹出窗口输出 HTTP 状态码
- jquery - 使用 Bootbox Confirm,在哪里放置 preventDefault?
- excel - MS Access 查询到 Excel,“参数太少。应为 1”
- c# - 如何获取拆分的剩余内容?
- nativescript - 使用 Nativescript 在外部浏览器中打开某些链接
- sql - 从两个id不相同的表中获取用户