我想在下面的图表中将这些双打转换为整数:
我已经按照其他帖子的建议创建了一个实现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 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))!
}
}
答案 0 :(得分:1)
授予chartDataSet
自己的valueFormatter
:
This Q&A显示了如何创建所需的valueFormatter。然后将valueFormatter
的{{1}}设置为:
charDataSet