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)

4

1 回答 1

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
于 2017-09-15T12:19:52.217 回答