上篇文章已经讲述了折线图的用法这边文章主要来谈饼状图。

其实Charts难的部分主要在于配置,所以同样主要说说他的配置。

pieGraphView.setExtraOffsets(left: 10, top: 0, right: 60, bottom: 0) // 饼状图距离边缘的间隙

pieGraphView.usePercentValuesEnabled = true // 是否根据所提供的数据, 将显示数据转换为百分比格式

pieGraphView.dragDecelerationEnabled = false // 拖拽饼状图后是否有惯性效果

// viewPie?.drawSliceTextEnabled = false // 是否显示区块文本

// pieGraphView.drawSlicesUnderHoleEnabled = false

pieGraphView.chartDescription?.enabled = false // 图形描述

pieGraphView.drawCenterTextEnabled = false // 是否显示中间文字

pieGraphView.drawHoleEnabled = true // 饼状图是否是空心

pieGraphView.holeRadiusPercent = 0.5 // 空心半径占比

pieGraphView.transparentCircleRadiusPercent = 0.52 //半透明空心半径占比

pieGraphView.rotationEnabled = true // 旋转指示

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

// 数据

var yValues: [PieChartDataEntry] = [] // 所占比例

var colors: [UIColor] = [] // 区块颜色

for i in 0..

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 // 折线中第一段起始位置相对于区块的偏移量, 数值越大, 折线距离区块越远

dataSet.valueLinePart1Length = 0.4 // 折线中第一段长度占比

dataSet.valueLinePart2Length = 0.6 // 折线中第二段长度最大占比

dataSet.valueLineWidth = 1 //折线的粗细

dataSet.valueLineColor = UIColor.gray //折线颜色

dataSet.xValuePosition = .outsideSlice //名称位置

dataSet.yValuePosition = .insideSlice //数据位置

dataSet.drawIconsEnabled = false

dataSet.sliceSpace = 2.0 // 相邻区块之间的间距

dataSet.colors = colors // 设置区块颜色

let pieChartData = PieChartData(dataSet: dataSet)

pieChartData.setValueFormatter(DigitValueFormatter())//设置百分比

pieChartData.setValueTextColor(UIColor.gray) //字体颜色为白色

pieChartData.setValueFont(UIFont.systemFont(ofSize: 11))//字体大小

pieGraphView.data = pieChartData // 将配置及数据添加到表中

很可惜,我们的需求是要百分比显示在内部,数据显示在外部,图例显示品牌,还没有找到很好的办法,你有么?我能想到的是图例覆盖一层view去展示......

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐