前面幾篇文章是《charts框架的集成》《charts柱狀圖》《charts折線圖》想括,建議大家結(jié)合起來看,這樣更詳細(xì)烙博。
傳送門:
下方是餅狀圖效果圖:
主要核心代碼如下瑟蜈,大家可以自己需要修改下方配置:
// 初始化 ??
_pieChartView = [[PieChartView alloc] initWithFrame:CGRectZero]; ?_pieChartView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.pieChartView];
self.pieChartView.frame = CGRectMake(0, 64, kSCREEN_WIDTH, kSCREEN_HEIGHT -64);
-------------------- 餅狀圖的配置----------------------
// 動(dòng)畫
[self.pieChartView animateWithXAxisDuration:1];
[self.pieChartView animateWithYAxisDuration:1];
// 餅狀圖距離邊緣的間隙
[_pieChartView setExtraOffsetsWithLeft:35 top:10 right:35 bottom:10]; ?
// 是否根據(jù)所提供的數(shù)據(jù), 將顯示數(shù)據(jù)轉(zhuǎn)換為百分比格式
_pieChartView.usePercentValuesEnabled = NO;
// 拖拽餅狀圖后是否有慣性效果
_pieChartView.dragDecelerationEnabled = YES
// 是否顯示區(qū)塊文本
_pieChartView.drawSliceTextEnabled = NO;
// 餅狀圖是否是空心
_pieChartView.drawHoleEnabled = NO;
// 空心半徑占比
_pieChartView.holeRadiusPercent = 0.8;
// 空心顏色
?_pieChartView.holeColor = [UIColor clearColor]; ?
// 半透明空心半徑占比
_pieChartView.transparentCircleRadiusPercent = 0.52;
// 半透明空心的顏色?
_pieChartView.transparentCircleColor = [UIColor colorWithRed:210/255.0 green:145/255.0 blue:165/255.0 alpha:0.3];
// ?設(shè)置空心文字
? ? ? ? if (_pieChartView.isDrawHoleEnabled == YES) {
// 是否顯示中間文字
? ? ? ? ? ?_pieChartView.drawCenterTextEnabled = YES;
// ?普通文本
// ?中間文字 ??
? ? ? ? ? _pieChartView.centerText = @"西瓜";
// ?富文本
? ? ? ?NSMutableAttributedString *centerText = [[NSMutableAttributedString alloc] initWithString:@"西瓜詳情"];
? ? ? ? ? [centerText setAttributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:18],
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?NSForegroundColorAttributeName: HEXCOLOR(0x444444)}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? range:NSMakeRange(0, centerText.length)];
? ? ? ? ? _pieChartView.centerAttributedText = centerText;
? ? }
// ? 設(shè)置餅狀圖描述
// ?是否開啟描述label
?_pieChartView.chartDescription.enabled = NO;?
?_pieChartView.descriptionFont = [UIFont systemFontOfSize:10];
?_pieChartView.descriptionTextColor = [UIColor grayColor];
-------------------------legend圖例的屬性配置-------------------------------
// 是否開啟圖例
_pieChartView.legend.enabled = NO;
// 圖例在餅狀圖中的大小占比, 這會(huì)影響圖例的寬高
_pieChartView.legend.maxSizePercent = 1;
?// ?文本間隔
_pieChartView.legend.formToTextSpace = 5;
// ? 圖例間隔大小
?_pieChartView.legend.yEntrySpace = 5;//10;
?_pieChartView.legend.xEntrySpace = 6;
// ? 字體大小
?_pieChartView.legend.font = [UIFont systemFontOfSize:20];
// ?字體顏色
?_pieChartView.legend.textColor = [UIColor grayColor];
// ? 圖例在餅狀圖中的位置
?_pieChartView.legend.position = ChartLegendPositionBelowChartCenter;
// ? ?圖示樣式: 方形、線條渣窜、圓形
?_pieChartView.legend.form = ChartLegendFormSquare; ?
// ? ?圖示大小
?_pieChartView.legend.formSize =20;
------------------------餅狀圖的數(shù)據(jù)配置----------------------
// ? 每個(gè)區(qū)塊的金額數(shù) ??
NSMutableArray* moneyArray =[NSMutableArrayarrayWithArray:@[@33.33,@33,@12,@50.66,@10]];
?// ?每個(gè)區(qū)塊的顏色
NSMutableArray *colors = [[NSMutableArray alloc] init];
?[colors addObject:[UIColor colorWithRed:141/255.f green:116/255.f blue:244/255.f alpha:1]];
? [colors addObject:[UIColor colorWithRed:245/255.f green:101/255.f blue:103/255.f alpha:1]];
?// 每個(gè)區(qū)塊的數(shù)據(jù)
?NSMutableArray *yVals = [[NSMutableArray alloc] init];
? ? for(inti =0; i < moneyArray.count; i++) {
? ? ? ? doublerandomVal = [moneyArray[i]doubleValue];
? ? ? ? BarChartDataEntry *entry = [[BarChartDataEntry alloc] initWithValue:randomVal xIndex:i];
? ? ? ? ChartDataEntry * entry = [[ChartDataEntry alloc] initWithValue:randomVal xIndex:i];
? ? ? ? ChartDataEntry* entry = [[ChartDataEntryalloc]initWithX:iy:randomVal];
? ? ? ? [yValsaddObject:entry];
? ? }
?PieChartDataSet *dataSet = [[PieChartDataSet alloc] initWithValues:yVals label:@""];
// 是否繪制顯示數(shù)據(jù)
dataSet.drawValuesEnabled = NO;
// 區(qū)塊顏色
dataSet.colors= colors;?
// 相鄰區(qū)塊之間的間距?
dataSet.sliceSpace=3;
// 選中區(qū)塊時(shí), 放大的半徑
dataSet.selectionShift=12;
// 名稱位置
dataSet.xValuePosition = PieChartValuePositionInsideSlice;
// 數(shù)據(jù)位置
// 數(shù)據(jù)與區(qū)塊之間的用于指示的折線樣式
dataSet.yValuePosition = PieChartValuePositionOutsideSlice;?
// 折線中第一段起始位置相對于區(qū)塊的偏移量, 數(shù)值越大, 折線距離區(qū)塊越遠(yuǎn)
dataSet.valueLinePart1OffsetPercentage = 0.85;
// 折線中第一段長度占比
dataSet.valueLinePart1Length = 0.4;
// 折線中第二段長度最大占比
dataSet.valueLinePart2Length = 0.6;
// 折線的粗細(xì)
dataSet.valueLineWidth=.6;
// 折線顏色
dataSet.valueLineColor= [UIColororange Color];
dataSet.valueLineVariableLength = YES;
dataSet.valueTextColor= [UIColorblackColor];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = kCFNumberFormatterCurrencyStyle;//NSNumberFormatterPercentStyle;
?[formattersetPositiveFormat:@"###,##0.00;"];
// 小數(shù)位數(shù)
formatter.maximumFractionDigits = 2;
formatter.multiplier=@1.f;
formatter.paddingPosition = kCFNumberFormatterPadBeforeSuffix;
// 數(shù)據(jù)設(shè)置上去
_pieChartView.data ?=data;
如果大家想要demo铺根,或者有問題想問可以加QQ交流群:191409807。