ChartsOrg / Charts

Beautiful charts for iOS/tvOS/OSX! The Apple side of the crossplatform MPAndroidChart.
Apache License 2.0
27.59k stars 6k forks source link

Highlight position in `chartValueSelected` is always 0 / nan #4998

Open SaladDays831 opened 1 year ago

SaladDays831 commented 1 year ago

What did you do?

Created a basic LineChartView, implemented the chartValueSelected of the ChartViewDelegate.

What did you expect to happen?

I want to get the xPx and yPx values of the highlight to place a custom UIView on top of the chart at the highlighted position

What happened instead?

The chartValueSelected gets called successfully, but I get weird values for all the positions: (highlight.drawX, highlight.drawY) = (0.0, 0.0) (highlight.xPx, highlight.yPx) = (nan, nan)

When setting a breakpoint inside the init method (the one with xPx and yPx) of the Highlight class - the xPx and yPx parameters passed into it are correct. But I assume I'm getting a different Highlight object in the chartValueSelected method (the one that's called from ChartViewBase -> highlightValue(), without xPx and yPx)

Charts Environment

Charts version/Branch/Commit Number:4.1.0 Xcode version:14.0 Swift version:5 Platform(s) running Charts:iOS *macOS version running Xcode:Ventura*

Demo Project

I don't have a demo project, but can provide it if needed. Currently I just want to verify if it's a global issue

SaladDays831 commented 1 year ago

I solved the problem by implementing a longPressGestureRecognizer and doing smth like this in gesture.state == .began / .changed:

let gestureLocation = gesture.location(in: chart)
guard let highlight = chart.getHighlightByTouchPoint(gestureLocation) else { return }
let pointInChart = CGPoint(x: highlight.xPx, y: highlight.yPx)
let pointInView = chart.convert(pointInChart, to: view)