上篇文章已經(jīng)講述了折線圖的用法這邊文章主要來談餅狀圖嗅蔬。
其實Charts難的部分主要在于配置,所以同樣主要說說他的配置。
pieGraphView.setExtraOffsets(left: 10, top: 0, right: 60, bottom: 0) // 餅狀圖距離邊緣的間隙
pieGraphView.usePercentValuesEnabled = true // 是否根據(jù)所提供的數(shù)據(jù), 將顯示數(shù)據(jù)轉(zhuǎn)換為百分比格式
pieGraphView.dragDecelerationEnabled = false // 拖拽餅狀圖后是否有慣性效果
// viewPie?.drawSliceTextEnabled = false // 是否顯示區(qū)塊文本
// pieGraphView.drawSlicesUnderHoleEnabled = false
pieGraphView.chartDescription?.enabled = false // 圖形描述
pieGraphView.drawCenterTextEnabled = false // 是否顯示中間文字
pieGraphView.drawHoleEnabled = true // 餅狀圖是否是空心
pieGraphView.holeRadiusPercent = 0.5 // 空心半徑占比
pieGraphView.transparentCircleRadiusPercent = 0.52 //半透明空心半徑占比
pieGraphView.rotationEnabled = true // 旋轉(zhuǎn)指示
pieGraphView.rotationAngle = 0.0
pieGraphView.highlightPerTapEnabled = false // 點擊放大效果
// 圖例
let legend = self.pieGraphView.legend
legend.maxSizePercent = 1 // 圖例在餅狀圖中的大小占比, 這會影響圖例的寬高
legend.formToTextSpace = 10 // 文本間隔
legend.horizontalAlignment = .right // 圖例在餅狀圖中的位置
legend.verticalAlignment = .bottom // 圖例在餅狀圖中的位置
legend.orientation = .vertical // 圖例在餅狀圖中的位置
legend.font = UIFont.systemFont(ofSize: 12)
legend.textColor = UIColor.gray
// 數(shù)據(jù)
var yValues: [PieChartDataEntry] = [] // 所占比例
var colors: [UIColor] = [] // 區(qū)塊顏色
for i in 0..<modelList.count {
if (i + 1) % 6 == 0 {
colors.append(UIColor.colorWithHex("11bc5e"))
} else if (i + 1) % 5 == 0 {
colors.append(UIColor.colorWithHex("3c6af1"))
} else if (i + 1) % 4 == 0 {
colors.append(UIColor.colorWithHex("f94495"))
} else if (i + 1) % 3 == 0 {
colors.append(UIColor.colorWithHex("feca14"))
} else if (i + 1) % 2 == 0 {
colors.append(UIColor.colorWithHex("ff842d"))
} else {
colors.append(UIColor.colorWithHex("1ec2ff"))
}
if type == 1 {
if modelList[i].price_flag == 1 {
yValues.append(PieChartDataEntry(value: modelList[i].price_perctg, label: modelList[i].brand_name))
} else {
yValues.append(PieChartDataEntry(value: modelList[i].price_perctg, label: modelList[i].brand_name))
}
} else {
yValues.append(PieChartDataEntry(value: modelList[i].number_perctg, label: modelList[i].brand_name))
}
}
let dataSet = PieChartDataSet(values: yValues, label: "")
dataSet.valueLinePart1OffsetPercentage = 0.8 // 折線中第一段起始位置相對于區(qū)塊的偏移量, 數(shù)值越大, 折線距離區(qū)塊越遠
dataSet.valueLinePart1Length = 0.4 // 折線中第一段長度占比
dataSet.valueLinePart2Length = 0.6 // 折線中第二段長度最大占比
dataSet.valueLineWidth = 1 //折線的粗細
dataSet.valueLineColor = UIColor.gray //折線顏色
dataSet.xValuePosition = .outsideSlice //名稱位置
dataSet.yValuePosition = .insideSlice //數(shù)據(jù)位置
dataSet.drawIconsEnabled = false
dataSet.sliceSpace = 2.0 // 相鄰區(qū)塊之間的間距
dataSet.colors = colors // 設(shè)置區(qū)塊顏色
let pieChartData = PieChartData(dataSet: dataSet)
pieChartData.setValueFormatter(DigitValueFormatter())//設(shè)置百分比
pieChartData.setValueTextColor(UIColor.gray) //字體顏色為白色
pieChartData.setValueFont(UIFont.systemFont(ofSize: 11))//字體大小
pieGraphView.data = pieChartData // 將配置及數(shù)據(jù)添加到表中
很可惜,我們的需求是要百分比顯示在內(nèi)部校翔,數(shù)據(jù)顯示在外部陕见,圖例顯示品牌,還沒有找到很好的辦法,你有么友酱?我能想到的是圖例覆蓋一層view去展示......