Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 864
Harsha Avatar asked Dec 07 '25 03:12

Harsha


1 Answers

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()
like image 191
Laur Avatar answered Dec 08 '25 16:12

Laur