ios-charts如何调整饼图高度

ios-charts how to adjust pie chart height

我正在尝试使饼图正确适合比图表高得多的视图。问题是我想在图表底部包含一个垂直的图例列表,这会使实际图表的高度动态变化。如果我在数据集中有太多条目,则图表底部的图例线不会显示。

是否有实用的方法来获取图表的 "actual" 或 "rendered" 高度,以便我可以将 pieChart.frame.height 设置为此值?或者,我如何调整图表的高度,使其包含包括图例在内的所有内容?

@IBOutlet weak var pieChart: PieChartView!

var aDataEntry = PieChartDataEntry(value:30)
var bDataEntry = PieChartDataEntry(value:25)

var dataEntries = [PieChartDataEntry]()



override func viewDidLoad() {
    super.viewDidLoad()

    pieChart.chartDescription?.text = "A test"
    aDataEntry.label = "A"
    bDataEntry.label = "B"

    dataEntries = [aDataEntry, bDataEntry]

    updateChartData()

    // Do any additional setup after loading the view.
}

func updateChartData() {
    let chartDataSet = PieChartDataSet(values: dataEntries, label:nil)
    let chartData = PieChartData(dataSet: chartDataSet)
    var colours:[UIColor] = []
    for i in 1..<10 {
        colours.append(UIColor(named: "Colour" + String(i))!)
    }
    chartDataSet.colors = colours 
    pieChart.data = chartData
    pieChart.usePercentValuesEnabled = true
    pieChart.legend.orientation = .vertical
    pieChart.notifyDataSetChanged()
    print(pieChart.frame.height)
}

在 adding/settings 图例之后,您只需调用 pieChartView.notifyDataSetChanged() 否则,您的图例将不在视图中。 这是示例:

pieChartView.legend.verticalAlignment = .bottom
pieChartView.legend.horizontalAlignment = .center
pieChartView.legend.orientation    = .vertical
pieChartView.notifyDataSetChanged()

编辑 1:为了快速解决您的问题,请在调用 PieChartDataSet 时使用空字符串而不是 nil

let chartDataSet = PieChartDataSet(values: dataEntries, label:"")