如何在 iOS 条形图上将这些双精度数转换为整数?
How can I convert these doubles to integers on this iOS Bar Chart?
我想将下图中的这些双精度数转换为整数:
我听从了其他帖子的建议并创建了一个实现 IAxisValueFormatter 的 YAxisValueFormatter() 但这不会影响条形图上的值,只会影响轴。
这是我的图表代码:
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:
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))!
}
}
给 chartDataSet
自己的 valueFormatter
:
展示了如何创建您需要的 valueFormatter。然后将charDataSet
的valueFormatter
设置为:
chartDataSet.valueFormatter = DigitValueFormatter()
我想将下图中的这些双精度数转换为整数:
我听从了其他帖子的建议并创建了一个实现 IAxisValueFormatter 的 YAxisValueFormatter() 但这不会影响条形图上的值,只会影响轴。
这是我的图表代码:
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:
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))!
}
}
给 chartDataSet
自己的 valueFormatter
:
charDataSet
的valueFormatter
设置为:
chartDataSet.valueFormatter = DigitValueFormatter()