我正在使用PieChartView在我的应用程序中绘制以下饼图。我的要求是仅显示%(percentage)而不显示键名。
这是我的视图中绘制饼图的代码
func drawPieChart(pieChart: PieChartView, data: [String: Double]) -> Void {
//Setup constraints first
var dataEntries = [PieChartDataEntry]()
var total = 0.0
for (key, val) in data {
total += val
}
for (key, val) in data {
let percent = (val / total)
let entry = PieChartDataEntry(value: percent, label: key)
dataEntries.append(entry)
}
let chartDataSet = PieChartDataSet(values: dataEntries, label: "")
chartDataSet.colors = ChartColorTemplates.bookingPieChart()
chartDataSet.sliceSpace = 2
chartDataSet.selectionShift = 5
let chartData = PieChartData(dataSet: chartDataSet)
let formatter = NumberFormatter()
formatter.numberStyle = .percent
formatter.maximumFractionDigits = 0
chartData.setValueFormatter(DefaultValueFormatter(formatter: formatter))
pieChart.data = chartData
}
答案 0 :(得分:1)
嗨,我想如果您不想显示键名,则需要在标签中发送一个空字符串
更改
let entry = PieChartDataEntry(value: percent, label: key)
到
let entry = PieChartDataEntry(value: percent, label: "")
答案 1 :(得分:0)
以下代码使用“ LegendEntry”类解决了我的问题。
for (key, val) in data {
let percent = (val / total)
let entry = PieChartDataEntry(value: percent, label: "")
dataEntries.append(entry)
let legendEntry: LegendEntry = LegendEntry(label: key, form: .circle, formSize: 5.0, formLineWidth: 0, formLineDashPhase: 0, formLineDashLengths: [0], formColor: colorData[key])
legendEntries.append(legendEntry)
}
pieChart.legend.setCustom(entries: legendEntries)
答案 2 :(得分:0)
如果要保留图例,请在PieChartDataSet上使用
set.entryLabelColor = UIColor(hex: 1, a: 0)