使用图表在饼图中快速重叠标签

问题描述

我正在使用一个名为Charts的库在Swift中为我制作的这个小冠状病毒应用创建饼图。问题是,如果图表中的两个值非常接近,则标签会重叠。如果标签彼此接触,是否可以将标签彼此稍微移开?如果这不可能,那么如果值低于某个百分比,是否可以删除标签?我还尝试将标签放在切片之外,但是它们仍然重叠。

This is an image of how the labels are overlapping

这是我的饼图代码:

activeCasesDataEntry = PieChartDataEntry(value: filteredActiveCases[selectedIndex!].withoutCommas())
deathsDataEntry = PieChartDataEntry(value: filteredDeaths[selectedIndex!].withoutCommas())
recoveredDataEntry = PieChartDataEntry(value: filteredRecovered[selectedIndex!].withoutCommas())

// Format the text in the hole of the chart
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
pieChart.centerAttributedText = NSAttributedString(
    string: "Outcome Of \nAll Cases",attributes: [
        NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18),NSAttributedString.Key.foregroundColor: UIColor.label,NSAttributedString.Key.paragraphStyle: paragraphStyle
])

pieChart.entryLabelColor = .label
pieChart.entryLabelFont = .systemFont(ofSize: 20)
pieChart.usePercentValuesEnabled = true
pieChart.holeColor = .systemBackground
pieChart.holeRadiusPercent = 0.4
pieChart.rotationEnabled = false
pieChart.setExtraOffsets(left: 7,top: 0,right: 7,bottom: 0)

let activeCasesColor = NSUIColor(red: 219/255,green: 185/255,blue: 65/255,alpha: 1)
let deathsColor = NSUIColor(red: 214/255,green: 87/255,blue: 69/255,alpha: 1)
let recoveredColor = NSUIColor(red: 101/255,green: 201/255,blue: 122/255,alpha: 1)

let legend = pieChart.legend
legend.font = .systemFont(ofSize: 15)
legend.setCustom(entries: [
    LegendEntry(label: "Active Cases",form: .square,formSize: .nan,formLineWidth: .nan,formLineDashPhase: .nan,formLineDashLengths: .none,formColor: activeCasesColor),LegendEntry(label: "Deaths",formColor: deathsColor),LegendEntry(label: "Recovered",formColor: recoveredColor)
])
legend.xEntrySpace = 15

pieChartDataEntries = [recoveredDataEntry,activeCasesDataEntry,deathsDataEntry]

let chartDataSet = PieChartDataSet(entries: pieChartDataEntries,label: nil)
let chartData = PieChartData(dataSet: chartDataSet)

// Format the values with percentages
let formatter = NumberFormatter()
formatter.numberStyle = .percent
formatter.maximumFractionDigits = 1
formatter.multiplier = 1.0
chartData.setValueFormatter(DefaultValueFormatter(formatter: formatter))

chartDataSet.valueTextColor = .label
chartDataSet.colors = ChartColorTemplates.material()

pieChart.data = chartData

谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)