Charts 功能强大,基本功能及属性如下:
本文主要讲解的是折线图:)
//添加Charts
let lineChart = LineChartView()
chartView.addSubview(lineChart)
lineChart.snp.makeConstraints { (make) in
make.edges.equalToSuperview().inset(UIEdgeInsetsMake(kDefault_bt_height + kDefault_20_pad + kDefault_lb_height, kDefault_24_pad, kDefault_lb_height, kDefault_24_pad))
}
let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0]
setChart(months, values: unitsSold)
//设置charts方法
func setChart(_ dataPoints: [String], values: [Double]) {
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(x: Double(i), y: values[i])
dataEntries.append(dataEntry)
}
let lineChartDataSet = LineChartDataSet(values: dataEntries, label: "单位:万元")
let lineChartData = LineChartData(dataSet: lineChartDataSet)
lineChart.data = lineChartData
//右下角图标描述
lineChart.chartDescription?.text = "ChartView"
//左下角图例
// lineChart.legend.formSize = 30
// lineChart.legend.form = .square
lineChart.legend.textColor = kDefault_0x949494_clolr
//设置X轴坐标
lineChart.xAxis.valueFormatter = IndexAxisValueFormatter(values: dataPoints)
lineChart.xAxis.granularity = 1.0
lineChart.xAxis.labelPosition = .bottom
lineChart.xAxis.drawGridLinesEnabled = false
lineChart.xAxis.axisLineColor = kDefault_0x949494_clolr
lineChart.xAxis.labelTextColor = kDefault_0x949494_clolr
//设置Y轴坐标
// lineChart.rightAxis.isEnabled = false
//不显示右侧Y轴
lineChart.rightAxis.drawAxisLineEnabled = false
//不显示右侧Y轴数字
lineChart.rightAxis.enabled = false
lineChart.leftAxis.axisLineColor = kDefault_0x949494_clolr
lineChart.leftAxis.gridColor = kDefault_0x949494_clolr
lineChart.leftAxis.labelTextColor = kDefault_0x949494_clolr
//设置双击坐标轴是否能缩放
lineChart.scaleXEnabled = false
lineChart.scaleYEnabled = false
// lineChart.dragEnabled = true
// lineChart.dragDecelerationEnabled = true
//设置图表背景色和border
//必须设置enable才能有效
// lineChart.drawGridBackgroundEnabled = true
// lineChart.drawBordersEnabled = true
// lineChart.gridBackgroundColor = UIColor.red
// lineChart.borderColor = UIColor.orange
// lineChart.borderLineWidth = 5
//设置折线线条
// lineChartDataSet.fillColor = kDefault_0xff6600_clolr
// lineChartDataSet.lineWidth = 4
//外圆
lineChartDataSet.setCircleColor(kDefault_0xff6600_4_clolr)
//画外圆
// lineChartDataSet.drawCirclesEnabled = true
//内圆
lineChartDataSet.circleHoleColor = kDefault_0xff6600_clolr
//画内圆
// lineChartDataSet.drawCircleHoleEnabled = true
//线条显示样式
// lineChartDataSet.lineDashLengths = [1,3,4,2]
// lineChartDataSet.lineDashPhase = 0.5
lineChartDataSet.colors = [kDefault_0xff872e_clolr]
//线条上的文字
lineChartDataSet.valueColors = [kDefault_0xff872e_clolr]
//显示
// lineChartDataSet.drawValuesEnabled = true
//添加显示动画
lineChart.animate(xAxisDuration: 1)
}
下面是样式
Charts第一次使用就能发现它的强大和无可挑剔,完全可以实现自定义的基本样式。
-end