最近項(xiàng)目中遇到了需要獲取網(wǎng)站上的數(shù)據(jù),然后以折線圖的方式表示出來的需求。于是發(fā)現(xiàn)了swift下非常強(qiáng)大的圖表庫-# Charts
下面是這個(gè)系列的幾篇匯總:
1、折線圖
2、柱狀圖
3、餅狀圖
4、k線圖
這一篇我們來看一下有關(guān)柱狀圖的實(shí)現(xiàn)。實(shí)現(xiàn)后的效果如下圖所示:

Image-1.jpg
1、我們需要導(dǎo)入非常強(qiáng)大的圖表庫-Charts
2、進(jìn)行柱狀圖的初始化,并設(shè)置其基本樣式,同時(shí)設(shè)置代理。由于代碼中有詳細(xì)的注釋,下面直接貼代碼
func test4()
{
self.barChartView.frame = CGRect(x: 0, y: 60, width: self.view.bounds.width, height: self.view.bounds.height/2 - 64)
self.barChartView.delegate = self
self.view.addSubview(self.barChartView)
barChartView.backgroundColor = UIColor.init(red: 230/255.0, green: 253/255.0, blue: 253/255.0, alpha: 1.0)
//基本樣式
barChartView.noDataText = "You need to provide data for the chart." //沒有數(shù)據(jù)時(shí)的文字提示
barChartView.drawValueAboveBarEnabled = true //數(shù)值顯示在柱形的上面還是下面
barChartView.drawBarShadowEnabled = false //是否繪制柱形的陰影背景
//barChartView的交互設(shè)置
self.barChartView.scaleYEnabled = false //取消Y軸縮放
self.barChartView.doubleTapToZoomEnabled = false //取消雙擊縮放
self.barChartView.dragEnabled = true //啟用拖拽圖表
self.barChartView.dragDecelerationEnabled = false //拖拽后是否有慣性效果
self.barChartView.dragDecelerationFrictionCoef = 0 //拖拽后慣性效果的摩擦系數(shù)(0~1),數(shù)值越小,慣性越不明顯
//設(shè)置barChartView的X軸樣式
let xAxis = self.barChartView.xAxis
xAxis.axisLineWidth = 1 //設(shè)置X軸線寬
xAxis.labelPosition = .bottom //X軸的顯示位置,默認(rèn)是顯示在上面的
xAxis.drawGridLinesEnabled = true //不繪制網(wǎng)格線
//xAxis.l = 4 //設(shè)置label間隔,若設(shè)置為1,則如果能全部顯示,則每個(gè)柱形下面都會(huì)顯示label
xAxis.labelTextColor = UIColor.brown //label文字顏色
xAxis.labelCount = 10
self.barChartView.rightAxis.enabled = false //不繪制右邊軸
//設(shè)置左側(cè)Y軸的樣式
let leftAxis = self.barChartView.leftAxis
leftAxis.forceLabelsEnabled = false //不強(qiáng)制繪制制定數(shù)量的label
//leftAxis = false //是否只顯示最大值和最小值
leftAxis.axisMinimum = -40 //設(shè)置Y軸的最小值
leftAxis.drawZeroLineEnabled = true //從0開始繪制
leftAxis.axisMaximum = 105 //設(shè)置Y軸的最大值
leftAxis.inverted = false //是否將Y軸進(jìn)行上下翻轉(zhuǎn)
leftAxis.axisLineWidth = 0.5 //Y軸線寬
leftAxis.axisLineColor = UIColor.black //Y軸顏色
leftAxis.labelCount = 5
leftAxis.forceLabelsEnabled = false
//設(shè)置Y軸上標(biāo)簽的樣式
leftAxis.labelPosition = .outsideChart //label位置
leftAxis.labelTextColor = UIColor.brown //文字顏色
leftAxis.labelFont = UIFont.systemFont(ofSize: 10) //文字字體
//設(shè)置Y軸上標(biāo)簽顯示數(shù)字的格式
let leftFormatter = NumberFormatter() //自定義格式
leftFormatter.positiveSuffix = " $" //數(shù)字后綴單位
barChartView.leftAxis.valueFormatter = DefaultAxisValueFormatter.init(formatter: leftFormatter)
//設(shè)置Y軸上網(wǎng)格線的樣式
leftAxis.gridLineDashLengths = [3.0, 3.0] //設(shè)置虛線樣式的網(wǎng)格線
leftAxis.gridColor = UIColor.init(red: 200/255.0, green: 200/255.0, blue: 200/255.0, alpha: 1.0) //網(wǎng)格線顏色
leftAxis.gridAntialiasEnabled = true //開啟抗鋸齒
let limitLine = ChartLimitLine(limit: 20, label: "限制線")
limitLine.lineWidth = 2
limitLine.lineColor = UIColor.green
limitLine.lineDashLengths = [5.0, 5.0] //虛線樣式
limitLine.labelPosition = .rightTop //位置
leftAxis.addLimitLine(limitLine) //添加到Y(jié)軸上
leftAxis.drawLimitLinesBehindDataEnabled = true //設(shè)置限制線繪制在柱形圖的后面
self.barChartView.legend.enabled = false //不顯示圖例說明
self.barChartView.chartDescription?.text = "" //不顯示,就設(shè)為空字符串即可
self.setData()
}
3、設(shè)置好圖表的樣式后,下面來將數(shù)據(jù)加載上去,相關(guān)代碼如下:
func setData()
{
var xVals = [String]()
for i in 0...60
{
xVals.append(NSString(format: "%d年", i+1993) as String)
}
//chartView.xAxis.valueFormatter = KMChartAxisValueFormatter.init(xValues as NSArray)
var yVals = [BarChartDataEntry]()
for j in 0...60
{
let val = (Double)(arc4random_uniform(40))
yVals.append(BarChartDataEntry.init(x: Double(j), y: val))
}
//創(chuàng)建BarChartDataSet對(duì)象,其中包含有Y軸數(shù)據(jù)信息,以及可以設(shè)置柱形樣式
let set1 = BarChartDataSet(values: yVals, label: "test")
//set1.bar = 0.2 //柱形之間的間隙占整個(gè)柱形(柱形+間隙)的比例
set1.drawValuesEnabled = true //是否在柱形圖上面顯示數(shù)值
set1.highlightEnabled = false //點(diǎn)擊選中柱形圖是否有高亮效果,(雙擊空白處取消選中)
set1.colors = ChartColorTemplates.material()
//將BarChartDataSet對(duì)象放入數(shù)組中
var dataSets = [BarChartDataSet]()
dataSets.append(set1)
//創(chuàng)建BarChartData對(duì)象, 此對(duì)象就是barChartView需要最終數(shù)據(jù)對(duì)象
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
//自定義數(shù)據(jù)顯示格式
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)
}
4、下面是幾個(gè)相關(guān)代理方法:
//點(diǎn)擊選中柱形圖時(shí)的代理方法
func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
}
//沒有選中柱形圖時(shí)的代理方法
func chartValueNothingSelected(_ chartView: ChartViewBase){
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
}
//捏合放大或縮小柱形圖時(shí)的代理方法
func chartScaled(_ chartView: ChartViewBase, scaleX: CGFloat, scaleY: CGFloat)
{
}
//拖拽圖表時(shí)的代理方法
func chartTranslated(_ chartView: ChartViewBase, dX: CGFloat, dY: CGFloat)
{
}