在 ios Charts (4.0.3) 的最后更新中,我一直在嘗試在 xAxis 上實作自定義標簽。
有這個代碼:
// some model inside with parsed JSON, this is working well.
var dataRad = [DataModel]()
var dataEntries = [BarChartDataEntry]()
for item in dataRad {
guard let level = Double(item.level!), let id = Double(item.id!) else { return }
let dataEntry = BarChartDataEntry(x: id, y: level)
// labels.append(item.temp!)
dataEntries.append(dataEntry)
}
// some setup of the Charts, e.g. colors etc.
let xAxis = chartsView.xAxis
xAxis.labelFont = UIFont(name:"Play-Bold", size:10)!
xAxis.labelPosition = .bottom
xAxis.labelCount = self.dataRad.count
xAxis.drawLabelsEnabled = true
let labels: [String] = ["1, 2", "2, 2", "3, 3", "4, 4", "5, 5", "6, 6", "7, 7", "8, 8", "9, 9", "10, 10"]
xAxis.valueFormatter = IndexAxisValueFormatter(values: labels)
此代碼不起作用。我在 xAxis 上看不到標簽。但是如果我洗掉這個格式化程式,我會看到我的“id”變數的默認值(在“for in”回圈中)。
我嘗試了一些技巧,例如擴展,添加。帶格式化程式的類。沒有什么對我有用...
請幫助使用標簽解決此問題。謝謝!
uj5u.com熱心網友回復:
這是我當前的圖表設定
let labelFont = UIFont.SFProText(style: .bold, ofSize: 12)
let lineWidth = CGFloat(1)
// Setup style for X axis
barChartView.xAxis.axisLineWidth = lineWidth
barChartView.xAxis.labelFont = labelFont
barChartView.xAxis.labelTextColor = .white
barChartView.xAxis.axisLineColor = .white
barChartView.xAxis.labelPosition = .bottom
barChartView.xAxis.drawAxisLineEnabled = true
barChartView.xAxis.drawLabelsEnabled = true
barChartView.xAxis.drawGridLinesEnabled = false
barChartView.xAxis.spaceMax = 0
barChartView.xAxis.spaceMin = 0
barChartView.xAxis.decimals = 0
barChartView.xAxis.granularity = 1
barChartView.xAxis.granularityEnabled = true
barChartView.xAxis.labelFont = labelFont
barChartView.xAxis.axisLineColor = .white
barChartView.xAxis.labelTextColor = .white
然后你可以試試我的CustomIndexAxisFormatter
:
class CustomIndexAxisFormatter: NSObject, AxisValueFormatter {
private var _values: [(Double, String)] = [(Double, String)]()
private var _valueCount: Int = 0
public var values: [(Double, String)] {
get {
return _values
}
set {
_values = newValue
_valueCount = _values.count
}
}
public override init(){
super.init()
}
public init(values: [(Double, String)]) {
super.init()
self.values = values
}
public static func with(values: [(Double, String)]) -> CustomIndexAxisFormatter? {
return CustomIndexAxisFormatter(values: values)
}
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
if let firstIndex = self.values.firstIndex(where: { $0.0 == value }) {
return _values[firstIndex].1
}
if value <= 1000 && values.indices.contains(Int(value)) {
return _values[Int(value)].1
} else {
guard let axis = axis else {
return ""
}
let count = Double(self._valueCount)
let diff = value - axis.axisMinimum
let range = axis.axisRange
let step = range / (count - 1)
let index = Int(round(diff / step))
guard values.indices.contains(index) else { return "" }
return _values[index].1
}
}
}
然后當你制作資料集時:
let labels: [String] = ["1, 2", "2, 2", "3, 3", "4, 4", "5, 5", "6, 6", "7, 7", "8, 8", "9, 9", "10, 10"]
var arrayXLabel: [(Double, String)] = []
for (index, item) in entries.enumerated() {
if index < labels.count - 1 {
// need to use item.x as Double value of Array label
// because we will need to link label with chart point properly
arrayXLabel.append((item.x, labels[index]))
}
}
barChartView.xAxis.valueFormatter = CustomIndexAxisFormatter(values: arrayXLabel ))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/506529.html
上一篇:為什么使用存盤在變數中的模式創建的貓鼬模式看不到必需的標志?
下一篇:錯誤執行緒8:EXC_BAD_INSTRUCTION(code=EXC_I386_INVOP,subcode=0x0)有什么問題?