Search code examples
swiftchartsswift2pie-chartios-charts

Force deselecting a slice from pie chart


I am looking for a way of forcing the slice of the pie chart to become unselected. It automatically deselects when you click out of the pie chart however I was wondering if there was a way to deselect it within a button action?

I have various buttons which displays filtered versions of the same data and, if a slice is selected when you click a button it remains selected.

Any help would be great.

Thanks


Solution

  • Within the function chartValueSelected which is called when a slice has been selected/highlighted, I set a global variable containing the DataSetIndex:

    var dataSetIndexToDeselect : Int = 0
    func chartValueSelected(chartView: ChartViewBase, entry: ChartDataEntry, dataSetIndex: Int, highlight: ChartHighlight) {
        dataSetIndexToDeselect = dataSetIndex  
    }
    

    On the button click, i unhighlight the slice by adding the following line the the IBAction:

    @IBAction func buttonClick(sender: AnyObject) {
        pieChartView.highlightValue(xIndex: -1, dataSetIndex: dataSetIndexToDeselect, callDelegate: false)
    }
    

    The '-1' on the xIndex causes the 'dataSetIndexToDeselect' value to no longer be selected.