Swift - 第三方图表库Charts使用详解17(散点图)
作者:hangge | 2018-08-27 08:10
1,基本用法
下面是包含两组数据的散点图。
import UIKit import Charts class ViewController: UIViewController { //散点图 var chartView: ScatterChartView! override func viewDidLoad() { super.viewDidLoad() //创建散点图组件对象 chartView = ScatterChartView() chartView.frame = CGRect(x: 20, y: 80, width: self.view.bounds.width - 40, height: 260) self.view.addSubview(chartView) //第一组散点图的10条随机数据 let dataEntries1 = (0..<10).map { (i) -> ChartDataEntry in let val = Double(arc4random_uniform(100) + 3) return ChartDataEntry(x: Double(i), y: val) } let chartDataSet1 = ScatterChartDataSet(values: dataEntries1, label: "图例1") //第二组散点图的10条随机数据 let dataEntries2 = (0..<10).map { (i) -> ChartDataEntry in let val = Double(arc4random_uniform(100) + 3) return ChartDataEntry(x: Double(i), y: val) } let chartDataSet2 = ScatterChartDataSet(values: dataEntries2, label: "图例2") chartDataSet2.setColor(.orange) //第二组数据使用橙色 //目前散点图包括2组数据 let chartData = ScatterChartData(dataSets: [chartDataSet1, chartDataSet2]) //设置散点图数据 chartView.data = chartData } }
2,修改散点的大小
chartDataSet1.scatterShapeSize = 5
3,设置散点中心部分的颜色和大小
chartDataSet1.scatterShapeHoleColor = .orange chartDataSet1.scatterShapeHoleRadius = 2.5
4,散点的样式
(1)默认情况下散点图采用的是正方形形状的散点,我们可以通过 setScatterShape() 方法改用其它样式的散点:
chartDataSet1.setScatterShape(.circle) //使用圆形散点
(2)具体样式如下:
- 圆形:.circle
- 三角形:.triangle
- 十字:.cross
- 叉:.x
- 上箭头:.chevronUp
- 下箭头:.chevronDown
全部评论(0)