JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
<br/><!-- Just so that JSFiddle's Result label doesn't overlap the Chart -->
<div id="chartContainer" style="height: 360px; width: 100%;"></div>
JavaScript
var chart = new CanvasJS.Chart("chartContainer", {
title:{
text: "Multi-Series Line Chart"
},
toolTip: {
shared: true
},
data: [
{
type: "line",
dataPoints: [
{ x: 10, y: 30 },
{ x: 30, y: 40 },
{ x: 40, y: 50 },
{ x: 60, y: 60 },
{ x: 70, y: 90 },
{ x: 80, y: 20 },
{ x: 90, y: 50}
]
}, {
type: "line",
dataPoints: [
{ x: 10, y: 10 },
{ x: 20, y: 20},
{ x: 30, y: 30 },
{ x: 40, y: 40 },
{ x: 50, y: 50 },
{ x: 60, y: 60 },
{ x: 70, y: 70 },
{ x: 90, y: 90}
]
}
]
});
addMissingDataPoints(chart);
chart.render();
function addMissingDataPoints(chart) {
var missingValues = [];
for(var i = 0; i < chart.options.data.length; i++) {
missingValues[i] = [];
var dataSeriesToBeChecked = chart.options.data[i];
for(var j = 0; j < chart.options.data.length; j++) {
if(j == i) continue;
var currentDp = chart.options.data[j].dataPoints;
for(var k = 0; k < currentDp.length; k++) {
var dp = getPreviousDp(currentDp[k].x, dataSeriesToBeChecked.dataPoints);
if(dp === true)
continue;
missingValues[i].push(dp);
}
}
}
for(var i = 0; i < chart.options.data.length; i++) {
for(var j = 0; j < missingValues[i].length; j++) {
// push the missing values
chart.options.data[i].dataPoints.push(missingValues[i][j]);
}
//sorting the dataPoints so that missing values get adjusted to appropriate place
chart.options.data[i].dataPoints.sort((a,b) => (a.x < b.x ? -1 : (a.x > b.x ? 1 : 0)))
}
}
function getPreviousDp(xValue, dps) {
for(var i=0; i<dps.length; i++) {
if(dps[i].x == xValue) {
return true;
}
if(dps[i].x > xValue)
break;
}
return {x: xValue, y: (i <= 0 ? null : dps[i-1].y)};
}