charts的功能还是很完善的,记录一下自己的开发过程,话不多说直接上代码。
cocoapods 导入“ pod 'Charts' ”
类中引用 import Charts
func addBarChartView() {
//创建柱状图组件对象
BarChartView = HorizontalBarChartView()
self.addSubview(BarChartView)
BarChartView.snp.makeConstraints { (make) in
make.left.equalTo(0)
make.top.equalTo(40)
make.width.equalTo(SCREEN_WIDTH-40)
make.bottom.equalTo(-30)
}
//边界距离
BarChartView.setExtraOffsets(left: 0, top: 0, right: 30, bottom: 0)
//缩放
BarChartView.scaleXEnabled = false
BarChartView.scaleYEnabled = false
BarChartView.autoScaleMinMaxEnabled = false
BarChartView.highlightPerTapEnabled = false
BarChartView.highlightPerDragEnabled = false
BarChartView.pinchZoomEnabled = false //手势捏合
BarChartView.dragEnabled = false
//数值显示
BarChartView.noDataText = ""
//不显示图例
BarChartView.legend.enabled = false
//x轴显示在左侧
BarChartView.xAxis.labelPosition = .bottom
//y轴起始刻度为0
BarChartView.leftAxis.axisMinimum = 0
BarChartView.rightAxis.axisMinimum = 0
BarChartView.drawValueAboveBarEnabled = true //文字显示在内测
var dataEntries = [BarChartDataEntry]()
//传入 x,y轴的数据
for i in 0..<lineArray2!.count {
let entry = BarChartDataEntry(x: Double(i), y: Double(lineArray2![lineArray2!.count - 1 - i])!)
dataEntries.append(entry)
}
let chartDataSet = BarChartDataSet(entries:dataEntries, label: "图例1")
chartDataSet.valueFormatter = self
chartDataSet.colors = getMainColorArray(leng: lineArray2!.count) //颜色数组
chartDataSet.drawValuesEnabled = true //文字是否显示
chartDataSet.valueColors = getMainColorArray(leng: lineArray2!.count)
chartDataSet.valueFont = UIFont.systemFont(ofSize: 12)
//目前柱状图只包括1组立柱
let chartData = BarChartData(dataSets: [chartDataSet])
chartData.barWidth = 0.4
//设置柱状图数据
BarChartView.data = chartData
BarChartView.fitScreen()
// BarChartView.drawGridBackgroundEnabled = false;
BarChartView.drawGridBackgroundEnabled = false
BarChartView.gridBackgroundColor = UIColor.gray
//隐藏横线
BarChartView.xAxis.drawGridLinesEnabled = false
// BarChartView.chartDescription!.enabled = false
BarChartView.leftAxis.enabled = false
//设置虚线和下面的文字颜色
BarChartView.rightAxis.labelPosition = .outsideChart
BarChartView.rightAxis.drawGridLinesEnabled = true
BarChartView.rightAxis.gridLineDashLengths = [5,5]
BarChartView.rightAxis.labelTextColor = UIColor.init(hex: "#6D737B")
BarChartView.rightAxis.labelFont = UIFont.systemFont(ofSize: 12)
//设置的小数位没有用 不知道是我没理解对 还是有Bug
BarChartView.xAxis.decimals = 5
BarChartView.rightAxis.decimals = 5
BarChartView.rightAxis.axisLineColor = UIColor.clear
BarChartView.xAxis.labelTextColor = UIColor.init(hex: "#6D737B")
BarChartView.xAxis.labelFont = UIFont.systemFont(ofSize: 12)
//自定义y轴的文字
BarChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: lineArray1.reversed())
// x轴自定义文字 可以走代理方法
// BarChartView.rightAxis.valueFormatter = self
BarChartView.xAxis.labelCount = lineArray1.count //y轴个数
// BarChartView.rightAxis.labelCount = lineArray1.count//x轴个数
}
//可以自定义轴上的数据 必须实现代理 IAxisValueFormatter // BarChartView.rightAxis.valueFormatter = self
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
print("dadadas==\(Int(value))")
// if axis == BarChartView.rightAxis {
// }
return ""
}
// 可以自定义各自区域显示的的数据 必须实现代理 IValueFormatter // chartDataSet.valueFormatter = self
// 因为上面的小数位设置没起作用 不知在何处可以设置了 只能走代理方法手动限制小数位
func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
if value == 0{
return "0"
}
let st = String(format: "%.2f", value)
return st
}
以上内容基本可以解决大部分横屏柱状图问题 , 如有写的不对的地方 欢迎大佬指正!
网友评论