danielgindi/Charts饼图选择指数

Sof*_*eda 3 charts ios pie-chart swift3

我正在使用图表绘制饼图.它工作正常,但我需要将选定的部分索引传递给下一个viewcontroller.我使用下面的代码,并阅读他们的图表3.0 的发行说明.我在Swift 3中工作.在以前的版本中,所选索引是从entry.dataSetIndex获得的.

通过这段代码,我总是得到0指数.请帮忙.

extension ViewController: ChartViewDelegate {

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

        print(highlight.dataSetIndex)
    }

}
Run Code Online (Sandbox Code Playgroud)

Ale*_*lex 10

使用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.
}
Run Code Online (Sandbox Code Playgroud)