[Chartjs]-How to add conditional fill color in Chart.js area chart?

1๐Ÿ‘

โœ…

I was able to make some progress on the design that I wanted, so thought of sharing my answer to benefit others. I was able to fill based on x-axis data values and get the following chart:

I had to use the segment property inside data configs to achieve this, with the help of a function. This is the modified code:

const highlightRegion = (ctx, value) => {

    if (ctx.p0DataIndex > boundary_val1 && ctx.p0DataIndex < boundary_val2) {
        return "#B4EDB3";
    }
    return "#E5E5E5";
};

const bgColor = ctx => highlightRegion(ctx, "#B4EDB3");

const data = {
    labels: x_values,
    datasets: [
        {
            label: 'Count',
            data: [0, 20, 40, 80, 150, 80, 30, 0],
            pointRadius: 0,
            fill: true,
            tension: 0.4,
            segment: {
                backgroundColor: bgColor,
                borderColor: bgColor,
            },
        },
    ],
};

Special thanks goes to this youtube series from where I was able to find my answer: https://www.youtube.com/watch?v=st2O-pvhWM4.

I will keep this post open, in case if there is a better solution as I think my solution is not absolutely correct.

0๐Ÿ‘

You can use multiple datasets with object data so you can specify starting and end points. After this you can manipulate the legend so it looks like its only a single dataset:

var options = {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
        label: 'Blood Sugar Measure extra',
        data: [{
          x: "Red",
          y: 5
        }, {
          x: "Blue",
          y: 8
        }],
        backgroundColor: 'lightGray',
        fill: true
      },
      {
        label: 'Blood Sugar Measure',
        data: [{
          x: "Blue",
          y: 8
        }, {
          x: "Yellow",
          y: 12
        }, {
          x: "Green",
          y: 10
        }],
        backgroundColor: 'pink',
        fill: true
      },
      {
        label: 'Blood Sugar Measure extra',
        data: [{
          x: "Green",
          y: 10
        }, {
          x: "Purple",
          y: 12
        }, {
          x: "Orange",
          y: 10
        }],
        backgroundColor: 'lightGray',
        fill: true
      }
    ]
  },
  options: {
    plugins: {
      legend: {
        onClick: (e, legendItem, legend) => {
          const ci = legend.chart;
          const currentlyHidden = ci.getDatasetMeta(0).hidden;

          for (let i = 0; i < ci.data.datasets.length; i++) {
            ci.setDatasetVisibility(i, currentlyHidden)
          }

          ci.update();
        },
        labels: {
          filter: (e) => (!e.text.includes('extra'))
        }
      }
    }
  }
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.1/chart.js"></script>
</body>

Leave a comment