iosswiftios-chartsswiftcharts

LineChart with optional Y parameter swift


Require a LineChart where in it allows y value as nil. As when no data for that particular day(x) is there the line should just go forward.

Currently I have researched on SwiftChart & Charts but both doesn't provide that functionality.

Please refer below image for more understanding.

enter image description here

As you can see Sunday, Monday & Wednesday there is data Tuesday there is no data so the line just continues from Monday to Wednesday.

How can I achieve this or if there is any library that can help me achieve this.


Solution

  • You can just skip some X value in https://github.com/danielgindi/Charts

    enter image description here

    private func setupChart() {
            let leftAxis = chart.leftAxis
            leftAxis.axisMinimum = 0
            leftAxis.axisMaximum = 100
            leftAxis.granularity = 10
            
            let rightAxis = chart.rightAxis
            rightAxis.enabled = false
            
            let xAxis = chart.xAxis
            xAxis.axisMinimum = 0
            xAxis.axisMaximum = 7
            xAxis.labelPosition = .bottom
            xAxis.granularity = 1
            
        }
        
        private func setupChartData() {
            var dataEntries: [ChartDataEntry] = []
            for i in 0...7 {
                if i % 2 == 0 {
                    let value = arc4random_uniform(100) + 1
                    if value != 0 {
                        let dataEntry = ChartDataEntry(x: Double(i), y: Double(value))
                        dataEntries.append(dataEntry)
                    }
                }
            }
            let dataSet = LineChartDataSet(entries: dataEntries, label: "")
            let data = LineChartData(dataSet: dataSet)
            chart.data = data
        }