Search code examples
iosswiftios-charts

How can I add a name for each column on ios-chart?


I am doing a BarChart with ios-chart library. As it is similar to MPAndroidChart I do not have a lot of problems creating them but I am not able to add a name/label above each column with the days of the week.

On Android, after setting the values to the respective BarDataSet I used:

BarData data = new BarData(labels, dataset); 

where labels are the name for each column and dataset is the BarDataSet.

The problem is that on Swift I cannot find that way to add the labels for each column. This is the code that I have by the moment (a simple example):

@IBOutlet weak var barChartView: BarChartView!

var entries = [ChartDataEntry]()
entries.append(BarChartDataEntry(x: 1.0, yValues: [1.0], label: "Monday"))
entries.append(BarChartDataEntry(x: 2.0, yValues: [4.0], label: "Tuesday"))
    
let charDataSet = BarChartDataSet(values: entries, label: "legend")
let charData = BarChartData(dataSets: [charDataSet])
barChartView.data = charData

And here is the result:

enter image description here

but I am not able to put Monday and Tuesday values where 1.04, 1.12, 1.20... values appear. Further, I am not able to add the labels on BarChartData function (as on Android).

Where should I add Monday and Tuesday values to be shown above of each column?

Thanks in advance!


Solution

  • If you look at the answer found at the following link, you will see that someone has gotten the new Swift 2.3 version of Charts to permit the use of string labels on the x-axis:

    https://stackoverflow.com/a/39149070/6889264

    You indicate that you are working with Swift 3.0, but the revised BarChartData() function syntax that created this issue looks to be the same between 2.3 and 3.0, so it seems likely the same solution applies.

    EDIT

    Just tested, that answer can be implemented in Swift 3.