Jess Murray
Jess Murray

Reputation: 1339

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

Upvotes: 4

Views: 2141

Answers (2)

Nikhil Manapure
Nikhil Manapure

Reputation: 3878

 pieChart.highlightValue(nil)

This worked for me. I am on Charts (3.0.2), Swift 3, XCode 8.3.3.

Hope this helps someone.

Upvotes: 7

Jess Murray
Jess Murray

Reputation: 1339

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.

Upvotes: 3

Related Questions