iOS 如何在折线图(图表)的标签中显示文本

How to display text in label for Line Chart (Chart) in iOS

我正在使用 https://github.com/danielgindi/Charts 库生成图表。(Swift-iOS) 我想显示具有 百分比值 的值,例如 1235(6%)、3478(34%)、742(3%) 等等。

这是我的成就:

这就是我想要的:

谁有想法请帮帮我

你必须像这个例子一样定义一个值格式化程序:

let pFormatter = NumberFormatter()
pFormatter.numberStyle = .percent
pFormatter.maximumFractionDigits = 1
pFormatter.multiplier = 1
pFormatter.percentSymbol = " %"
data.setValueFormatter(DefaultValueFormatter(formatter: pFormatter))

data.setValueFont(.systemFont(ofSize: 11, weight: .light))
data.setValueTextColor(.white)

chartView.data = data

这是从饼图示例复制的。

好的,我找到了一些东西,也许对你有帮助:

首先创建自定义格式化程序:

class CustomVF : IValueFormatter {
var maxValue : Double = 1
func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
    return "\(value) (\(value / totalSum * 100)%)"
}
}

那你就用它:

let set1 = BarChartDataSet(entries: yVals, label: "DataSet")
...
let customVF = CustomVF()
customVF.maxValue = 200.0
set1.valueFormatter = customVF