Search code examples
iosswiftxcodeuisliderios-charts

How do I update the limit lines in IOS Charts, danielgindi/Charts


So I have added a couple of limit lines.

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
}

Now I want to update the limitline values whenever I move the sliders.enter image description here


Solution

  • Although in Android version of Charts we have invalidate() that refreshes the chartView but in iOS i don't see its available. In iOS i use to update data with animate methods and i believe you can also achieve this as follows,

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