Skip to content Skip to sidebar Skip to footer

Chart.js - Color Specific Parts Of The Background In A Line Chart

I have a line chart much like this one: http://www.chartjs.org/samples/latest/charts/line/basic.html I would like to color the areas -100 < y < -40 and 40 < y < 100 a s

Solution 1:

You can use annotation plugin and in particular Box annotations.

Here below there is an example with Scatter chart:

var randomScalingFactor = function() {
  return (Math.random() > 0.5 ? 1.0 : -1.0) * Math.round(Math.random() * 100);
};
var randomColor = function(opacity) {
  return'rgba(' + Math.round(Math.random() * 255) + ',' + Math.round(Math.random() * 255) + ',' + Math.round(Math.random() * 255) + ',' + (opacity || '.3') + ')';
};
var data = {
  datasets: [{
    label: "My First dataset",
    data: [{
      x: randomScalingFactor(),
      y: randomScalingFactor(),
    }, {
      x: randomScalingFactor(),
      y: randomScalingFactor(),
    }, {
      x: randomScalingFactor(),
      y: randomScalingFactor(),
    }, {
      x: randomScalingFactor(),
      y: randomScalingFactor(),
    }, {
      x: randomScalingFactor(),
      y: randomScalingFactor(),
    }, {
      x: randomScalingFactor(),
      y: randomScalingFactor(),
    }, {
      x: randomScalingFactor(),
      y: randomScalingFactor(),
    }]
  }, {
    label: "My Second dataset",
    data: [{
      x: randomScalingFactor(),
      y: randomScalingFactor(),
    }, {
      x: randomScalingFactor(),
      y: randomScalingFactor(),
    }, {
      x: randomScalingFactor(),
      y: randomScalingFactor(),
    }, {
      x: randomScalingFactor(),
      y: randomScalingFactor(),
    }, {
      x: randomScalingFactor(),
      y: randomScalingFactor(),
    }, {
      x: randomScalingFactor(),
      y: randomScalingFactor(),
    }, {
      x: randomScalingFactor(),
      y: randomScalingFactor(),
    }]
  }]
};
data.datasets.forEach(function(dataset) {
  dataset.borderColor = randomColor(0.4);
  dataset.backgroundColor = randomColor(0.1);
  dataset.pointBorderColor = randomColor(0.7);
  dataset.pointBackgroundColor = randomColor(0.5);
  dataset.pointBorderWidth = 1;
});

var ctx = document.getElementById("canvas").getContext("2d");
window.myScatter = newChart(ctx, {
	type: 'scatter',
  data: data,
  options: {
    scales: {
      xAxes: [{
        position: 'bottom',
        gridLines: {
          zeroLineColor: "rgba(0,255,0,1)"
        },
        scaleLabel: {
          display: true,
          labelString: 'x axis'
        },
      }],
      yAxes: [{
        position: 'left',
        gridLines: {
          zeroLineColor: "rgba(0,255,0,1)"
        },
        scaleLabel: {
          display: true,
          labelString: 'y axis'
        },
        ticks: {
        	min: -100,
          max: 100
        }
      }]
    },
    annotation: {
      drawTime: "afterDraw",
      events: ['dblclick'],
      annotations: [{
      	id: 'low-box',
        type: 'box',
        xScaleID: 'x-axis-1',
        yScaleID: 'y-axis-1',
        xMin: -100,
        xMax: 100,
        yMin: -100,
        yMax: -40,
        backgroundColor: 'rgba(255, 0, 0, 0.3)',
        //borderColor: 'rgb(255, 0, 0)',borderWidth: 1
      },{
      	id: 'hi-box',
        type: 'box',
        xScaleID: 'x-axis-1',
        yScaleID: 'y-axis-1',
        xMin: -100,
        xMax: 100,
        yMin: 100,
        yMax: 40,
        backgroundColor: 'rgba(255, 0, 0, 0.3)',
        //borderColor: 'rgb(255, 0, 0)',borderWidth: 1
      }]
    }
  }
});
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/chartjs-plugin-annotation/0.5.7/chartjs-plugin-annotation.min.js"></script><canvasid="canvas"></canvas>

Post a Comment for "Chart.js - Color Specific Parts Of The Background In A Line Chart"