效果圖先上:
前言:
? ? ? ? 需求是要出一個數(shù)據(jù)分析的餅狀形數(shù)據(jù)分析肝谭,然后找了N多demo。最后發(fā)現(xiàn)charts的效果比其他的都要更強勢一些赖晶,更優(yōu)美一些俄占,所以就選擇了這個框架 ?Charts的git地址鏈接
感受:
? ? ? ? 這玩意是個折磨人的小妖精,中間反復(fù)就不多提了直接說問題和注意事項:
? ? ? ? ? ? ? ? xcode10.0 下操作
第一步:集成charts葡缰,這里放上:參考地址鏈接亏掀,遇到問題的再補充一下
這里要提一下目錄千萬不要錯了,如果錯了就gg
然后繼續(xù)
? ??
因為項目是OC泛释,而Charts是swift寫的滤愕,所以要橋接文件
在橋接文件里面寫上 ?@importCharts;
第二步:配置搞好了那么就開始寫代碼
? ? 在你所需使用的vc里導(dǎo)入鏈接文件
寫屬性
#define BgColor [UIColor colorWithRed:230/255.0f green:253/255.0f blue:253/255.0f alpha:1]#define kScreenWidth [UIScreen mainScreen].bounds.size.width#define kScreenHeight [UIScreen mainScreen].bounds.size.height
@property (nonatomic, strong) PieChartView *pieChartView;
@property (nonatomic, strong) PieChartData *data;
設(shè)置餅狀圖
-(void)creasePieChartView{
? ? self.pieChartView = [[PieChartView alloc] init];
? ? self.pieChartView.backgroundColor = BgColor;
? ? [self.view addSubview:self.pieChartView];
? ? [self.pieChartView mas_makeConstraints:^(MASConstraintMaker *make) {
? ? ? ? make.size.mas_equalTo(CGSizeMake(kScreenWidth, 200));
? ? ? ? make.top.equalTo(104);
? ? ? ? make.left.equalTo(0);
? ? }];
? ? [self.pieChartView setExtraOffsetsWithLeft:5 top:10 right:70 bottom:5];//餅狀圖距離邊緣的間隙
? ? self.pieChartView.usePercentValuesEnabled = YES;//是否根據(jù)所提供的數(shù)據(jù), 將顯示數(shù)據(jù)轉(zhuǎn)換為百分比格式
? ? self.pieChartView.dragDecelerationEnabled = YES;//拖拽餅狀圖后是否有慣性效果
? ? self.pieChartView.drawSlicesUnderHoleEnabled= NO;//
? ? self.pieChartView.chartDescription.enabled = NO;//餅狀圖描述
? ? self.pieChartView.drawHoleEnabled = YES;//餅狀圖是否是空心
? ? self.pieChartView.holeRadiusPercent = 0.5;//空心半徑占比
? ? self.pieChartView.holeColor = [UIColor grayColor];//空心顏色
? ? self.pieChartView.transparentCircleRadiusPercent = 0.55;//半透明空心半徑占比
? ? self.pieChartView.transparentCircleColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.3];//半透明空心的顏色
? ? if (self.pieChartView.isDrawHoleEnabled == YES) {
? ? ? ? self.pieChartView.drawCenterTextEnabled = YES;//是否顯示中間文字
? ? ? ? //普通文本
?? ? ? ? self.pieChartView.centerText = @"數(shù)據(jù)分析";//中間文字
? ? ? ? //富文本
//? ? ? ? NSMutableAttributedString *centerText = [[NSMutableAttributedString alloc] initWithString:@"餅狀圖bbb\n餅狀圖aaa"];
//? ? ? ? [centerText setAttributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:16],
//? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSForegroundColorAttributeName: [UIColor orangeColor]}
//? ? ? ? ? ? ? ? ? ? ? ? ? ? range:NSMakeRange(0, centerText.length)];
//? ? ? ? self.pieChartView.centerAttributedText = centerText;
? ? }
? ? self.pieChartView.legend.maxSizePercent = 1;//圖例在餅狀圖中的大小占比, 這會影響圖例的寬高
? ? self.pieChartView.legend.formToTextSpace = 5;//文本間隔
? ? self.pieChartView.legend.font = [UIFont systemFontOfSize:10];//字體大小
? ? self.pieChartView.legend.textColor = [UIColor grayColor];//字體顏色
? ? // self.pieChartView.legend.position = ChartLegendPositionBelowChartCenter;//圖例在餅狀圖中的位置
? ? self.pieChartView.legend.horizontalAlignment = ChartLegendHorizontalAlignmentRight;
? ? self.pieChartView.legend.verticalAlignment = ChartLegendVerticalAlignmentTop;
? ? self.pieChartView.legend.orientation = ChartLegendOrientationVertical;
? ? self.pieChartView.legend.form = ChartLegendFormCircle;//圖示樣式: 方形、線條怜校、圓形
? ? self.pieChartView.legend.formSize = 8;//圖示大小
? ? [self.pieChartView setRotationEnabled:true];//禁止拖拽
? ? [selfsetData];
}
最后設(shè)置給餅狀圖設(shè)置數(shù)據(jù)
-(PieChartData*)setData{
? ? //每個區(qū)塊的數(shù)據(jù)
? ? //每個區(qū)塊的名稱或描述
? ? NSArray *arr =@[@"12.3",@"10",@"12.7",@"25.6",@"24.4"]; //[[NSMutableArray alloc]init];
? ? NSArray *nameArr = @[@"通碼業(yè)務(wù)",@"銀行充值",@"手機充值",@"Q幣充值",@"包裹代收"];//[[NSMutableArray alloc]init];
//? ? for (pieChart *model in _pieCharModel.pieChart) {
//? ? ? ? [arr addObject:model.totalamt];//@[@"8.54",@"3.26",@"2"];
//? ? ? ? [nameArr addObject:model.typename];
//? ? }
? ? NSMutableArray *values = [[NSMutableArray alloc] init];
? ? // IMPORTANT: In a PieChart, no values (Entry) should have the same xIndex (even if from different DataSets), since no values can be drawn above each other.
? ? for(inti =0; i < arr.count; i++){
? ? ? ? NSString* aaa = arr[i];
? ? ? ? doublebb = aaa.doubleValue;
? ? ? ? [valuesaddObject:[[PieChartDataEntryalloc]initWithValue: bblabel: nameArr[i]]];
? ? }
? ? //dataSet
? ? PieChartDataSet*dataSet = [[PieChartDataSetalloc]initWithValues:valueslabel:@""];
? ? dataSet.drawValuesEnabled = YES;//是否繪制顯示數(shù)據(jù)
? ? NSMutableArray *colors = [[NSMutableArray alloc] init];
? ? [colorsaddObjectsFromArray:ChartColorTemplates.vordiplom];
? ? [colorsaddObjectsFromArray:ChartColorTemplates.joyful];
? ? [colorsaddObjectsFromArray:ChartColorTemplates.colorful];
? ? [colorsaddObjectsFromArray:ChartColorTemplates.liberty];
? ? [colorsaddObjectsFromArray:ChartColorTemplates.pastel];
? ? [colorsaddObject:[UIColor colorWithRed:51/255.f green:181/255.f blue:229/255.f alpha:1.f]];
? ? dataSet.colors= colors;//區(qū)塊顏色
? ? dataSet.sliceSpace=3;//相鄰區(qū)塊之間的間距
? ? dataSet.selectionShift=8;//選中區(qū)塊時, 放大的半徑
? ? dataSet.xValuePosition = PieChartValuePositionInsideSlice;//名稱位置
? ? dataSet.yValuePosition = PieChartValuePositionOutsideSlice;//數(shù)據(jù)位置
? ? //數(shù)據(jù)與區(qū)塊之間的用于指示的折線樣式
? ? dataSet.valueLinePart1OffsetPercentage = 0.85;//折線中第一段起始位置相對于區(qū)塊的偏移量, 數(shù)值越大, 折線距離區(qū)塊越遠
? ? dataSet.valueLinePart1Length = 0.5;//折線中第一段長度占比
? ? dataSet.valueLinePart2Length = 0.4;//折線中第二段長度最大占比
? ? dataSet.valueLineWidth=1;//折線的粗細
? ? dataSet.valueLineColor= [UIColorbrownColor];//折線顏色
? ? //data
? ? PieChartData*data = [[PieChartDataalloc]initWithDataSet:dataSet];
? ? NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
? ? formatter.numberStyle = NSNumberFormatterPercentStyle;
? ? formatter.maximumFractionDigits = 1;//小數(shù)位數(shù)
? ? formatter.multiplier=@1.f;
? ? [datasetValueFormatter:[[ChartDefaultValueFormatter alloc] initWithFormatter:formatter]];//設(shè)置顯示數(shù)據(jù)格式
? ? [datasetValueTextColor:[UIColor brownColor]];
? ? [datasetValueFont:[UIFontsystemFontOfSize:10]];
? ? self.pieChartView.data = data;
? ? [self.pieChartView setNeedsDisplay];
? ? returndata;
}