Search code examples
swiftios-charts

Chart viewport height increase when chart has no data plot on chart


I am using https://github.com/danielgindi/Charts library. When the chart has no data then chart height increased it should be the same as when the chart has data. Chart xAxis labels overlapped with char title. Any help would be appreciated. Code for chart setup

var dataEntries: [ChartDataEntry] = []

        axisFormatDelegate = self
        chartView.legend.form = .none
        
        let rightAxis = chartView.rightAxis
        rightAxis.enabled = false
        
        let yAxis = chartView.leftAxis
       
        let xAxisValue = chartView.xAxis
        xAxisValue.valueFormatter = axisFormatDelegate
        
        xAxisValue.axisMinimum = -1
        xAxisValue.axisMaximum = Double(forX.count)
        xAxisValue.granularity = 1
        
        for i in 0..<forX.count {
            
            if forY[i] != 0 {
                
                let dataEntry = ChartDataEntry(x: Double(i), y: forY[i])
                dataEntries.append(dataEntry)
            }
            
        }
        

        let lineChartDataSet = LineChartDataSet(entries: dataEntries, label: "")
        let lineChartData = LineChartData(dataSet: lineChartDataSet)
        print("Line chart data: \(lineChartData.dataSets)")
        chartView.data = lineChartData
        

The chart with data:

enter image description here

The chart with no data:

enter image description here


Solution

  • I think it because you set legend.form to .none, it not showing legend but still had space for it

    I usually use this to hide legend and to give extra margin in bottom of xAxis label use

    chartView.legend.enabled = false
    chartView.extraBottomOffset = 10