在 PieChartView 中仅显示来自(键,值)的数字

Show only numbers in PieChartView from (key, value)

我正在使用 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
}

您好,我想如果您不想显示键名,您需要在标签中发送一个空字符串

改变

 let entry = PieChartDataEntry(value: percent, label: key)

 let entry = PieChartDataEntry(value: percent, label: "")

以下代码使用 "LegendEntry" class.

解决了我的问题
    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)

如果要保留 PieChartDataSet 上的图例

set.entryLabelColor = UIColor(hex: 1, a: 0)