用户点击图表时添加点 - 'LineChartView'(iOS-Charts)

时间:2017-05-29 15:15:43

标签: ios swift charts ios-charts

有一种方法可以实现一种方法,当用户点击图表时,我可以知道要在我的数组中插入的x-y值吗?

我正在尝试使用:

func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {}

但是这个方法向我展示了我已经拥有的最近点。

如果我使用:

@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)
}

'touchPoint'返回绝对视图的x-y,与图表无关。

有人可以帮帮我吗?谢谢!

1 个答案:

答案 0 :(得分:1)

我已经解决了为视图添加手势识别器的问题:

// Gesture Recognizer to add a new value
func addGesture() {
    let gesture = UILongPressGestureRecognizer(target: self, action: #selector(gestureRecognizerShouldBegin(_:)))
    gesture.minimumPressDuration = 0.3
    _chartView?.addGestureRecognizer(gesture)
}

在手势方法中,我使用了valueForTouchPoint(point: _)的{​​{1}} 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
}

并且,如果我缩放或平移图表,该方法将返回正确的值x-y值。

希望这会对某人有所帮助!