Reputation: 1141
There's a way to implement a method where, when user clicks on chart, I can know the x-y values to insert in my array?
I'm trying to use:
func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {}
but this method shows me the nearest point that I already have.
If I use:
@IBOutlet var chart: LineChartView!
let tap = UITapGestureRecognizer(target: self, action: #selector(self.longTouch(_:)))
self.chartView.addGestureRecognizer(tap)
func touch(_ sender: UIGestureRecognizer) {
let touchPoint = sender.location(in: self.chart)
}
the 'touchPoint' return the absolute view's x-y and not related to the chart.
Someone can help me? Thanks!
Upvotes: 1
Views: 1600
Reputation: 1141
I've resolved adding a gesture recognizer to the view:
// Gesture Recognizer to add a new value
func addGesture() {
let gesture = UILongPressGestureRecognizer(target: self, action: #selector(gestureRecognizerShouldBegin(_:)))
gesture.minimumPressDuration = 0.3
_chartView?.addGestureRecognizer(gesture)
}
in the gesture method I've used the valueForTouchPoint(point: _)
that inerith to the LineChartView
.
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer.state == .began {
// Recognize the location in the view
let touchLocation = gestureRecognizer.location(in: self._chartView)
// The 'x-y' value where user touch the view
let touchedPoint: CGPoint? = self._chartView?.valueForTouchPoint(point: touchLocation, axis: .right)
guard let point = touchedPoint else { return false }
// save the value...
return true
}
And, if I zoom or pan the chart, the method return the correct value x-y values.
Hope this will help someone!
Upvotes: 2