1

i'am refering to this post:

chart.js Line chart with different background colors for each section

and i can't make it work with chart.js version 2.1.6 (API has changed), any idea ?

Community
  • 1
  • 1
  • Here is one method how to do this https://stackoverflow.com/questions/37144031/background-colour-of-line-charts-in-chart-js/47674241#47674241 – niksi Dec 06 '17 at 12:27

1 Answers1

0

I did this:

Chart.pluginService.register({
  beforeDraw: function (chart, easing) {
    if (chart.chartArea) {
      var helpers = Chart.helpers;
      var ctx = chart.chart.ctx;
      var chartArea = chart.chartArea;
      var chartWidth = chartArea.right - chartArea.left;
      var chartHeight = Math.abs(chartArea.top - chartArea.bottom);
      ctx.save();

      var pattern = document.createElement('canvas');
      pattern.width = 40;
      pattern.height = chartHeight;
      var pctx = pattern.getContext('2d');

      pctx.fillStyle = "#f0f2f5";
      pctx.fillRect(0, 0, 20, chartHeight);
      pctx.fillStyle = "#ffffff";
      pctx.fillRect(20, 0, 20, chartHeight);

      var pattern = ctx.createPattern(pattern, "repeat");
      ctx.fillStyle = pattern;

      ctx.fillRect(chartArea.left, chartArea.top, chartArea.right - chartArea.left, chartArea.bottom - chartArea.top);
      ctx.restore();
    }
  });
user1326293
  • 923
  • 2
  • 9
  • 24