I am using https://github.com/danielgindi/Charts for creating a chart. After user scrolls the chart, I wanted to get the data point closest to the middle of the chart:
let currentMinX = self.chartView.lowestVisibleX
let currentMaxX = self.chartView.highestVisibleX
let midX = currentMinX + (currentMaxX - currentMinX)/2
let currentMinY = self.chartView.chartYMin // y is not scrollable, so I can use it
let currentMaxY = self.chartView.chartYMax
let midY = currentMinY + (currentMaxY - currentMinY)/2
let midPoint = CGPoint(x: midX, y: midY)
let closestPoint = chartView.getHighlightByTouchPoint(midPoint)
print("\(closestPoint?.x ?? 0)-\(closestPoint?.y ?? 0)")
Unfortunately closestPoint is the closest to the lower-left corner of the visible area, not the one closest to the middle. Why is that? Debugging movie: https://youtu.be/YTYqt5o6ifQ
EDIT: Apparently the transformer was returning the wrong value: I've fixed it by changing the Transformer's method:
@objc open func valueForTouchPoint(x: CGFloat, y: CGFloat) -> CGPoint
{
return CGPoint(x: x, y: y)//CGPoint(x: x, y: y).applying(pixelToValueMatrix)
}