11

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!

Community
  • 1
  • 1
Francisco Romero
  • 12,787
  • 22
  • 92
  • 167

2 Answers2

3

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.

Community
  • 1
  • 1
Samuel Hayden
  • 142
  • 10
0
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
let daysValues = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0]

setChart(days, values: daysValues)

var dataEntries: [BarChartDataEntry] = []

for i in 0..<dataPoints.count {
    let dataEntry = BarChartDataEntry(value: values[i], xIndex: i)
    dataEntries.append(dataEntry)
}

let chartDataSet = BarChartDataSet(yVals: dataEntries, label: "Days Value")
let chartData = BarChartData(xVals: days, dataSet: chartDataSet)
barChartView.data = chartData

I have taken this answer from https://www.appcoda.com/ios-charts-api-tutorial/ : that you can refer for more options and detailed description.

prabodhprakash
  • 3,825
  • 24
  • 48
  • I am using Swift 3.0 and there is no function with parameters as `BarChartDataEntry(double, Int)`. Only `BarChartDataEntry(double, double)` are available. – Francisco Romero Sep 27 '16 at 11:40
  • We can still pass Int as double? does it not work this way? – prabodhprakash Sep 27 '16 at 11:52
  • Yes, I have tried passing it as a double with `Double(i)` (Xcode 8 suggested it to me but I forgot to update the comment). Now the problem that I am facing is the error `fatal error: Index out of range` so I am looking for if there is more index than needed on the code. I will submit any changes I get. – Francisco Romero Sep 27 '16 at 12:01
  • Okay, to do a quick experiment, you can try hardcode the range of index and then run the code to see, if you get legends properly. That will help us identify, if the legends are working or not. – prabodhprakash Sep 27 '16 at 12:06
  • If you look at my code, it is almost the same your code and mine. The problem again comes because I cannot set `xVals: days` to my `BarChartData` function. – Francisco Romero Sep 27 '16 at 12:10
  • **P.S:** I am using Charts 3.0 and Swift 3.0. – Francisco Romero Sep 27 '16 at 12:11
  • When i went to github repo, it says "Disclaimer: This is a legacy release to support Swift 2.3 for Charts v2.2.5. If you need Swift 3.0 support please wait for Charts v3.0.0 to be released". I'm not sure, how are you using 3.0 version. – prabodhprakash Sep 27 '16 at 12:14
  • On the info it says that it supports Swift 3.0. What can I understand is that it is only not released to use with CocoaPods but you can use it with Swift. I am really new on Swift and iOS development so maybe I am understanding it wrong. – Francisco Romero Sep 27 '16 at 12:19
  • Further, I think that, as the charts are shown on my project, it is really supporting Swift 3.0. – Francisco Romero Sep 27 '16 at 12:21
  • They have made a release 6 days ago - v2.3.0. They are yet to make a release that support Swift 3.0. You can see it here: https://github.com/danielgindi/Charts/releases – prabodhprakash Sep 27 '16 at 12:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/124296/discussion-between-prabodhprakash-and-error404). – prabodhprakash Sep 27 '16 at 12:22