0

I'm setting a CombinedChartView that have some value on xAxis, lets say (15, 16, 17, 2, 3)

with this line of code

for i in 0..<gameStatsMonth.count {

     let dataEntryTeam = ChartDataEntry(x: Double(gameStatsMonth[i].day!), y: Double(gameStatsMonth[i].points!))

     lineDataEntryTeam.append(dataEntryTeam)
}

It happens that the values are displayed sorted in xAxis, like this: 2, 3, 15, 16, 17.

There is any way to keep the first order of x values ? (the order on which they were inserted in lineDataEntryTeam)

mmarques
  • 625
  • 3
  • 9
  • 27
  • Are you see this answer https://stackoverflow.com/questions/41979926/how-to-achieve-trackball-and-access-label-above-bar-on-bar-charts-in-swift-3x/42019505#42019505 – IOS Singh Sep 15 '17 at 12:28
  • @mmarques I had meet the same issue, did you solve it? – HamGuy Jul 13 '18 at 15:08

1 Answers1

1

Add an array for the xAxis (outside your functions):

var xaxisVals = [Double]()

Update your for loop

for i in 0..<gameStatsMonth.count {
    let dataEntryTeam = ChartDataEntry(x: Double(gameStatsMonth[i].day!), y: Double(gameStatsMonth[i].points!))
    xaxisVals.append(Double(gameStatsMonth[i].day!))
    lineDataEntryTeam.append(dataEntryTeam)
}

Adopt IAxisValueFormatter:

class YourViewController: UIViewController, IAxisValueFormatter

Fill the delegate method:

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

In the class, define the delegate:

weak var axisFormatDelegate: IAxisValueFormatter?

In viewDidLoad set the delegate to self:

axisFormatDelegate = self

After the for loop, set the xAxis:

let xaxis = lineDataEntryTeam.xAxis
xaxis.granularityEnabled = true
xaxis.granularity = 1
xaxis.valueFormatter = axisFormatDelegate
Papershine
  • 4,995
  • 2
  • 24
  • 48
  • `return xaxisVals[Int(value)]` out of range in this case, `xaxisVals` contains all days, the `value` in the delegate method is not the index for `xaxisVals`, but for the value – HamGuy Jul 10 '18 at 00:09