iOS Swift Charts的使用(三): 餅狀圖的實(shí)現(xiàn)

最近項(xiàng)目中遇到了需要獲取網(wǎng)站上的數(shù)據(jù),然后以折線圖的方式表示出來的需求。于是發(fā)現(xiàn)了swift下非常強(qiáng)大的圖表庫(kù)-# Charts


下面是這個(gè)系列的幾篇匯總:
1、折線圖
2、柱狀圖
3、餅狀圖
4、k線圖


這一篇我們來看一下有關(guān)餅狀圖的實(shí)現(xiàn)。實(shí)現(xiàn)后的效果如下圖所示:

Image-1-1.jpg

1、我們需要導(dǎo)入非常強(qiáng)大的圖表庫(kù)-Charts
2、進(jìn)行餅狀圖的初始化,并設(shè)置其基本樣式,同時(shí)設(shè)置代理。由于代碼中有詳細(xì)的注釋,下面直接貼代碼


func test3()
{
    pieChartView.frame = CGRect(x: 20, y: 60, width: 500, height: 600)
    self.view.addSubview(pieChartView)
    pieChartView.backgroundColor = UIColor.init(red: 230/255, green: 253/255.0, blue: 253/255.0, alpha: 1)
    pieChartView.setExtraOffsets(left: 40, top: 10, right: 40, bottom: 30)  //設(shè)置這塊餅的位置
    pieChartView.chartDescription?.text = "餅狀圖示例" //描述文字
    pieChartView.chartDescription?.font = UIFont.systemFont(ofSize: 12)
    pieChartView.chartDescription?.textColor = UIColor.black
    
    pieChartView.usePercentValuesEnabled = true  //轉(zhuǎn)化為百分比
    //pieChartView.dragDecelerationEnabled = false //把拖拽效果關(guān)了
    pieChartView.drawEntryLabelsEnabled = true //顯示區(qū)塊文本
    pieChartView.entryLabelFont = UIFont.systemFont(ofSize: 10) //區(qū)塊文本的字體
    pieChartView.entryLabelColor = UIColor.white
    pieChartView.drawSlicesUnderHoleEnabled = true
    
    pieChartView.drawHoleEnabled = true  //這個(gè)餅是空心的
    pieChartView.holeRadiusPercent = 0.382  //空心半徑黃金比例
    pieChartView.holeColor = UIColor.white //空心顏色設(shè)置為白色
    pieChartView.transparentCircleRadiusPercent = 0.5  //半透明空心半徑
    
    pieChartView.drawCenterTextEnabled = true //顯示中心文本
    pieChartView.centerText = "餅狀圖"  //設(shè)置中心文本,你也可以設(shè)置富文本`centerAttributedText`
    
    //圖例樣式設(shè)置
    pieChartView.legend.maxSizePercent = 1  //圖例的占比
    pieChartView.legend.form = .circle //圖示:原、方、線
    pieChartView.legend.formSize = 8 //圖示大小
    pieChartView.legend.formToTextSpace = 4 //文本間隔
    pieChartView.legend.font = UIFont.systemFont(ofSize: 8)
    pieChartView.legend.textColor = UIColor.gray
    pieChartView.legend.horizontalAlignment = .left
    pieChartView.legend.verticalAlignment = .top
    
    self.pieChartView.animate(xAxisDuration: 1.0, yAxisDuration: 1.0, easingOption: .easeInBack)
    
    self.drawPieChartView()
    
}

3、設(shè)置好圖表的樣式后,下面來將數(shù)據(jù)加載上去,相關(guān)代碼如下:


func drawPieChartView()
{
    let titles = ["紅","黃","藍(lán)色","橙","綠"]
    let yData = [20,30,10,40,60]
    var yVals = [PieChartDataEntry]()
    
    for i in 0...titles.count - 1
    {
        let entry = PieChartDataEntry.init(value: Double(yData[i]), label: titles[i])
        yVals.append(entry)
    }
    
    let dataSet = PieChartDataSet.init(values: yVals, label:"test")
    dataSet.colors = [UIColor.red,UIColor.yellow,UIColor.blue,UIColor.orange,UIColor.green] //設(shè)置名稱和數(shù)據(jù)的位置 都在內(nèi)就沒有折線了
    dataSet.xValuePosition = .insideSlice
    dataSet.yValuePosition = .outsideSlice
    dataSet.sliceSpace = 1 //相鄰塊的距離
    dataSet.selectionShift = 6.66  //選中放大半徑
    
    //指示折線樣式
    dataSet.valueLinePart1OffsetPercentage = 0.8 //折線中第一段起始位置相對(duì)于區(qū)塊的偏移量, 數(shù)值越大, 折線距離區(qū)塊越遠(yuǎn)
    dataSet.valueLinePart1Length = 0.8 //折線中第一段長(zhǎng)度占比
    dataSet.valueLinePart2Length = 0.4 //折線中第二段長(zhǎng)度最大占比
    dataSet.valueLineWidth = 1 //折線的粗細(xì)
    dataSet.valueLineColor = UIColor.brown //折線顏色
    
    let data = PieChartData.init(dataSets: [dataSet])
    data.setValueFormatter(KMChartAxisValueFormatter.init()) //格式化值(添加個(gè)%)
    data.setValueFont(UIFont.systemFont(ofSize: 10.0))
    data.setValueTextColor(UIColor.lightGray)
    pieChartView.data = data
    
    
    
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容