Search code examples
swiftios-charts

LineChart avoid plotting of 0 values


I am using https://github.com/danielgindi/Charts library. I am trying to remove 0 values from the chart but not able to find a particular solution any help would be appreciated. yAxis has the value of type Double The chart currently displays:

enter image description here

The updated line chart as I want

enter image description here

 var dataEntries: [ChartDataEntry] = []
        
 for i in 0..<forX.count {
            
   let dataEntry = ChartDataEntry(x: Double(i), y: forY[i])
   print(dataEntry)
   dataEntries.append(dataEntry)
           
}
        
let lineChartDataSet = LineChartDataSet(entries: dataEntries, label: "")
      
let lineChartData = LineChartData(dataSet: lineChartDataSet)
chart.data = lineChartData

Solution

  • Just don't add it to your dataEntries

    for i in 0..<forX.count {
       if forY[i] != 0 {
           let dataEntry = ChartDataEntry(x: Double(i), y: forY[i])
           print(dataEntry)
           dataEntries.append(dataEntry)
       }
    }
    

    edit: you can check this https://github.com/aiwiguna/ExampleCharts