Попробуйте это расширение: -
//Bar chart extensio to set custom xAxia and yAxis
extension BarChartView {
private class BarChartFormatter: NSObject, IAxisValueFormatter {
var labels: [String] = []
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
return labels[Int(value)]
}
init(labels: [String]) {
super.init()
self.labels = labels
}
}
func setBarChartData(xValues: [String], yValues: [String], label: String) {
var dataEntries: [BarChartDataEntry] = []
for i in 0..<yValues.count {
let dataEntry = BarChartDataEntry(x: Double(i), y: Double(yValues[i])!)
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: label)
chartDataSet.colors = [NSUIColor(red: 0/255.0, green: 110/255.0, blue: 194/255.0, alpha: 1.0)]
chartDataSet.valueFont = NSUIFont.systemFont(ofSize: 13)
let chartData = BarChartData(dataSet: chartDataSet)
let chartFormatter = BarChartFormatter(labels: xValues)
let xAxis = XAxis()
xAxis.valueFormatter = chartFormatter
self.xAxis.valueFormatter = xAxis.valueFormatter
self.drawValueAboveBarEnabled = true
self.data = chartData
}
}
Вызов функции: -
@IBOutlet var chartView: BarChartView!
...
chartView.setBarChartData(xValues: dates, yValues: Leads, label: "Leads")
Где даты и Leads - это массив строк