Я хочу, чтобы преобразовать эти двойники в целых числах на этом графике ниже:Как я могу преобразовать эти двойники в целые числа на этой диаграмме iOS?
Я последовал совету других сообщений и создал YAxisValueFormatter(), который реализует IAxisValueFormatter, но это не влияет на значения на стержни, только ось.
Это мой график код:
func setChart(dataPoints: [String], values: [Double]){
let formato:BarChartFormatter = BarChartFormatter()
formato.setValues(values: dataPoints)
let xaxis:XAxis = XAxis()
let xAxis : XAxis = self.barChartView.xAxis;
barChartView.noDataText = "you need to provide some data for the chart."
var dataEntries: [BarChartDataEntry] = Array()
for i in 0..<dataPoints.count
{
let dataEntry = BarChartDataEntry(x: Double(i), y: values[i])
dataEntries.append(dataEntry)
}
xaxis.valueFormatter = formato
barChartView.xAxis.valueFormatter = xaxis.valueFormatter
xAxis.labelFont = UIFont(name: "Avenir", size: 14.0)!
xAxis.labelTextColor = UIColor.white
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Games Played")
let chartData = BarChartData(dataSets: [chartDataSet])
chartDataSet.colors = ChartColorTemplates.material()
barChartView.xAxis.labelPosition = .bottom
barChartView.xAxis.drawGridLinesEnabled = false
barChartView.xAxis.valueFormatter = xaxis.valueFormatter
barChartView.chartDescription?.enabled = false
barChartView.legend.enabled = true
barChartView.rightAxis.enabled = false
barChartView.data = chartData
barChartView.leftAxis.enabled = true
barChartView.legend.enabled = false
barChartView.chartDescription?.text = ""
chartData.addDataSet(chartDataSet)
barChartView.data = chartData
chartData.setDrawValues(true)
chartDataSet.drawValuesEnabled = true
barChartView.barData?.setValueFont(UIFont(name: "Avenir", size: 12.0))
barChartView.barData?.setValueTextColor(UIColor.white)
barChartView.xAxis.granularity = 1.0
barChartView.leftAxis.drawGridLinesEnabled = false
barChartView.rightAxis.drawGridLinesEnabled = false
barChartView.xAxis.drawGridLinesEnabled = false
barChartView.leftAxis.labelFont = UIFont(name: "Avenir", size: 12.0)!
barChartView.leftAxis.labelTextColor = UIColor.white
barChartView.leftAxis.axisMinimum = 0
barChartView.leftAxis.valueFormatter = YAxisValueFormatter()
self.barChartView.xAxis.labelPosition = XAxis.LabelPosition.bottom
}
И это мой YAxisValueFormatter класс:
class YAxisValueFormatter: NSObject, IAxisValueFormatter {
let numFormatter: NumberFormatter
override init() {
numFormatter = NumberFormatter()
numFormatter.minimumFractionDigits = 0
numFormatter.maximumFractionDigits = 0
// if number is less than 1 add 0 before decimal
numFormatter.minimumIntegerDigits = 0 // how many digits do want before decimal
numFormatter.paddingPosition = .beforePrefix
numFormatter.paddingCharacter = "0"
}
/// Called when a value from an axis is formatted before being drawn.
///
/// For performance reasons, avoid excessive calculations and memory allocations inside this method.
///
/// - returns: The customized label that is drawn on the axis.
/// - parameter value: the value that is currently being drawn
/// - parameter axis: the axis that the value belongs to
///
public func stringForValue(_ value: Double, axis: AxisBase?) -> String {
return numFormatter.string(from: NSNumber(floatLiteral: value))!
}
}
Если я получаю я получаю error- 'Не удается присвоить значение типа«YAxisValueFormatter»к типу 'IValueFormatter? – RDowns
Ключом является настройка и установка 'chartDataSet'' valueFormatter'. Я предполагал, что он может использовать тот же форматтер, что и ось, но это, по-видимому, неверно. Что произойдет, если вы скажете «chartDataSet.valueFormatter = YAxisValueFormatter(). NumFormatter'? – vacawama
К сожалению, такая же ошибка – RDowns