1

I am using this code for config left axis:

let leftAxis = chartView.leftAxis
leftAxis.removeAllLimitLines()
leftAxis.gridLineDashLengths = [5, 5]
leftAxis.drawLimitLinesBehindDataEnabled = true
leftAxis.labelTextColor = .white
leftAxis.axisMinimum = 0
leftAxis.drawLabelsEnabled = true
let array = ["a","b","c","e","f","g","h"]
leftAxis.valueFormatter = IndexAxisValueFormatter(values: array)
leftAxis.granularity = 1

Why not show my labels in left axis iOS Swift?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
reza_khalafi
  • 6,230
  • 7
  • 56
  • 82

1 Answers1

6

IndexAxisValueFormatter is used for passing an array of x-axis labels, not y-axes labels. You need to create your own y-axis values formatter by implementing IAxisValueFormatter protocol.

E.g, I want to display custom string labels for some values on my y-Axis. I have created MyLeftAxisFormatter class.

class MyLeftAxisFormatter: NSObject, IAxisValueFormatter {

    var labels: [Int : String] = [ 0 : "zero", 3 : "three", 6 : "six", 9 : "nine", 12 : "twelve", 15 : "fifteen", 18 : "eighteen"]

    func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        return labels[Int((value).rounded())] ?? ""
    }
}

And use it for left axis on my chart.

lineChartView.leftAxis.valueFormatter = MyLeftAxisFormatter()

Y-Axis Labels

Keep in your mind, that displayed values for y-axis are calculated by charts engine in depending on various factors such as view's height, min/max values, etc. And this values can change when you will change source data.

AlexSmet
  • 2,141
  • 1
  • 13
  • 18