我正在使用iOS的Charts库来创建饼图。我浏览了给出的演示,并希望在选择饼形图时添加一个标记。
现在的问题是,即使我可以获得百分比值,也无法获得所选值代表的标签字符串。我在演示中使用了XYMarker文件。
这是我的数据输入代码:
`let type = ["Basic Commission", "BSC Bonus", "Overriding Commission", "Other Income"]
let value = [25, 10, 50, 15]
let entries = (0..<count).map { (i) -> PieChartDataEntry in
return PieChartDataEntry(value: Double(value[i % value.count]),
label: type[i % type.count])
}
let set = PieChartDataSet(values: entries, label: "")
我的XYMarker文件如下:
public class XYMarkerView: BalloonMarker {
fileprivate var yFormatter = NumberFormatter()
public override init(color: UIColor, font: UIFont, textColor: UIColor, insets: UIEdgeInsets) {
yFormatter.numberStyle = .percent
yFormatter.maximumFractionDigits = 1
yFormatter.multiplier = 1
yFormatter.percentSymbol = " %"
super.init(color: color, font: font, textColor: textColor, insets: insets)
}
public override func refreshContent(entry: ChartDataEntry, highlight: Highlight) {
let string = yFormatter.string(from: NSNumber(floatLiteral: entry.y))!
setLabel(string)
}}
任何人都可以帮助获取标签字符串吗?
答案 0 :(得分:0)
import Foundation
import Charts
//Its Custom Class for Pie chart;
public class PieMarkerView: BalloonMarker {
fileprivate var yFormatter = NumberFormatter()
public override init(color: UIColor, font: UIFont, textColor: UIColor, insets: UIEdgeInsets) {
yFormatter.minimumFractionDigits = 1
yFormatter.maximumFractionDigits = 1
super.init(color: color, font: font, textColor: textColor, insets: insets)
}
public override func refreshContent(entry: ChartDataEntry, highlight: Highlight) {
if (entry is PieChartDataEntry) {
let pe = entry as! PieChartDataEntry
let string = ""
+ pe.label!
+ "\n "
+ yFormatter.string(from: NSNumber(floatLiteral: entry.y))!
setLabel(string)
return
}else{
let string = "x: "
+ String(entry.x)
+ ", y: "
+ yFormatter.string(from: NSNumber(floatLiteral: entry.y))!
setLabel(string)
}
}
}
在自定义类上方添加饼图标记视图
//Pie chart MarkerView
let marker = PieMarkerView(color: UIColor(white: 180/250, alpha: 1),
font: UIFont(name: "open sans", size: 12)!,
textColor: .white,
insets: UIEdgeInsets(top: 8, left: 8, bottom: 20, right: 8))
marker.chartView = pieChart
marker.minimumSize = CGSize(width: 80, height: 40)
pieChart.marker = marker