绘制柱状图
//MARK: 绘制柱状图
func test4()
{
self.barChartView.frame = CGRect(x: 0, y: 160, width: self.view.bounds.width, height: self.view.bounds.height/2 - 64)
self.barChartView.delegate = self
self.view.addSubview(self.barChartView)
barChartView.backgroundColor = .white
//基本样式
barChartView.noDataText = "暂无数据" //没有数据时的文字提示
barChartView.drawValueAboveBarEnabled = true //数值显示在柱形的上面还是下面
barChartView.drawBarShadowEnabled = false //是否绘制柱形的阴影背景
//barChartView的交互设置
self.barChartView.scaleYEnabled = false //取消Y轴缩放
self.barChartView.doubleTapToZoomEnabled = false //取消双击缩放
self.barChartView.dragEnabled = true //启用拖拽图表
self.barChartView.dragDecelerationEnabled = false //拖拽后是否有惯性效果
self.barChartView.dragDecelerationFrictionCoef = 0 //拖拽后惯性效果的摩擦系数(0~1),数值越小,惯性越不明显
//设置barChartView的X轴样式
let xAxis = self.barChartView.xAxis
xAxis.axisLineWidth = 1 //设置X轴线宽
xAxis.labelPosition = .bottom //X轴的显示位置,默认是显示在上面的
xAxis.drawGridLinesEnabled = true //不绘制网格线
//xAxis.l = 4 //设置label间隔,若设置为1,则如果能全部显示,则每个柱形下面都会显示label
xAxis.labelTextColor = UIColor.init(hex: "#919191") //label文字颜色
xAxis.labelCount = 3
xAxis.axisLineColor = UIColor.init(hex: "#E6E6E6")
xAxis.drawAxisLineEnabled = true
self.barChartView.rightAxis.enabled = false //不绘制右边轴
//设置左侧Y轴的样式
let leftAxis = self.barChartView.leftAxis
leftAxis.forceLabelsEnabled = false //不强制绘制制定数量的label
//leftAxis = false //是否只显示最大值和最小值J
leftAxis.axisMinimum = 0.00 //设置Y轴的最小值
leftAxis.drawZeroLineEnabled = true //从0开始绘制
leftAxis.axisMaximum = 2.00 //设置Y轴的最大值
leftAxis.inverted = false //是否将Y轴进行上下翻转
leftAxis.axisLineWidth = 0.0 //Y轴线宽
leftAxis.axisLineColor = UIColor.clear //Y轴颜色
leftAxis.labelCount = 4
leftAxis.forceLabelsEnabled = false
leftAxis.drawGridLinesEnabled = true
leftAxis.labelTextColor = UIColor.init(hex: "#919191")
leftAxis.labelFont = UIFont.systemFont(ofSize: 11)
//设置Y轴上标签的样式
leftAxis.labelPosition = .outsideChart //label位置
leftAxis.labelTextColor = UIColor.init(hex: "#919191") //文字颜色
leftAxis.labelFont = UIFont.systemFont(ofSize: 11) //文字字体
//设置Y轴上标签显示数字的格式
// let leftFormatter = NumberFormatter() //自定义格式
// leftFormatter.positiveSuffix = " $" //数字后缀单位
// barChartView.leftAxis.valueFormatter = DefaultAxisValueFormatter.init(formatter: leftFormatter)
//设置Y轴上网格线的样式
leftAxis.gridLineDashLengths = [0.0, 0.0] //设置虚线样式的网格线
leftAxis.gridColor = UIColor.clear //网格线颜色
leftAxis.gridAntialiasEnabled = true //开启抗锯齿
// let limitLine = ChartLimitLine(limit: 20, label: "限制线")
// limitLine.lineWidth = 2
// limitLine.lineColor = UIColor.green
// limitLine.lineDashLengths = [5.0, 5.0] //虚线样式
// limitLine.labelPosition = .topRight //位置
// leftAxis.addLimitLine(limitLine) //添加到Y轴上
// leftAxis.drawLimitLinesBehindDataEnabled = true //设置限制线绘制在柱形图的后面
self.barChartView.legend.enabled = false //不显示图例说明
self.barChartView.chartDescription?.text = "" //不显示,就设为空字符串即可
self.setData()
}
func setData()
{
var xVals = [String]()
for i in 0...30
{
xVals.append("12月\(i)日")
// xVals.append(NSString(format: "%d年", i+1993) as String)
}
//chartView.xAxis.valueFormatter = KMChartAxisValueFormatter.init(xValues as NSArray)
var yVals = [BarChartDataEntry]()
for j in 0...30
{
let val = randomBetween(0.00, 2.00)
yVals.append(BarChartDataEntry.init(x: Double(j), y: val))
}
//创建BarChartDataSet对象,其中包含有Y轴数据信息,以及可以设置柱形样式
let set1 = BarChartDataSet(entries: yVals, label: "test")
//set1.bar = 0.2 //柱形之间的间隙占整个柱形(柱形+间隙)的比例
set1.drawValuesEnabled = false //是否在柱形图上面显示数值
set1.highlightEnabled = false //点击选中柱形图是否有高亮效果,(双击空白处取消选中)
set1.colors = [UIColor.init(hex: "#EEEEEE")]
//将BarChartDataSet对象放入数组中
var dataSets = [BarChartDataSet]()
dataSets.append(set1)
//创建BarChartData对象, 此对象就是barChartView需要最终数据对象
self.barChartView.xAxis.valueFormatter = KMChartAxisValueFormatter.init(xVals as NSArray)
let data:BarChartData = BarChartData(dataSets: dataSets)
data.setValueFont(UIFont.init(name: "HelveticaNeue-Light", size: 10)!) //文字字体
data.setValueTextColor(UIColor.orange) //文字颜色
data.barWidth = 0.7
//自定义数据显示格式
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
//formatter.positiveFormat = " $"
self.barChartView.leftAxis.valueFormatter = DefaultAxisValueFormatter.init(formatter: formatter)
self.barChartView.data = data
self.barChartView.animate(xAxisDuration: 1)
}
private func randomBetween(_ firstNum: Double, _ secondNum: Double) -> Double{
return Double(arc4random()) / Double(UINT32_MAX) * abs(firstNum - secondNum) + min(firstNum, secondNum)
}
// //点击选中柱形图时的代理方法
// func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
//
// }
//没有选中柱形图时的代理方法
func chartValueNothingSelected(_ chartView: ChartViewBase){
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
}
//捏合放大或缩小柱形图时的代理方法
func chartScaled(_ chartView: ChartViewBase, scaleX: CGFloat, scaleY: CGFloat)
{
}
//拖拽图表时的代理方法
func chartTranslated(_ chartView: ChartViewBase, dX: CGFloat, dY: CGFloat)
{
}
绘制折线图
func test2()
{
lineChartView.frame = CGRect(x: 20, y: 60, width: self.view.bounds.width - 40, height: 200)
self.view.addSubview(lineChartView)
lineChartView.delegate = self
lineChartView.backgroundColor = .white
lineChartView.noDataText = "暂无数据"
//设置交互样式
lineChartView.scaleYEnabled = false //取消Y轴缩放
lineChartView.doubleTapToZoomEnabled = true //双击缩放
lineChartView.dragEnabled = true //启用拖动手势
lineChartView.dragDecelerationEnabled = true //拖拽后是否有惯性效果
lineChartView.dragDecelerationFrictionCoef = 0.9 //拖拽后惯性效果的摩擦系数(0~1),数值越小,惯性越不明显
//设置X轴样式
let xAxis = lineChartView.xAxis
xAxis.axisLineWidth = 1.0/UIScreen.main.scale //设置X轴线宽
xAxis.labelPosition = .bottom //X轴的显示位置,默认是显示在上面的
xAxis.drawGridLinesEnabled = false;//不绘制网格线
xAxis.spaceMin = 4;//设置label间隔
xAxis.axisMinimum = 0
xAxis.labelTextColor = UIColor.blue//label文字颜色
//设置Y轴样式
lineChartView.rightAxis.enabled = false //不绘制右边轴
let leftAxis = lineChartView.leftAxis
leftAxis.labelCount = 4 //Y轴label数量,数值不一定,如果forceLabelsEnabled等于YES, 则强制绘制制定数量的label, 但是可能不平均
leftAxis.forceLabelsEnabled = true //不强制绘制指定数量的label
leftAxis.axisMinimum = 0 //设置Y轴的最小值
leftAxis.drawZeroLineEnabled = true //从0开始绘制
//leftAxis.axisMaximum = 1000 //设置Y轴的最大值
leftAxis.inverted = false //是否将Y轴进行上下翻转
leftAxis.axisLineWidth = 0.0 //设置Y轴线宽
leftAxis.axisLineColor = UIColor.clear//Y轴颜色
//leftAxis.valueFormatter = NumberFormatter()//自定义格式
//leftAxis.s //数字后缀单位
leftAxis.labelPosition = .outsideChart//label位置
leftAxis.labelTextColor = UIColor.init(hex: "#919191")//文字颜色
leftAxis.labelFont = UIFont.systemFont(ofSize: 11)//文字字体
//设置网格样式
leftAxis.gridLineDashLengths = [3.0,3.0] //设置虚线样式的网格线
leftAxis.gridColor = UIColor.init(hex: "#E6E6E6") //网格线颜色
leftAxis.gridAntialiasEnabled = true //开启抗锯齿
// //添加限制线
// let litmitLine = ChartLimitLine(limit: 260, label: "限制线")
// litmitLine.lineWidth = 2
// litmitLine.lineColor = UIColor.green
// litmitLine.lineDashLengths = [5.0,5.0] //虚线样式
// litmitLine.labelPosition = .topRight
// litmitLine.valueTextColor = UIColor.brown
// litmitLine.valueFont = UIFont.systemFont(ofSize: 12)
// leftAxis.addLimitLine(litmitLine)
// leftAxis.drawLimitLinesBehindDataEnabled = true //设置限制线绘制在折线图的后面
// //设置折线图描述及图例样式
// lineChartView.chartDescription?.text = "" //折线图描述
// lineChartView.chartDescription?.textColor = UIColor.cyan //描述字体颜色
// lineChartView.legend.form = .line // 图例的样式
// lineChartView.legend.formSize = 20 //图例中线条的长度
// lineChartView.legend.textColor = UIColor.darkGray
//设置折线图的数据
let xValues = ["x1","x2","x3","x4","x5","x6","x7","x8","x9","x10","x11","x12","x13","x14","x15","x16","x17","x18","x19","x20","x21","x22","x23","x24","x25","x26"]
lineChartView.xAxis.valueFormatter = KMChartAxisValueFormatter.init(xValues as NSArray)
//ineChartView.xAxis.labelCount = 12
//lineChartView.leftAxis.valueFormatter = KMChartAxisValueFormatter.init()
//在这里如果不需要自定义x轴的数据格式,可以使用原生的格式如下:
//`lineChartView.xAxis.valueFormatter = IndexAxisValueFormatter.init(values: xValues)`
let leftValueFormatter = NumberFormatter() //自定义格式
leftValueFormatter.positiveSuffix = "%" //数字后缀单位
lineChartView.leftAxis.valueFormatter = DefaultAxisValueFormatter.init(formatter: leftValueFormatter)
lineChartView.legend.form = .none
// var yDataArray1 = [ChartDataEntry]()
// for i in 0...xValues.count-1 {
// let y = arc4random()%500
// let entry = ChartDataEntry.init(x: Double(i), y: Double(y))
//
// yDataArray1.append(entry)
// }
var yDataArray1 = [ChartDataEntry]()
for i in 0...xValues.count-1 {
let y = arc4random()%500
let entry = ChartDataEntry.init(x: Double(i), y: Double(y))
yDataArray1.append(entry)
}
let set1 = LineChartDataSet.init(entries: yDataArray1, label: "")
set1.drawCirclesEnabled = false //是否绘制转折点
set1.colors = [UIColor.init(hex: "#007EFF")]
set1.lineWidth = 1
set1.mode = .linear //设置曲线是否平滑
set1.drawFilledEnabled = true
set1.fillColor = UIColor.init(hex: "#DFEFFF")
set1.fillAlpha = 0.5
//
// var yDataArray2 = [ChartDataEntry]();
// for i in 0...(xValues.count-1) {
// let y = arc4random()%500+1
// let entry = ChartDataEntry.init(x: Double(i), y: Double(y))
//
// yDataArray2.append(entry);
// }
// let set2 = LineChartDataSet.init(entries: yDataArray2, label: "test1")
// set2.colors = [UIColor.green]
// set2.drawCirclesEnabled = false
// set2.lineWidth = 1.0
let data = LineChartData.init(dataSets: [set1])
lineChartView.data = data
//lineChartView.animate(xAxisDuration: 1.0, yAxisDuration: 1.0, easingOption: .easeInBack)
lineChartView.animate(xAxisDuration: 1) //设置动画时间
}
func showMarkerView(value:String)
{
let marker = MarkerView.init(frame: CGRect(x: 20, y: 20, width: 60, height: 20))
marker.chartView = self.lineChartView
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 60, height: 20))
label.text = value
label.textColor = UIColor.white
label.font = UIFont.systemFont(ofSize: 12)
label.backgroundColor = UIColor.gray
label.textAlignment = .center
marker.addSubview(label)
self.lineChartView.marker = marker
}
// 点击选中柱形图时的代理方法 点击选中折线图时的代理方法
func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight)
{
if self.lineChartView.frame.size.height > 0
{
self.showMarkerView(value: "\(entry.y)")
}
}
网友评论