[Chartjs]-Creating a diagonal shaded area in chart.js

1๐Ÿ‘

โœ…

If you make sure your last points fall outside of the chartarea so the line contiues to keep going it will fill correclty:

const options = {
  type: 'line',
  data: {
    datasets: [{
      label: '# of Votes',
      data: [{
        x: 0,
        y: 20
      }, {
        x: 30,
        y: 0
      }],
    }, {
      label: '# of Points',
      fill: '-1',
      data: [{
        x: 0,
        y: 15
      }, {
        x: 25,
        y: 0
      }],
    }]
  },
  options: {
    scales: {
      x: {
        type: 'linear',
        min: 10,
        max: 20
      },
      y: {
        min: 0,
        max: 20
      }
    }
  }
}

const ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.0/chart.js"></script>

<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
</body>

Leave a comment