Tworzenie niestandardowego formatowania:
class RandomCustomFormatter: NSObject, IValueFormatter {
convenience init(lineChart: LineChartView, xArray: [Double], yArray: [Double]) {
self.init()
var y = yArray
y.shuffle(count: y.count)
var dataEntries = [ChartDataEntry]()
var c = 0
for _ in xArray {
dataEntries.append(ChartDataEntry(x: xArray[c], y: y[c]))
c+=1
}
let theDataSet = LineChartDataSet(values: dataEntries, label: "Test Data")
print("dataentries shuffled: \(dataEntries)")
lineChart.data = LineChartData(dataSet: theDataSet)
}
public func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
let valueToUse = Int(value)
print("valuetouse: \(valueToUse)")
return String(valueToUse)
}
}
Dodaj rozszerzenie tablicy:
extension Array {
mutating func shuffle(count: Int) {
for _ in 0...count-1 {
sort { (_,_) in arc4random() < arc4random() }
}
}
}
ustawić formatowania:
//x datapoints
let x = [1.0,2.0,3.0,4.0,5.0,6.0]
//y datapoints
var y = [8.0,16.0,23.0,63.0,66.0,323.0]
let formatter = RandomCustomFormatter(lineChart: lineChart, xArray: x, yArray: y)
self.lineChart?.data?.setValueFormatter(formatter)
wynik 1:
Wynik 2: