ios - 饼图的标签与小型设备中的饼图重叠
问题描述
我在当前项目中使用了以下库来显示饼图:https ://github.com/danielgindi/Charts 。
但是,我在 iPhone 小型设备中遇到了饼图问题。我已经实现了以下代码来显示 pi 图表:
class ChartVC: UIViewController {
@IBOutlet weak var pieChartView : PieChartView!
var arrPiChartData : [PiChartData]?
func setupPiChart() {
let l = pieChartView.legend
l.horizontalAlignment = .right
l.verticalAlignment = .bottom
l.orientation = .vertical
l.xEntrySpace = 0
l.yEntrySpace = 0
l.yOffset = 0
l.font = self.isPhone ? Typography.robotoRegular14 : Typography.robotoRegular18
self.pieChartView.entryLabelFont = Typography.robotoRegular14
self.pieChartView.drawHoleEnabled = false
self.pieChartView.notifyDataSetChanged()
}
func setupData() {
var arr = [PieChartDataEntry]()
if self.arrPiChartData != nil {
for piChartData in self.arrPiChartData! {
var name = ""
if let strName = piChartData.name {
name = strName
}
var value = 0.0
if let floatValue = piChartData.percentAmount {
value = Double(floatValue)
}
let entry = PieChartDataEntry(value: value, label: name)
arr.append(entry)
}
let set = PieChartDataSet(values: arr, label: "Total Sales by Company Users")
set.drawIconsEnabled = false
set.sliceSpace = 2
set.xValuePosition = .outsideSlice
set.entryLabelColor = UIColor.black
set.entryLabelFont = self.isPhone ? Typography.robotoRegular14 : Typography.robotoRegular18
set.colors = self.arrColours
let data = PieChartData(dataSets: [set])
let pFormatter = NumberFormatter()
pFormatter.numberStyle = .percent
pFormatter.maximumFractionDigits = 1
pFormatter.multiplier = 1
pFormatter.percentSymbol = " %"
data.setValueFormatter(DefaultValueFormatter(formatter: pFormatter))
self.pieChartView.data = data
} else {
self.pieChartView.data = nil
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.setupPiChart()
self.setupData()
}
//------------------------------------------------------------------------------
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.pieChartView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0)
}
}
此代码用于显示饼图。但是当我在 iPhone SE、5S、6、7、8 等小型设备上运行应用程序时出现问题。标签数据与饼图重叠。
请参阅下图,它将描述实际问题。
我试图找到解决方案,但没有得到。
我需要帮助来解决此问题或更好的解决方案。
解决方案
由于没有很好的方法在饼图中垂直显示更多图例,我更新了我的代码以在水平方向显示它。饼图中的标题也无法正确显示,因此请将其删除。为此,我将代码更新如下,以显示正确的饼图:
func setupPiChart() {
// This will align label text to top right corner
let l = pieChartView.legend
l.horizontalAlignment = .left
l.verticalAlignment = .bottom
l.orientation = .horizontal
l.xEntrySpace = 10
l.yEntrySpace = 0
l.font = self.isPhone ? Typography.robotoRegular14 : Typography.robotoRegular18
self.pieChartView.entryLabelFont = Typography.robotoRegular14
self.pieChartView.drawHoleEnabled = false
self.pieChartView.drawEntryLabelsEnabled = false
self.pieChartView.notifyDataSetChanged()
}
现在,它的显示效果如下图所示:
推荐阅读
- typescript - 如何修复导入库中的 Typescript 错误
- c# - “重试次数超过 10。失败。” 因为 csproj 配置错误
- python - 二次距离
- azure-data-explorer - Azure 数据资源管理器与 Azure Synapse Analytics(又名 SQL DW)
- r - 如何删除所有名为 .x、.y 的变量?
- android - Xiaomi: what code to request permission to show app badge icon
- c# - P/Invoke runtime STD error not redirected to file
- python - How to associate an input variable to a list name?
- javascript - Can't access parent elements
- python - 在python中覆盖列表的构造函数