如何解决使用图表在饼图中快速重叠标签
我正在使用一个名为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 举报,一经查实,本站将立刻删除。