根据Y轴值Chartjs不同的行背景颜色

时间:2018-05-03 07:09:03

标签: javascript chart.js

如何为chartjs背景添加多种颜色?我已经添加了单色但我想要的就像下面的图像 enter image description here

这是我的代码

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();
        }
    }
});

谢谢。

1 个答案:

答案 0 :(得分:0)

这可以通过chartjs-plugin-annotation的框注释功能来实现。 https://github.com/chartjs/chartjs-plugin-annotation