Harsha
Harsha

Reputation: 818

How to display a custom text instead of value on X-Axis in Charts

I'm using LineChart from Danielgindi/Charts. I have a requirement to display custom text instead of values on x-axis.

With my implementation, I got this

My Implementation

But, I need it as

enter image description here

If you observe in the second image, there is text (WEEK 1, WEEK 2, WEEK 3, WEEK 4) on x-axis instead of values (0.0, 0.8, 1.6, 2.4) as in first image.

Let me know, if there is any option to show the text instead of value on x-axis.

Upvotes: 3

Views: 714

Answers (1)

Laur
Laur

Reputation: 81

You have to create a class which implements AxisValueFormatter protocol.

The only function you have to implement is func stringForValue(_ value: Double, axis: AxisBase?) -> String

  class SomeClass: AxisValueFormatter  {
    
    func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        return weeks[Int(value)]
    }
}

var weeks = ["WEEK1", "WEEK2", "WEEK3", "WEEK4"]

 chart.xAxis.valueFormatter = SomeClass()

Upvotes: 2

Related Questions