javascript - Chart JS — 条件水平行背景色

标签 javascript charts chart.js

我有点难以根据垂​​直轴上的数字向 ChartJS 中的行添加条件背景颜色。

例如。

If the vertical axis is between 0 - 6, background colour for those rows is green.
If the vertical axis is between 6 - 12 background colour for those rows is grey
If the vertical axis is > 12 background colour for those rows is red

有没有人做过这样的事情?

我附上了一张大致描述功能的图片。

enter image description here

干杯!

最佳答案

chartjs 无法做到这一点。但是你可以自己写 plugin并在 beforeDraw 钩子(Hook)中自己绘制背景。

var chart = new Chart(ctx, {
    plugins: [{
        beforeDraw: function(chart) {
            //..
        }
    }]
});

您可以从图表参数中获取所有信息来计算 y 轴段的高度。 我在下面包含了一个片段如何实现。但是请注意,这更多的是概念验证而不是正确的实现:

        var canvas = document.getElementById('myChart');
        window.chartColors = {
            red: 'rgb(255, 99, 132)',
            orange: 'rgb(255, 159, 64)',
            yellow: 'rgb(255, 205, 86)',
            green: 'rgb(51, 204, 51)',
            blue: 'rgb(54, 162, 235)',
            purple: 'rgb(153, 102, 255)',
            grey: 'rgb(201, 203, 207)'
        };

        var myLineChart = new Chart(canvas,
            {
                type: 'line',
                data: {
                    labels: ['1', '2', '3', '4', '5'],
                    datasets: [
                        {
                            label: '# of Votes',
                            fill: false,
                            backgroundColor: window.chartColors.blue,
                            borderColor: window.chartColors.blue,
                            data: [2, 5, 12.5, 9, 6.3]
                        }
                    ]
                },
                options: {
                    responsive: true,
                    title: {
                        display: true,
                        text: 'Conditional Background'
                    },
                    backgroundRules: [{
                        backgroundColor: window.chartColors.green, 
                        yAxisSegement: 6
                    }, {
                        backgroundColor: window.chartColors.grey,
                        yAxisSegement: 12
                    }, {
                        backgroundColor: window.chartColors.red,
                        yAxisSegement: Infinity
                    }],
                    scales: {
                        yAxes: [{
                            ticks: {
                                beginAtZero: true,
                                stepSize: 1
                            }
                        }]
                    }
                },
                plugins: [{
                    beforeDraw: function (chart) {
                        var ctx = chart.chart.ctx;
                        var ruleIndex = 0;
                        var rules = chart.chart.options.backgroundRules;
                        var yaxis = chart.chart.scales["y-axis-0"];
                        var xaxis = chart.chart.scales["x-axis-0"];
                        var partPercentage = 1 / (yaxis.ticksAsNumbers.length - 1);
                        for (var i = yaxis.ticksAsNumbers.length - 1; i > 0; i--) {
                            if (yaxis.ticksAsNumbers[i] < rules[ruleIndex].yAxisSegement) {
                                ctx.fillStyle = rules[ruleIndex].backgroundColor;
                                ctx.fillRect(xaxis.left, yaxis.top + ((i - 1) * (yaxis.height * partPercentage)), xaxis.width, yaxis.height * partPercentage);
                            } else {
                                ruleIndex++;
                                i++;
                            }
                        }
                    }
                }]
            });
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.min.js"></script>
<canvas id="myChart" width="400" height="250"></canvas>

关于javascript - Chart JS — 条件水平行背景色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47784304/

相关文章:

javascript - 在正确的输入上使用 js 重新启用提交按钮

javascript - 我是否正确地以 Base64 编码图像?

javascript - Google Chart 刻度线根本不显示

javascript - 为什么 JavaScript 中的数学方法比按位运算符更快?

javascript - 滚动检查内部div时如何卡住div

javascript - 如何绘制包含多个数据但只有2个标签的折线图?

java - 如何绘制android动画 donut chart

javascript - 相同数据集的数据在chartjs中没有相同的颜色

javascript - 当我将鼠标悬停在 ChartJS 条形图中的条形上时,如何将光标更改为指针?

chart.js - 如何在 chart.js 图表中形成差距?