如何更新IOS图表中的极限线,danielgindi /图表

时间:2018-07-10 07:10:32

标签: ios swift xcode uislider ios-charts

所以我添加了一些限制线。

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 个答案:

答案 0 :(得分:1)

尽管在Charts的{​​{3}}版本中,我们有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)
    }
}