ChartJs - set background color of the space between ticks
Asked Answered
B

1

0

I need to set the even/odd space between the ticks background color just like in the following image;

enter image description here

See the "Interlaced Colors" background color. I need to do this for both vertical and horizontal graphs.

See for example: https://canvasjs.com/docs/charts/chart-options/axisY/interlacedcolor/

Any idea on how can I implement something similar in chart js?

I also found an almost good solution here: http://jsfiddle.net/e7oy6yk6/2

1) HTML code:

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

2) Javascript code:

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

Thanks.

Biographical answered 16/7, 2018 at 9:34 Comment(0)
M
2

You need to adapt the example you based yourself on to use the height instead of the width.

I simply changed the properties and the coordinates to draw the rectangles and it works.

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);
Misappropriate answered 16/7, 2018 at 13:52 Comment(4)
Your solution is not exactly what I need. I want to be able to set background for each odd/even lines. On Y scale between 0 and 10 will be a color (color a), between 10 and 20 will be a different color (color b), between 20 and 30 will be again color a, between 30 and 40 will be color b and so on.Biographical
I updated my answer, check the logic behind, it's fairly easy to reproduce and to adapt to the set of data you might have.Misappropriate
I added a solution for vertical lines also. Please check: jsfiddle.net/e7oy6yk6/175Biographical
Indeed, nice one!Misappropriate

© 2022 - 2024 — McMap. All rights reserved.