iOS Charts庫的簡單使用 - BarChartView
iOS Charts庫的簡單使用 - PieChartView
iOS Charts庫的簡單使用 - LineChartView
iOS Charts庫的簡單使用 - CombinedChartView
Charts 庫各類之間屬性基本互通耀盗,可相互參考圈膏,更多屬性參看 iOS Charts庫的簡單使用 - BarChartView
更多使用方法及屬性參考 MPAndroidChart
文檔:MPAndroidChart Documentation
最后附上 demo蠢络,demo有個別錯誤未修改,請甄別火欧。
PieChartView
的部分屬性及簡單使用
開局一張簡單的餅圖
let pieChartView = PieChartView(frame: CGRect(x: 0, y: 300, width: UIScreen.main.bounds.width, height: 200))
view.addSubview(pieChartView)
let datas: [Double] = [32, 12, 65, 23, 50]
var values = [PieChartDataEntry]()
for i in 0..<datas.count {
values.append(PieChartDataEntry(value: datas[i], label: "\(datas[i])"))
}
let dataSet = PieChartDataSet(entries: values, label: "圖例")
dataSet.colors = ChartColorTemplates.vordiplom()
let data = PieChartData(dataSet: dataSet)
pieChartView.data = data
image.png
屬性
//是否根據(jù)所提供的數(shù)據(jù), 將顯示數(shù)據(jù)轉(zhuǎn)換為百分比格式。默認false
pieChartView.usePercentValuesEnabled = true
//空心半徑占比尽狠。默認0.5
pieChartView.holeRadiusPercent = 0.2
//半透明空心半徑占比蚕礼。默認0.55
pieChartView.transparentCircleRadiusPercent = 0.55
//半透明空心的顏色。
pieChartView.transparentCircleColor = .red
//是否允許拖拽旋轉(zhuǎn)席舍。默認true
pieChartView.rotationEnabled = true
pieChartView.dragDecelerationEnabled = true
//拖拽旋轉(zhuǎn)后慣性系數(shù)0-1布轿。默認0.9
pieChartView.dragDecelerationFrictionCoef = 0.9
//設(shè)置為false,禁止點擊手勢高亮顯示值来颤,值仍然可以通過拖動或編程方式突出顯示汰扭。默認true
pieChartView.highlightPerTapEnabled = true
//標記,指示是否應(yīng)該繪制入口標簽福铅。默認 YES
pieChartView.drawEntryLabelsEnabled = true
//條目標簽的顏色萝毛。
pieChartView.entryLabelColor = .red
//條目標簽字體大小。
pieChartView.entryLabelFont = .systemFont(ofSize: 15)
image.png
//餅狀圖是否是空心.默認YES
pieChartView.drawHoleEnabled = true
//空心顏色滑黔。默認white
pieChartView.holeColor = .cyan
//是否顯示中心文字笆包。默認YES
pieChartView.drawCenterTextEnabled = true
//中心文字
pieChartView.centerText = "中心文字"
let centerAttributedText = NSMutableAttributedString(string: "中心文字")
centerAttributedText.setAttributes([NSAttributedString.Key.foregroundColor : UIColor.red], range: NSRange(location: 0, length: "中心".count))
//中心文字 富文本
pieChartView.centerAttributedText = centerAttributedText
//中心文字的偏移量
pieChartView.centerTextOffset = CGPoint(x: 0, y: 0)
//中心文字顯示的半徑百分比。默認1.0
pieChartView.centerTextRadiusPercent = 0.5
image.png
//餅圖顯示的最大角度略荡。默認360
pieChartView.maxAngle = 180
//餅圖顯示時旋轉(zhuǎn)的角度庵佣。默認270
pieChartView.rotationAngle = 180
image.png
//餅圖周圍的最小偏移量,影響餅圖大小汛兜。默認0
pieChartView.minOffset = 0
//是否雙指拖拽旋轉(zhuǎn)巴粪,值為true時則單指無法拖拽旋轉(zhuǎn)。默認false
pieChartView.rotationWithTwoFingers = false
//是否顯示圖例
pieChartView.legend.enabled = true
//圖例在餅狀圖中的大小占比序无。默認0.95
pieChartView.legend.maxSizePercent = 0.95
//圖例文本間隔验毡。默認5.0
pieChartView.legend.formToTextSpace = 15.0
//圖例字體大小衡创。默認10.0
pieChartView.legend.font = .systemFont(ofSize: 10)
//圖例字體顏色間隔帝嗡。默認black
pieChartView.legend.textColor = .red
//圖例在餅狀圖中水平方向的位置。默認left
pieChartView.legend.horizontalAlignment = .left
//圖例在餅狀圖中垂直方向的位置璃氢。默認bottom
pieChartView.legend.verticalAlignment = .bottom
//圖例排列方向哟玷。默認horizontal
pieChartView.legend.orientation = .horizontal
//圖示樣式。默認square
pieChartView.legend.form = .square
//圖示大小。默認8.0
pieChartView.legend.formSize = 8
PieChartDataSet
屬性
//是否顯示數(shù)值巢寡。默認true
dataSet.drawValuesEnabled = true
//文字顯示位置喉脖。默認insideSlice
dataSet.xValuePosition = .outsideSlice
//文字顯示位置。默認insideSlice
dataSet.yValuePosition = .outsideSlice
//數(shù)值文字大小
dataSet.valueFont = .systemFont(ofSize: 15)
dataSet.valueTextColor = .red
//文字大小
dataSet.entryLabelFont = .systemFont(ofSize: 20)
//文字顏色
dataSet.entryLabelColor = .orange
//yValuePosition == .outsideSlice 時生效抑月,指示線顏色和餅圖切片顏色相同树叽。默認false
dataSet.useValueColorForLine = false
//yValuePosition == .outsideSlice 時生效,指示線顏色谦絮。抵消useValueColorForLine题诵。默認black
//dataSet.valueLineColor = .red
//yValuePosition == .outsideSlice 時生效,指示線寬度层皱。默認1.0
dataSet.valueLineWidth = 2
//yValuePosition == .outsideSlice 時生效性锭,指示線偏移量占餅圖百分比。默認0.75
dataSet.valueLinePart1OffsetPercentage = 0.75
//yValuePosition == .outsideSlice 時生效叫胖,指示線前半部分長度占餅圖百分比草冈。默認0.3
dataSet.valueLinePart1Length = 0.3
//yValuePosition == .outsideSlice 時生效,指示線后半部分長度占餅圖百分比瓮增。默認0.4
dataSet.valueLinePart2Length = 0.4
//yValuePosition == .outsideSlice 時生效怎棱,指示線后半部分長度占餅圖百分比。默認0.4
//yValuePosition == .outsideSlice 時生效绷跑,旋轉(zhuǎn)餅圖時蹄殃,是否動態(tài)改變指示線的長度。默認true
dataSet.valueLineVariableLength = true
//餅圖選中后的顏色
dataSet.highlightColor = .purple
//餅圖距離邊距距離你踩。默認18
dataSet.selectionShift = 18
//餅片之間的距離诅岩。默認0.0
dataSet.sliceSpace = 5
//各餅片顏色
dataSet.colors = ChartColorTemplates.vordiplom()
//默認NO
dataSet.automaticallyDisableSliceSpacing = false
image.png
最后附上 demo