score:1
Accepted answer
This should do the trick.
And Here is a fiddle of a plugin that will fill between any two datasets.
https://jsfiddle.net/ke5n5LnL/26/
Code:
<html>
<div>
<canvas id="myChart"></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);
ctx.beginPath();
// vars for tracing
var curr, prev;
// trace set1 line
for (var i = 0; i < meta1.data.length; i++) {
curr = meta1.data[i];
if (i === 0) {
ctx.moveTo(curr._view.x, curr._view.y);
ctx.lineTo(curr._view.x, curr._view.y);
prev = curr;
continue;
}
if (curr._view.steppedLine === true) {
ctx.lineTo(curr._view.x, prev._view.y);
ctx.lineTo(curr._view.x, curr._view.y);
prev = curr;
continue;
}
if (curr._view.tension === 0) {
ctx.lineTo(curr._view.x, curr._view.y);
prev = curr;
continue;
}
ctx.bezierCurveTo(
prev._view.controlPointNextX,
prev._view.controlPointNextY,
curr._view.controlPointPreviousX,
curr._view.controlPointPreviousY,
curr._view.x,
curr._view.y
);
prev = curr;
}
// connect set1 to set2 then BACKWORDS trace set2 line
for (var i = meta2.data.length - 1; i >= 0; i--) {
curr = meta2.data[i];
if (i === meta2.data.length - 1) {
ctx.lineTo(curr._view.x, curr._view.y);
prev = curr;
continue;
}
if (curr._view.steppedLine === true) {
ctx.lineTo(prev._view.x, curr._view.y);
ctx.lineTo(curr._view.x, curr._view.y);
prev = curr;
continue;
}
if (curr._view.tension === 0) {
ctx.lineTo(curr._view.x, curr._view.y);
prev = curr;
continue;
}
// reverse bezier
ctx.bezierCurveTo(
prev._view.controlPointPreviousX,
prev._view.controlPointPreviousY,
curr._view.controlPointNextX,
curr._view.controlPointNextY,
curr._view.x,
curr._view.y
);
prev = curr;
}
ctx.closePath();
ctx.fillStyle = dataset.fillBetweenColor || "rgba(0,0,0,0.1)";
ctx.fill();
}
} // end afterDatasetsDraw
}; // end fillBetweenLinesPlugin
Chart.pluginService.register(fillBetweenLinesPlugin);
function GenGraph(y) {
// function to generate the graph with chart.js
$(document).ready(function(){
var roomCap = 220
var prediction = [62, 65, 135, 145, 140, 120, 135, 189, 180, 175, 100, 25]
var gndTruthUpper = [75, 100, 150, 175, 150, 150, 175, 200, 175, 150, 125, 100]
var gndTruthLower = [50, 50, 75, 50, 25, 50, 75, 100, 125, 150, 125, 100, 75]
var data = {
labels: ["00", "05", "10", "15", "20", "25", "30", "35", "40", "45", "50", "55"],
datasets: [{
label: 'prediction',
fill: false,
pointRadius: 0,
borderColor: 'blue',
data: prediction
},{
label: 'GTUpper',
fill: false,
pointRadius: 0,
borderDash: [10, 10],
borderColor: 'black',
data: gndTruthUpper,
fillBetweenSet: 2,
fillBetweenColor: 'rgba(0,0,0,0.1)'
},{
label: 'GTLower',
fill: false,
pointRadius: 0,
borderDash: [10, 10],
borderColor: 'black',
data: gndTruthLower
}]
};
var options = {
scales: {
yAxes: [{
ticks: {
min: 0,
max: roomCap
}
}]
}
};
var ctx = document.getElementById("myChart").getContext("2d");
var myChart = new Chart(ctx, {
type: 'line',
data: data,
options: options
});
});
};
GenGraph();
</script>
Source: stackoverflow.com
Related Query
- How to shade between 2 lines on a line graph using chart.js version2
- How to plot a single value with line in line chart graph using charts.js?
- How to add an on click event to my Line chart using Chart.js
- How to start the line graph from the left Y axis in a line/bar mixed chart (Chart.js)?
- how to draw line graph with line animation from left to right using chartjs?
- How to make aspecific API call from within a Line Chart class in ReactJS using react-chartjs-2?
- change space between horizontal (grid) lines for line chart
- Populating Chart.Js line graph with multiple lines using data from a JSON Object
- How to create a bar and a line in a same graph using chart.js in React?
- how to increase space between legend and chart in chartjs (ng2charts ) using angular
- How to add an end line to my graph using Chart.js?
- Can't Draw Horizontal Line on Graph Using ChartJS Annotation Plugin and Primevue Chart
- Why is my line chart using multiple lines to connect random points? (Chart.js)
- How do you set x and y axis and Title for a line chart using charts.js?
- How to move the x-axis values and line points to the middle of two x-axis lines using chartjs?
- How can I reduce the spacing between my legend and chart proper in my Chart.JS bar chart, and increase it in the graph area?
- How to plot a line chart which has both the start and points outside the canvas area using Chart.js
- How to draw multiple color bars in a bar chart along a Horizontal Line using chart.js
- How to decrease bottom width of triangle using line chart in chartJs?
- How to Draw a line on chart without a plot point using chart.js
- How to edit my custom tooltips in my line chart using chart.js?
- How to fill a graph by a color till a vertical line using chart.js
- How to create a line on a chart using chartjs-plugin-annotation
- How to Add X axis Padding in chart js Line Graph
- how to draw Line chart using chart.js and datalabels should be shown
- How to achieve a mixed chart of 'horizontal' bars and line chart using Chart.js?
- how to create line chart using chart.js in angular 2+
- How to run Chart.js samples using source code
- Line Chart with null values: How to keep Lines connected?
- How to display data from database into line graph using php and mysql?
More Query from same tag
- How to displays item names from the database using a monthly or yearly chart PHP MySQL
- Load ChartJS via VueJS - ChartJS empty and 0px dimension
- ChartJs - Labelling minimum and maximum value of the Y Axis
- Chart.js and Angular 8 - Dynamically updating Chart.js labels and data from *ngfor
- chart.js not allowing y axis steps with logarithmic scale
- How can I hide points on chart?
- Chart.JS Data Element To Be settled as Euro Currency
- text inside the bar - chart.js
- Update Chart.js plugin
- How can I use npm-plugins with react-chartjs-2?
- Display point style on legend in chart.js
- Is this graph possible using chart.js?
- ChartJS changing graphs based upon Drop Down selection
- How to change HTML style in ChartOption with if condition?
- Angular 2 & ng2-charts: (SystemJS) Unexpected directive 'BaseChartComponent' imported
- How can I add background color of length bars in chart (chartJS)?
- Chartjs with Node.js : Unexpected token <
- Display with Month name in the Tooltip
- Charts.js scales yaxes ticks min max doesnt work
- Manipulate chart.js based plots in R (radarchart)
- JS count numbers between
- Why I don't see the second plot?
- Creating and Updating Graph in HTML and Javascript
- How to remove the inside-border from doughnut chart? Chart.js
- Trigger bar/pie graph highlight when hovering over an external element [chart.js / chart js] [AngularJS] [angular-chart.js]
- Starting a Chart.js Time Scale at 0
- Chart Click Event - Clicking label of doughnut chart, doesn't return label ng2-charts
- Highlight background on a single hour with chart.js and annotations
- ChartJS tooltip scrollbar to prevent data overflow?
- Chartjs not reading data from vuex mapped state