(2)样例代码
import UIKit import Charts class ViewController: UIViewController { //折线图 var chartView: LineChartView! override func viewDidLoad() { super.viewDidLoad() //创建折线图组件对象 chartView = LineChartView() chartView.frame = CGRect(x: 20, y: 80, width: self.view.bounds.width - 40, height: 300) self.view.addSubview(chartView) //生成20条随机数据 var dataEntries = [ChartDataEntry]() for i in 0..<20 { let y = arc4random()%100 let entry = ChartDataEntry.init(x: Double(i), y: Double(y)) dataEntries.append(entry) } //这50条数据作为1根折线里的所有数据 let chartDataSet = LineChartDataSet(values: dataEntries, label: "图例1") //目前折线图只包括1根折线 let chartData = LineChartData(dataSets: [chartDataSet]) //设置折现图数据 chartView.data = chartData } }
转载于:https://www.hangge.com/blog/cache/detail_2116.html