我有一个简单的折线图a * x + b示例。

import UIKit
import Charts

class ViewController: UIViewController {

@IBOutlet weak var lineChart: LineChartView!

@IBOutlet weak var bStepper: UIStepper!
@IBOutlet weak var aStepper: UIStepper!

var aValue: Double = 1
var bValue: Double = 1

var dataEntries = [ChartDataEntry]()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    aValue = aStepper.value
    bValue = bStepper.value

    updateChartData()
}

@IBAction func aChange(_ sender: UIStepper) {
    aValue = sender.value
    updateChartData()
}

@IBAction func bChange(_ sender: UIStepper) {
    bValue = sender.value
    updateChartData()
}

func updateChartData() {
    lineChart.noDataText = "No data"
    for i in -10...10 {
        let dataEntry = ChartDataEntry(x: Double(i), y: aValue*Double(i)+bValue)
        dataEntries.append(dataEntry)
    }
    let lineFuncOne = LineChartDataSet(entries: dataEntries, label: nil)
    lineFuncOne.colors = [NSUIColor.blue]
    let chartData = LineChartData(dataSet: lineFuncOne)
    lineChart.data = chartData
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}


我想创建一种方法,每当我在应用程序中递增或递减步进器的值时,便使用a和b步进器的值更新折线图(a * x + b)。您能帮我创建这个解决方案吗?我开始了自己的旅程,并以此告终。

最佳答案

我自己解决了

var dataEntries = ChartDataEntry
放入updateChartData()并处理发件人insinde updateChartData()的a,b值

关于ios - 如何使用步进值更新折线图?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59097301/

10-09 16:14