score:2

on chart.js v2.0 you have this feature now inside. see https://www.chartjs.org/samples/latest/charts/area/line-datasets.html

score:5

setting fill property to +1 of a dataset will set the backgroundcolor from this line to the next line in dataset.

datasets: [{
        label: 'systolic guideline',
        data: [],
        fill: '+1',
        bordercolor: '#ffc108',
        backgroundcolor: 'rgba(255,193,8,0.2)'
      },
      {
        label: 'diastolic guideline',
        data: [],
        fill: true,
        bordercolor: '#ffc108',
        backgroundcolor: 'rgba(0,0,0,0)'
      }]

score:18

here is a solution that uses a plugin to fill between two datasets. supports all line styles and fill shading between multiple lines. to fill between a dataset, use the custom param fillbetweenset to tell a dataset to fill the area between another dataset.

fiddle - https://jsfiddle.net/ke5n5lnl/26/

preview:

enter image description here

code:

<html>
    <div>
        <canvas id="demo"></canvas>
    </div>
</html>

<script>
    var fillbetweenlinesplugin = {
    afterdatasetsdraw: function (chart) {
        var ctx = chart.chart.ctx;
        var xaxis = chart.scales['x-axis-0'];
        var yaxis = chart.scales['y-axis-0'];
        var datasets = chart.data.datasets;
        ctx.save();

        for (var d = 0; d < datasets.length; d++) {
            var dataset = datasets[d];
            if (dataset.fillbetweenset == undefined) {
                continue;
            }

            // get meta for both data sets
            var meta1 = chart.getdatasetmeta(d);
            var meta2 = chart.getdatasetmeta(dataset.fillbetweenset);

            // do not draw fill if one of the datasets is hidden
            if (meta1.hidden || meta2.hidden) continue;

            // create fill areas in pairs
            for (var p = 0; p < meta1.data.length-1;p++) {
                // if null skip
              if (dataset.data[p] == null || dataset.data[p+1] == null) continue;

              ctx.beginpath();

              // trace line 1
              var curr = meta1.data[p];
              var next = meta1.data[p+1];
              ctx.moveto(curr._view.x, curr._view.y);
              ctx.lineto(curr._view.x, curr._view.y);
              if (curr._view.steppedline === true) {
                ctx.lineto(next._view.x, curr._view.y);
                ctx.lineto(next._view.x, next._view.y);
              }
              else if (next._view.tension === 0) {
                ctx.lineto(next._view.x, next._view.y);
              }
              else {
                  ctx.beziercurveto(
                    curr._view.controlpointnextx,
                    curr._view.controlpointnexty,
                    next._view.controlpointpreviousx,
                    next._view.controlpointpreviousy,
                    next._view.x,
                    next._view.y
                  );
                            }

              // connect dataset1 to dataset2
              var curr = meta2.data[p+1];
              var next = meta2.data[p];
              ctx.lineto(curr._view.x, curr._view.y);

              // trace backwords set2 to complete the box
              if (curr._view.steppedline === true) {
                ctx.lineto(curr._view.x, next._view.y);
                ctx.lineto(next._view.x, next._view.y);
              }
              else if (next._view.tension === 0) {
                ctx.lineto(next._view.x, next._view.y);
              }
              else {
                // reverse bezier
                ctx.beziercurveto(
                  curr._view.controlpointpreviousx,
                  curr._view.controlpointpreviousy,
                  next._view.controlpointnextx,
                  next._view.controlpointnexty,
                  next._view.x,
                  next._view.y
                );
              }

                            // close the loop and fill with shading
              ctx.closepath();
              ctx.fillstyle = dataset.fillbetweencolor || "rgba(0,0,0,0.1)";
              ctx.fill();
            } // end for p loop
        }
    } // end afterdatasetsdraw
}; // end fillbetweenlinesplugin

chart.pluginservice.register(fillbetweenlinesplugin);

var chartdata = {
    labels: [1, 2, 3, 4, 5,6,7,8],
    datasets: [
      {
          label: "set 1",
          data: [10, 20, null, 40, 30,null,20,40],
          bordercolor: "#f00",
          fill: false,
          steppedline: false,
          tension: 0,
          fillbetweenset: 1,
          fillbetweencolor: "rgba(255,0,0, 0.2)"
      },
      {
          label: "set 2",
          data: [60, 40, 10, 50, 60,null,50,20],
          bordercolor: "#00f",
          fill: false,
          steppedline: false,
          tension: 0.5
      },
      {
          label: "set 2",
          data: [40, 50, 30, 30, 20,null,60,40],
          bordercolor: "#0d0",
          fill: false,
          steppedline: false,
          tension: 0,
          fillbetweenset: 1,
          fillbetweencolor: "rgba(5,5,255, 0.2)"
      }
    ]
};

var chartoptions = {
    responsive: true,
    title: {
        display: true,
        text: 'demo fill between lines'
    }
};

var chartdemo = new chart($('#demo').get(0), {
    type: 'line',
    data: chartdata,
    options: chartoptions
});
</script>

Related Query

More Query from same tag