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:
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>
Source: stackoverflow.com
Related Query
- Chart JS Fill Between two lines
- Chart.js: Fill background between two lines
- Fill between two lines Chartjs
- How can I make two of my lines in Chart JS thicker
- Filling area between two lines - Chart.js v2
- Find intersection between the chart lines in chartjs
- How to add background color between two lines in yAxis Chartjs
- How to add background color between two specific lines in Chartjs 3.1
- change space between horizontal (grid) lines for line chart
- Mixed Chart calculating difference between two bars - ChartJS
- Is it possible to fill particular portion between given start & end angle in pie chart in chart.js?
- ChartJs: Different color fill between points makes lines straight...need curves (lineTension)
- Chart.js - Increase spacing between legend and chart
- ChartJS New Lines '\n' in X axis Labels or Displaying More Information Around Chart or Tooltip with ChartJS V2
- Chart.js - line chart with two yAxis: "TypeError: yScale is undefined"
- chart js how to fill legend box with colour
- Chartjs Line Color Between Two Points
- How to display data labels outside in pie chart with lines in ionic
- Center point labels between ticks (Polar Area Chart JS)
- Fill Chart.js bar chart with diagonal stripes or other patterns
- Chart.js - mixing bar and line graphs - can I get the lines to fill the full column?
- How to create a gradient fill line chart in latest Chart JS version (3.3.2)?
- ChartJS Radar Chart radar lines color?
- Getting chart js bar chart to fill window
- Chart js: Update line chart having two data sets
- Increase padding between legend and chart with react-chartjs-2
- Chart,js Pie Chart can the gap between a pie chart and the legend be adjusted
- Chart.js :set yAxis point to 0 when there is gap between two dates
- ChartJs: Different Fill Colour Between Data Points
- two xAxes horizontal bar chart Charts.js
More Query from same tag
- php/laravel: Creating chartdata, return 0 if no data is being submitted to database
- Trouble setting options for radar chart on Quickchart.io
- Trouble using utcoffset with Chart.js
- Chart js label issue in react js
- Iterating through array in Chart.js data field
- Updating chart.js chart with dataset of different size
- Adding a Date adapter for Time Cartesian axis from a cdn
- ChartKick - ChartJs - Legend onClick override with default behaviour
- How to stop resizing chart.js in case hovered and out of boundaries?
- Vuechart js not rendering on a component
- Why isn't my Angular 4 ChartJS component rendering?
- Chartjs sample line chart setup doesn't show dataLabels
- ChartJS Only Show Large Font Size for a Specific Tick
- Charts.js how to fix the height with more than 50 horizontal bars
- How to make dashed thick lines with dots in ChartJS ? Is it possible?
- ChartDataLabels is globally active for some reason
- Chart.js Multiple dataset
- diplay barchart in a <div>
- ChartJS: Remove padding on first and last bars on Barchart
- Is it able to align line chart at left margin in mixed chart?
- Need help splitting date to new line in Doughnut Chart js
- How to point a Chart.js plugin to different doughnut charts?
- Chart.js not displaying when including variable
- what is wrong with my code ? java script chart position
- Chart.JS: How can I only display data labels when the bar width is big enough for the text?
- How to install chart.js
- Chart.js not showing up on online site
- ChartJS Y-Axis scale value wrong
- Chart.js scatter charts: Is there a way to indicate that datapoints of two data sets are on top of each other?
- Angular ChartJs does not show Data for multiple Charts