Atif Shabeer
Atif Shabeer

Reputation: 167

iOS Charts not showing all xAxis Value

I am writing an iOS App in Swift 4.2 I am using Charts library to display Horizontal Bar Chart. I need to set its xAxis Values. Only last value is being displayed.

Issue Screenshot: enter image description here

Code Snippet:

@IBOutlet var chartView: HorizontalBarChartView!

override func viewDidLoad() {
        super.viewDidLoad()

        let xAxis = chartView.xAxis
        xAxis.labelPosition = .bottom
        xAxis.labelFont = .systemFont(ofSize: 10)
        xAxis.drawAxisLineEnabled = false
        xAxis.drawGridLinesEnabled = false
        xAxis.granularity = 1
        xAxis.enabled=true

    xAxis.setLabelCount(3, force: false) //Not working as per expectations
   xAxis.valueFormatter=IndexAxisValueFormatter(values: ["A","B","C"])  //Not working as per expectations




        self.setDataCount(3,values:[142000,122400,100110])
        chartView.animate(yAxisDuration: 2.5)
    }


    func setDataCount(_ count: Int, values:[Double]) {
        let barWidth = 2.0
        let spaceForBar = 4.0
        var n=0
        let yVals = (0..<count).map { (i) -> BarChartDataEntry in

            let val = values[n]
            n+=1

            return BarChartDataEntry(x: Double(i)*spaceForBar, y: val)

        }

        let set1 = BarChartDataSet(entries: yVals, label: "DataSet")
        set1.drawIconsEnabled = false

        let data = BarChartData(dataSet: set1)
        data.setValueFont(UIFont(name:"HelveticaNeue-Light", size:10)!)
        data.barWidth = barWidth

        chartView.data = data

        //let xAxis = chartView.xAxis
        //xAxis.setLabelCount(3, force: false)
        //xAxis.valueFormatter=IndexAxisValueFormatter(values: ["A","B","C"])
    }

I tried to find solutions:

xAxis.valueFormatter=IndexAxisValueFormatter(values: ["A","B","C"]) 

but these are not working.

Upvotes: 3

Views: 4664

Answers (2)

Viral Savaliya
Viral Savaliya

Reputation: 180

I updated your given code please check changes.

I hope its work for you.

class ViewController: UIViewController {

@IBOutlet var chartView: HorizontalBarChartView!

override func viewDidLoad() {
    super.viewDidLoad()

    let xAxis = chartView.xAxis
    xAxis.labelPosition = .bottom
    xAxis.labelFont = .systemFont(ofSize: 10)
    xAxis.drawAxisLineEnabled = false
    xAxis.drawGridLinesEnabled = false
    xAxis.granularity = 1
    xAxis.enabled=true

    xAxis.setLabelCount(3, force: false) //Not working as per expectations
    xAxis.valueFormatter = IndexAxisValueFormatter(values: ["A","B","C"])//AxisValueFormatter(values: ["A","B","C"])  //Not working as per expectations

    self.setDataCount(3,values:[142000,122400,100110])
    chartView.animate(yAxisDuration: 2.5)
}


func setDataCount(_ count: Int, values:[Double]) {

    let yVals = (0..<count).map { (i) -> BarChartDataEntry in

        let val = values[i]

        return BarChartDataEntry(x: Double(i), y: val)
    }

    let set1 = BarChartDataSet(values: yVals, label: "DataSet")
    set1.drawIconsEnabled = false

    let data = BarChartData(dataSet: set1)

    data.setValueFont(UIFont(name:"HelveticaNeue-Light", size:10)!)
    //data.barWidth = barWidth

    chartView.data = data

    //let xAxis = chartView.xAxis
    //xAxis.setLabelCount(3, force: false)
    //xAxis.valueFormatter=IndexAxisValueFormatter(values: ["A","B","C"])
}

}

Upvotes: 1

Razi Tiwana
Razi Tiwana

Reputation: 1435

This is happening because the default functionality of IndexAxisValueFormatter

You have to code your own custom IAxisValueFormatter like this

final class CustomFormatter: IAxisValueFormatter {

    var labels: [String] = []

    func stringForValue(_ value: Double, axis: AxisBase?) -> String {

        let count = self.labels.count

        guard let axis = axis, count > 0 else {
            return ""
        }

        let factor = axis.axisMaximum / Double(count)

        let index = Int((value / factor).rounded())

        if index >= 0 && index < count {
            return self.labels[index]
        }

        return ""
    }
}

Then use it like

 let customFormater = CustomFormater()
 customFormater.labels = ["A","B","C"]

 chartView.xAxis.valueFormatter = customFormater

Make sure the size/count of the data and labels is the same or it wont give you the desired results.

Upvotes: 4

Related Questions