danielgindi/Charts 饼图选择索引

danielgindi/Charts Pie Chart selected index

我正在使用 Charts to draw a pie chart. It is working fine but i need to pass the selected portion index to next viewcontroller. I used below code and also read their release notes 图表 3.0。我在 Swift 工作 3. 在以前的版本中,选择的索引来自 entry.dataSetIndex。

通过这段代码,我总是得到 0 索引。请帮忙。

extension ViewController: ChartViewDelegate {

    func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {

        print(highlight.dataSetIndex)
    }

}

使用iOS Charts 3.0.0,以下代码输出选定的索引:

class ChartController: UIViewController, ChartViewDelegate {

func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {

    if let dataSet = chartView.data?.dataSets[ highlight.dataSetIndex] {

        let sliceIndex: Int = dataSet.entryIndex( entry: entry)
        print( "Selected slice index: \( sliceIndex)")
    }
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear( animated)

    // 1. create chart view
    let chart = PieChartView( frame: self.view.frame)
    chart.delegate = self

    // TODO: exercise for reader...add data, styles, etc.
}