charts的导入
这里我们使用pod集成charts库,下面是关于pod的使用
关于pod集成第三方库
打开终端,进入项目文件夹,使用pod init 初始一个podfile文件
查找charts
pod search ‘Charts’
结果
将pod ‘Charts’, ‘~> 3.2.1’ 添加到Podfile文件中,在 do 和end之间,pod for xxxxx之后
终端执行pod install将Charts添加到项目中
Charts使用
LineChartView ----- 折线图
BarChartView ----- 柱状图
CandleStickChartView ----- K线图
PieChartView ----- 饼状图
RadarChartView ----- 雷达图
数据
let subjectScore: [[String: Any]] = [ ["subject": "语文", "score": 100.0], ["subject": "数学", "score": 150.0], ["subject": "英语", "score": 100.0], ["subject": "物理", "score": 150.0], ["subject": "化学", "score": 100.0], // ["subject": "生物", "score": 150.0]s ]
在ctrl中import Charts
创建的雷达图,添加到view可用
let chartView = RadarChartView(frame: CGRect(x: 106, y: 55, width: 200, height: 200))
chartView.delegate = self
chartView.backgroundColor = UIColor.init(red: 251/255, green: 145/255, blue: 78/255, alpha: 1)
chartView.layer.borderWidth = 1
// 图表的描述
chartView.chartDescription?.enabled = false
// chartView.chartDescription?.text = "这是一个图表"
// 对角线
chartView.webLineWidth = 0.5
// 边线
chartView.innerWebLineWidth = 0.5
// 对角线颜色
chartView.webColor = .white
// 边线颜色
chartView.innerWebColor = .white
// 透明度
chartView.webAlpha = 1
// 是否可以旋转
chartView.rotationEnabled = false
// 没有数据时的配置
chartView.noDataFont = .systemFont(ofSize: 14)
chartView.noDataText = "没有数据"
chartView.noDataTextColor = .white
// x轴
let xAxis = chartView.xAxis
xAxis.labelFont = .systemFont(ofSize: 9, weight: .light)
xAxis.xOffset = 0
xAxis.yOffset = 0
xAxis.valueFormatter = self
xAxis.labelTextColor = .white
// y轴
let yAxis = chartView.yAxis
yAxis.labelFont = .systemFont(ofSize: 9, weight: .light)
yAxis.labelCount = 1
yAxis.axisMinimum = 0
yAxis.axisMaximum = 100.0
yAxis.drawLabelsEnabled = false
// chartview的label
let l = chartView.legend
l.horizontalAlignment = .center
l.verticalAlignment = .top
l.orientation = .horizontal
l.drawInside = false
l.font = .systemFont(ofSize: 10, weight: .light)
l.xEntrySpace = 7
l.yEntrySpace = 5
l.textColor = .black
let entries = (0..<self.subjectScore.count).map { (item) -> RadarChartDataEntry in
return RadarChartDataEntry(value: self.subjectScore[item]["score"] as! Double)
}
// 图标的数据和标题
let set3 = RadarChartDataSet(values: entries, label: "sdasdasdas")
// 绘制区域的边缘颜色
set3.setColor(UIColor(red: 236/255, green: 236/255, blue: 252/255, alpha: 1))
// 绘制区域的填充颜色
set3.fillColor = UIColor(red: 236/255, green: 236/255, blue: 252/255, alpha: 1)
// 绘制区域是否填充
set3.drawFilledEnabled = true
// 绘制区域的透明度
set3.fillAlpha = 0.7
// 绘制区域的边缘
set3.lineWidth = 2
// 绘制区域的边缘是否高亮
set3.drawHighlightCircleEnabled = true
//
set3.setDrawHighlightIndicators(true)
let data = RadarChartData(dataSets: [set3])
// value的字体大小
data.setValueFont(.systemFont(ofSize: 8, weight: .light))
// 显示value
data.setDrawValues(false)
// value的颜色
data.setValueTextColor(.white)
chartView.data = data