6

I want to highlight and show value only when tapping on the iOS-Chart. I enabled the highlight but not the values because I only want them when tap and highlight

lineChartDataSet.drawValuesEnabled = false
lineChartDataSet.highlightEnabled = true

Do I need this function?

func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {}
Maruta
  • 1,063
  • 11
  • 24
  • 1
    Yes you need to use the chartValueSelected function, inside that function code whatever you want to happen when a value is highlighted – DevB2F Sep 24 '17 at 18:29
  • thanks! I would like to show the value only when touching the screen. Is there a way to do it? – Maruta Nov 18 '17 at 15:12
  • @Maruta did you manage to solve this? – EagerMike Jul 02 '18 at 11:04
  • actually not.. it doesn't seem an available feature in this iOS Chart library. What I could only do is to show the values after first tap and then drag to show other values. But I still couldn't show the value ONLY while taping or dragging – Maruta Jul 03 '18 at 12:10

1 Answers1

4

It's an old question, but I think it's still actual for some developers.

If you want to show values, baloon or highlight bar only while user is touching a chart view you may catch a touch event with UILongPressGestureRecognizer.

I instantiated new TappableLineChartView class from LineChartView. But you can work with BarChartView in the same way. Also if you don't want to instantiate new classes, you can incorporate addTapRecognizer and chartTapped functions in your view controller.

In my example, I show and hide values, but in the same manner, you can show and hide a balloon or another marker.

class TappableLineChartView: LineChartView {

    public override init(frame: CGRect)
    {
        super.init(frame: frame)
        addTapRecognizer()
    }

    public required init?(coder aDecoder: NSCoder)
    {
        super.init(coder: aDecoder)
        addTapRecognizer()
    }

    func addTapRecognizer() {
        let tapRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(chartTapped))
        tapRecognizer.minimumPressDuration = 0.1
        self.addGestureRecognizer(tapRecognizer)
    }

    @objc func chartTapped(_ sender: UITapGestureRecognizer) {
        if sender.state == .began || sender.state == .changed {
            // show
            let position = sender.location(in: self)
            let highlight = self.getHighlightByTouchPoint(position)
            let dataSet = self.getDataSetByTouchPoint(point: position)
            dataSet?.drawValuesEnabled = true
            highlightValue(highlight)
        } else {
            // hide
            data?.dataSets.forEach{ $0.drawValuesEnabled = false }
            highlightValue(nil)
        }
    }
}
AlexSmet
  • 2,141
  • 1
  • 13
  • 18