Home Forums Chart Support Interpolate the missing data as last value in Multiseries chart Reply To: Interpolate the missing data as last value in Multiseries chart

#36782

In order to add missing dataPoint on each series, you can loop through each dataSeries and find the missing dataPoints and push into respective dataSeries as shown in the code snippet below.

``````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)};
}``````

Please take a look at this JSFiddle for complete code.

—-
Manoj Mohan
Team CanvasJS