【问题标题】:danielgindi/Charts Pie Chart selected indexdanielgindi/Charts 饼图选择索引
【发布时间】:2016-11-29 05:38:55
【问题描述】:

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

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

extension ViewController: ChartViewDelegate {

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

        print(highlight.dataSetIndex)
    }

}

【问题讨论】:

    标签: ios charts swift3 pie-chart


    【解决方案1】:

    使用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.
    }
    

    【讨论】:

    • 非常感谢您的回答并节省了我的时间。非常感谢您的帖子。
    • 堆积条形图也需要highlight.stackIndex
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-14
    • 1970-01-01
    • 2012-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多