Divya Saraswati
Divya Saraswati

Reputation: 159

How to hide the markerView if bar value is 0.0. in bar graph using iOS charts

I am adding on marker on tap using iOS charts. I want to open the marker only if value is not zero. Can we do this using iOS Charts library.

Upvotes: 3

Views: 1510

Answers (2)

Jack
Jack

Reputation: 14379

You can create custom class & use it as -

public class XValueFormatter: NSObject, ValueFormatter {
     public func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
         return value <= 0.0 ? "" : String(describing: value)
     }
 }

use it as

  let chartData = BarChartData(dataSet: chartDataSet)
  chartData.setValueFormatter(XValueFormatter())

Upvotes: 1

Divya Saraswati
Divya Saraswati

Reputation: 159

First set the delegate of barChartView and then in chartValueSelected function write this code

  func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight){
    if entry.y == 0.0{
        barChartView.highlightValue(nil, callDelegate: false)
    }

}

wherever you need to hide the marker just add this code

 barChartView.highlightValue(nil, callDelegate: false)

Upvotes: 8

Related Questions