2016-11-29 84 views
2

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

通过此代码我总是得到0索引。请任何帮助。

extension ViewController: ChartViewDelegate { 

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

     print(highlight.dataSetIndex) 
    } 

} 

回答

5

使用的是iOS 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. 
} 
+0

我真的很感谢你的回答,节省我的时间。 真的非常感谢这篇文章。 –