Search code examples
swiftxcodecoordinatesios-charts

How do I obtain the x-value of highlighted point when using Charts UI?


I have a variable that changes depending on the x-value of the point as defined by the user on the graph. How do I go about this? I need the x-coordinate of the point in order to change another view.

Right now, I have a simple CombinedChart. What I want is when apoint is highlighted, the x-coordinate is then obtained, and this changes a label. (For example, if the person clicks on (1,3), the label will display "1"

I have tried:

Global.chiller1x = Double(chiller1Chart.xAxis.labelPosition.rawValue)
print(String(format: "%.2f", Global.chiller1x ?? 0))

I have also looked at getMarkerPosition, etc but none seem to have worked.


Solution

  • You can use the Chart Delegate (ChartViewDelegate) method chartValueSelected() to capture user click with X & Y value on that touch points.

    Use Below Method:

    extension LineChartViewController: ChartViewDelegate{
        public func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
            //Print Value like entry.x & entry.y
        }
    }
    

    Hope this will helps you to get user touch with X & Y values.