折线图
import UIKit
importCharts
publicclassLineChartController:UIViewController{
//折线图
letlineChart =LineChartView(frame:CGRect(x:0, y:100, width:BKLScreenWidth, height:300))
override publicfuncviewDidLoad() {
super.viewDidLoad()
configLineChat()
}
funcconfigLineChat() {
title="折线图"
view.backgroundColor = .white
view.addSubview(lineChart)
letmonths = ["11月","12月","1月","2月","3月","4月"]
letunitsSold = [220.0,49.0,60.0,300.0,182.0,106.0]
setChart(months, values: unitsSold)
}
funcsetChart(_dataPoints: [String], values: [Double]) {
vardataEntries: [ChartDataEntry] = []
foriin0..
letdataEntry =ChartDataEntry(x:Double(i), y: values[i])
dataEntries.append(dataEntry)
}
letlineChartDataSet =LineChartDataSet(values: dataEntries, label:"单位:万元")
letlineChartData =LineChartData(dataSet: lineChartDataSet)
lineChart.data= lineChartData
//设置最多显示多少数据 可滑动
lineChart.setVisibleXRangeMaximum(4)
//设置最小的padding(图表对于整个 Chart),默认为10,不设置大一点可能会造成数据在 Chart 外无法显示
lineChart.minOffset = 30
//设置X轴坐标
lineChart.xAxis.valueFormatter = IndexAxisValueFormatter(values: dataPoints)
//x轴坐标间距
lineChart.xAxis.granularity = 1.0
//x轴位置 居下
lineChart.xAxis.labelPosition = .bottom
//是否显示竖线 单元格样式
lineChart.xAxis.drawGridLinesEnabled = false
//x轴底部线条颜色
lineChart.xAxis.axisLineColor = .gray
lineChart.xAxis.labelTextColor = .gray
//不显示右侧Y轴
lineChart.rightAxis.drawAxisLineEnabled = false
//不显示右侧Y轴数字
lineChart.rightAxis.enabled = false
//设置双击坐标轴是否能缩放
lineChart.scaleXEnabled = false
lineChart.scaleYEnabled = false
//设置图表背景色和border
//必须设置enable才能有效
lineChart.drawGridBackgroundEnabled = true
lineChart.drawBordersEnabled = true
//图表背景色
lineChart.gridBackgroundColor = .clear
//容器颜色
lineChart.backgroundColor = .lightGray
//边框颜色
lineChart.borderColor = .clear
//边框宽度
lineChart.borderLineWidth = 5
//右下角图标描述
lineChart.chartDescription?.text = ""
//左下角图例
lineChart.legend.formSize = 20
//左下角方形或者圆形
lineChart.legend.form = .circle
//图标距离文字距离
lineChart.legend.formToTextSpace = 20
//左下角文字颜色
lineChart.legend.textColor = .orange
//添加显示动画
lineChart.animate(xAxisDuration:1)
//设置折线
//线条宽度
lineChartDataSet.lineWidth=2
//线条样式 虚线 实线
lineChartDataSet.lineDashLengths= [0,0]
//线条设置颜色,可多个
lineChartDataSet.colors= [.red]
//线条上的文字颜色
lineChartDataSet.valueColors= [.purple]
//显示线条
lineChartDataSet.drawValuesEnabled=true
//外圆颜色
lineChartDataSet.setCircleColor(UIColor.orange.withAlphaComponent(0.5))
//外圆大小
lineChartDataSet.circleRadius=10
//允许外圆
lineChartDataSet.drawCirclesEnabled=true
//内圆颜色
lineChartDataSet.circleHoleColor= .orange
//内圆大小
lineChartDataSet.circleHoleRadius=5
//允许内圆
lineChartDataSet.drawCircleHoleEnabled=true
}
}