Skip to content Skip to sidebar Skip to footer

How To Add Background Color Between Two Lines In Yaxis Chartjs

How i can add background color between two lines in Y-Axis in chartjs if you checked the figure i need to be able to do something like this set a color between two lines first on

Solution 1:

You have to register your own plugin to fill chartArea before chart line drawing:

Chart.pluginService.register({
  beforeDraw: function (chart, easing) {
    if (chart.config.options.fillColor) {
      var ctx = chart.chart.ctx;
      var chartArea = chart.chartArea;
      ctx.save();
      ctx.fillStyle = chart.config.options.fillColor;
      ctx.fillRect(chartArea.left, chartArea.top, chartArea.right - chartArea.left, chartArea.bottom - chartArea.top);
      ctx.restore();
    }
  }
});

var chartData = {
  labels: ['a', 'b', 'c', 'd'],
  datasets: [{
    label: 'value',
    backgroundColor: 'rgba(255, 0, 255, 0.8)',
    borderColor: 'blue',
    data: [30, 50, 25, 10]
  }]
};

var ctx = document.getElementById("myChart").getContext("2d");
var myBar = newChart(ctx, {
  type: 'line',
  data: chartData,
  options: {
  	scales: {
    	yAxes: [{ ticks: { max: 60 } }]
    },
  	legend: { display: false },
		fillColor: 'rgba(255, 128, 0, 0.8)',
  }
});
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"></script><canvasid="myChart"height="300"width="500"></canvas>

Post a Comment for "How To Add Background Color Between Two Lines In Yaxis Chartjs"