I want to display String values for X-Axis in line chart via using 'Charts'
I've followed tutorial for same here. As per wrote there
let lineChartData = LineChartData(xVals: dataPoints, dataSet: lineChartDataSet)
I'm not finding in current latest version of library.
In, current library version's demo code I'm only able to fill double
type of data for X-axis and Y-axis.
Please help me to solve this.
Here is desired output.
Asked
Active
Viewed 2,209 times
5

Mrugesh Tank
- 3,495
- 2
- 29
- 59
-
Check this one https://stackoverflow.com/a/51612972/10150796 – Nikunj Kumbhani Mar 18 '19 at 06:48
-
That is available for barChart, but I want to draw line chart. So constructor is different. – Mrugesh Tank Mar 18 '19 at 06:50
-
It's not good to use String as X & Y values. You have to convert the string to double and use it. – Satyam Mar 18 '19 at 06:55
-
@Satyam, I want to display "Jan", "Feb", "Mar" on X-axis as displayed in image. – Mrugesh Tank Mar 18 '19 at 06:58
-
@MrugeshTank You can do the same thing for any type of chart. – Nikunj Kumbhani Mar 18 '19 at 06:59
-
2I used it long time back. There's a protocol IAxisValueFormatter that you can implement and achieve your result. – Satyam Mar 18 '19 at 07:00
-
@Satyam, You saved my day. You can post this and some example as answer, I'll accept – Mrugesh Tank Mar 18 '19 at 07:16
1 Answers
4
There's a protocol IAxisValueFormatter that you can implement and achieve your expected result.
EDIT:
How to use it
While initialising
chartView.xAxis.valueFormatter = self
and implementing protocol.
extension LineChart1ViewController: IAxisValueFormatter {
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
let months = ["January","February","March","April","May","June","July","Auguest","September","October","November","December"]
return months[Int(value)]
}
}

Mrugesh Tank
- 3,495
- 2
- 29
- 59

Satyam
- 15,493
- 31
- 131
- 244