![](/img/trans.png)
[英]iOS Charts - place Y value label at the bottom instead of the top of the circle
[英]How to add custom value in charts for bottom label in swift iOS
我必須使用 swift iOS 在圖表中顯示帶有美元符號的 [0, 25, 50, 75, 100]
var xValue = [25.0, 50.0]
var dollarValue = ["$25", "$50", "$75", "$100"]
在視圖中添加了 didLoad
rightAxis.valueFormatter = IndexAxisValueFormatter(values: dollarValue)
func updateCharts() {
var chartDataEntry = [BarChartDataEntry]()
let spaceForBar = 10.0;
for i in 0..<xValue.count {
let value = BarChartDataEntry(x: Double(i) * spaceForBar, y: xValue[i])
chartDataEntry.append(value)
}
let chartDataSet = BarChartDataSet(entries: chartDataEntry, label: "10x In-Store ")
chartDataSet.colors = ChartColorTemplates.material()
let chartMain = BarChartData(dataSet: chartDataSet)
chartMain.barWidth = 5.0
horizontalBarChartContainnerView.animate(yAxisDuration: 0.5)
horizontalBarChartContainnerView.data = chartMain
}
我想顯示 0 $25 $50 $75 $100 而不是 0 10 20 30 40 50
您需要創建一個自定義格式化程序
public class CustomAxisValueFormatter: NSObject, AxisValueFormatter {
public func stringForValue(_ value: Double, axis: AxisBase?) -> String {
return "$\(Int(value))"
} }
並將軸值格式化程序設置為您的自定義格式化程序
xAxis.valueFormatter = CustomAxisValueFormatter()
This code is working fine
public class CustomAxisValueFormatter: AxisValueFormatter {
var labels: [String] = []
public func stringForValue(_ value: Double, axis: AxisBase?) -> String {
let count = self.labels.count
guard let axis = axis, count > 0 else {
return ""
}
let factor = axis.axisMaximum / Double(count)
let index = Int((value / factor).rounded())
if index >= 0 && index < count {
return self.labels[index]
}
return ""
}
}
在 viewDidLoad() 中添加
rightAxis.axisMaximum = 4
let customAxisValueFormatter = CustomAxisValueFormatter()
customAxisValueFormatter.labels = ["$25", "$80", "$75", "$100"]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.