如何更新 IOS Charts 中的限制线,danielgindi/Charts?

4

所以我添加了几条限制线。

func addLimitLines() {
    let stopLoss = ChartLimitLine(limit: Double(1.70) , label: "stop loss")
    stopLoss.lineWidth = 0.5
    stopLoss.lineColor = .blue
    stopLoss.lineDashLengths = [8.0]
    chartView.rightAxis.addLimitLine(stopLoss)

    let takeProfit = ChartLimitLine(limit: Double(1.68), label: "take profit")
    takeProfit.lineWidth = 0.5
    takeProfit.lineColor = .blue
    takeProfit.lineDashLengths = [8.0]
    chartView.rightAxis.addLimitLine(takeProfit)
    chartView.reloadInputViews()
}

@IBAction func stopLossChange(_ sender: UISlider) {
   //slider action for updating the stoploss limitline
}

@IBAction func takeProfitChange(_ sender: UISlider) {
   //slider action for updating the takeprofit limitline
}

现在我想在移动滑块时更新限制线的值。enter image description here
1个回答

2
尽管在 Android 版本的 Charts 中我们有 invalidate() 来刷新 chartView,但在 iOS 中我没有看到它可用。在 iOS 中,我使用 animate 方法来更新数据,我相信您也可以按照以下方式实现:
@IBAction func stopLossChange(_ sender: UISlider) {
    self.updateLineLimit(Double(sender.value), label: "stop loss")
}

@IBAction func takeProfitChange(_ sender: UISlider) {
    self.updateLineLimit(Double(sender.value), label: "take profit")
}

private func updateLineLimit(_ value: Double, label: String) {
    if let line = chartView.rightAxis.limitLines.filter({ $0.label == label }).first {
        line.limit = value
        chartView.animate(yAxisDuration: 0.00001)
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接