Search code examples
amchartsamcharts4

Is there a way to omit NaN in AmCharts line series without line going through value zero (0)?


Is there a chance to skip/omit the NaN values in AmChart4. The line is not correct as soon as there is some data missing in the dataset. In this case the line goes thought value 0, instead of simply connecting at next known value. Or at least disconnect at point where NaN appears. I tried series.connect = false, but it did not results as I expected.

Reproducible example here

Any help would be appreciated. Thank you in advance.


Solution

  • The only way to skip those values is to remove them from the array. You can use the beforedatavalidated event to remove NaN values before the chart uses them:

    chart.events.on("beforedatavalidated", function(ev) {
      let source = ev.target.data;
      let data = [];
      for(let i = 0; i < source.length; i++) {
        let row = JSON.parse(JSON.stringify(source[i]));
        data.push(row);
        if (isNaN(row.series1)) {
            row.series1 = undefined;
        }
        if (isNaN(row.series2)) {
            row.series2 = undefined;
        }
      }
      ev.target.data = data;
    });
    

    Updated fiddle