我在当前项目中使用了以下库来显示饼图:https://github.com/danielgindi/Charts。
但是,在iPhone小屏幕设备上,我遇到了饼图显示的问题。我已经实现了以下代码来显示饼图:
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这样的小设备上运行应用程序时,会出现问题。标签数据重叠到了饼图上。
请查看以下图片,它将描述实际问题。
我尝试过寻找解决方案,但没有找到。我需要帮助来解决这个问题或者提供更好的解决方案。
valueLinePart/xAxisLabels
。 - Kamran