Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show and hide line chart highlight on touch

Tags:

ios-charts

I want to only highlight a data point when the finger is on the chart, as soon as it lifts off the screen I want to call, or simple deselect the highlight.

func chartValueNothingSelected(chartView: ChartViewBase) {
    print("Nothing Selected")
    markerView.hidden = true
}

I've tried to override the touch ended but haven't gotten it to work.

like image 985
Nick Hayward Avatar asked Jun 18 '16 16:06

Nick Hayward


2 Answers

You can turn off highlighting any bars/data all together using the highlightEnabled property.

Example of this is:

barChartView.data?.highlightEnabled = false

If you still want to be able to highlight values, but want them to automatically deselect after the touch has ended, I also found another function highlightValues(highs: [ChartHighlight]?) which says in the documentation..

Provide null or an empty array to undo all highlighting.

Call this when you want to deselect all the values and I believe this will work. Example of this could be:

let emptyVals = [ChartHighlight]()  
barChartView.highlightValues(emptyVals)

Ref: Charts Docs: highlightValues documentation

like image 188
fordrof Avatar answered Mar 15 '23 13:03

fordrof


If you don't have to do anything with the tapped data you can use:

barChartView.data?.highlightEnabled = false

If you want to use the tapped data point without displaying the highlight lines, you can use the selection delegate (don't forget to add ChartViewDelegate to your class):

yourChartView.delegate = self // setup the delegate

Add delegate function:

func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
  // do something with the selected data entry here

  yourChartView.highlightValue(nil) // deselect selected data point
}
like image 23
budiDino Avatar answered Mar 15 '23 14:03

budiDino