ChartJs - 刻度之间的空间设置背景颜色(ChartJs - set background

2019-10-28 20:51发布

我需要设置蜱背景颜色之间的偶/奇空间就像下面的图像中;

请参阅“隔行颜色”背景颜色。 我需要为垂直和水平的图形做到这一点。

例如见: https://canvasjs.com/docs/charts/chart-options/axisY/interlacedcolor/

我如何能实现图表JS类似的任何想法?

我还发现了一个几乎是很好的解决方案在这里: http://jsfiddle.net/e7oy6yk6/2

1)的HTML代码:

<canvas id="myChart"></canvas>

2)Javascript代码:

    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;

      var columnCount = chart.data.datasets[0].data.length;
      var width = chartArea.right - chartArea.left;
      var height = chartArea.bottom - chartArea.top

                var columnWidth = width/columnCount;
                ctx.save();
                ctx.fillStyle = chart.config.options.chartArea.backgroundColor;
      var startPoint = chartArea.left
      for (var i = 1; i < width/2; i++) {
        ctx.fillRect(startPoint, chartArea.top, columnWidth, height);
        startPoint += columnWidth * 2;
      }
                ctx.restore();
            }
        }
    });

    var config = {
        type: 'line',
        data: {
            labels: ["January", "February", "March", "April", "May", "June", "July"],
            datasets: [{
                label: "My First dataset",
                data: [65, 0, 80, 81, 56, 85, 40],
                fill: false
            }]
        },
        options: {
            chartArea: {
                backgroundColor: 'rgba(251, 85, 85, 0.4)'
            }
        }
    };

    var ctx = document.getElementById("myChart").getContext("2d");
    new Chart(ctx, config);

谢谢。

Answer 1:

您需要调整您根据自己的使用高度,而不是宽度的例子。

我只是改变了性质,并绘制矩形的坐标和它的作品。

http://jsfiddle.net/e7oy6yk6/170/

    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;

          var values = chart.data.datasets[0].data; // Added
          // var columnCount = chart.data.datasets[0].data.length;
          var rowCount = Math.ceil(Math.max.apply(null, values) / 10); // Replace by the number of rows you need

          var width = chartArea.right - chartArea.left;
          var height = chartArea.bottom - chartArea.top

          // var columnWidth = width/columnCount;
          var rowHeight = height / rowCount; // Added
          ctx.save();
          ctx.fillStyle = chart.config.options.chartArea.backgroundColor;
          var startPoint = chartArea.top // Changed
          while (startPoint < chartArea.bottom) { // Changed
            ctx.fillRect(chartArea.left, startPoint, width, rowHeight); // Changed order
            startPoint += rowHeight * 2; // Changed
          }
          ctx.restore();
        }
      }
    });

    var config = {
      type: 'line',
      data: {
        labels: ["January", "February", "March", "April", "May", "June", "July"],
        datasets: [{
          label: "My First dataset",
          data: [65, 0, 80, 81, 56, 85, 40],
          fill: false
        }]
      },
      options: {
        chartArea: {
          backgroundColor: 'rgba(251, 85, 85, 0.4)'
        }
      }
    };

    var ctx = document.getElementById("myChart").getContext("2d");
    new Chart(ctx, config);


文章来源: ChartJs - set background color of the space between ticks