我有一个简单的折线图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)。您能帮我创建此解决方案吗?我已经开始了旅程,并以此结束。
答案 0 :(得分:0)
我自己解决了这个问题 把 var dataEntries = ChartDataEntry 放入updateChartData()并处理发件人insinde updateChartData()的a,b值