一厕诡、下載Charts后可以到里面的內(nèi)容
將下載后解壓后的整個文件夾復(fù)制到工程里
但是只導(dǎo)入
工程Charts.xcodeproj-->
二樱哼、配置
三、橋接
如果刪除了橋接文件骗露,需要重新創(chuàng)建的不僅僅要刪除橋接互拾,還要刪除路徑
其他配置
上面這兩個選項一般都是默認設(shè)置好的,不需要修改萧落,以防萬一可以看下践美。
刪除Bridging.swift里的
importFoundation
最后在用到的地方調(diào)入這個兩個文件
三、如何使用(很多地方都有變動铐尚,特別是X軸的設(shè)置)
3.1餅圖的創(chuàng)建和數(shù)據(jù)
- (PieChartView *)createPieChart
{
PieChartView * pieView = [[PieChartView alloc]initWithFrame:CGRectMake(0,0, kWidth,CGRectGetHeight(_contentView.frame))];
[pieViewsetExtraOffsetsWithLeft:5top:5right:5bottom:5];//餅狀圖距離邊緣的間隙pieView.usePercentValuesEnabled =YES;//是否根據(jù)所提供的數(shù)據(jù), 將顯示數(shù)據(jù)轉(zhuǎn)換為百分比格式
pieView.dragDecelerationEnabled =YES;//拖拽餅狀圖后是否有慣性效果pieView.drawSliceTextEnabled =NO;//是否顯示區(qū)塊文本
pieView.drawHoleEnabled =YES;//餅狀圖是否是空心pieView.noDataText =@"暫無數(shù)據(jù)";
pieView.holeRadiusPercent =0.5;//空心半徑占比pieView.holeColor = [UIColorclearColor];//空心顏色pieView.transparentCircleRadiusPercent =0.52;//半透明空心半徑占比pieView.transparentCircleColor = [UIColorcolorWithRed:210/255.0green:145/255.0blue:165/255.0alpha:0.3];//半透明空心的顏色pieView.descriptionText =@"";
pieView.descriptionTextAlign =NSTextAlignmentLeft;
pieView.descriptionFont = [UIFontboldSystemFontOfSize:15];
pieView.descriptionTextColor = [UIColordarkGrayColor];
pieView.centerText =@"餅圖";//圓中心字[pieViewanimateWithXAxisDuration:1.1easingOption:ChartEasingOptionEaseOutBack];
ChartLegend *legend = pieView.legend;
legend.enabled =YES;
legend.maxSizePercent =1;//圖例在餅狀圖中的大小占比, 這會影響圖例的寬高legend.formToTextSpace =5;//文本間隔
legend.font = [UIFontsystemFontOfSize:13];//字體大小legend.textColor = [UIColorgrayColor];//字體顏色
legend.position = ChartLegendPositionRightOfChartCenter ;//圖例在餅狀圖中的位置legend.form = ChartLegendFormSquare;//圖示樣式: 方形拨脉、線條、圓形legend.formSize =8;//圖示大小returnpieView;
}
- (PieChartData *)getPieChartData
{NSMutableArray*values = [[NSMutableArrayalloc] init];for(inti =0; i <5; i++) {NSString* str = [NSStringstringWithFormat:@"測試%d",i];
[valuesaddObject:[[PieChartDataEntry alloc]initWithValue:0.2label:str]];
}
PieChartDataSet *dataSet = [[PieChartDataSet alloc]initWithValues:valueslabel:@""];
dataSet.sliceSpace =1;
dataSet.valueTextColor = [UIColordarkGrayColor];
dataSet.valueLinePart1OffsetPercentage =0.85;//折線中第一段起始位置相對于區(qū)塊的偏移量, 數(shù)值越大, 折線距離區(qū)塊越遠
dataSet.xValuePosition = PieChartValuePositionInsideSlice;//名稱位置dataSet.yValuePosition = PieChartValuePositionOutsideSlice;//數(shù)據(jù)位置dataSet.drawValuesEnabled =YES;//是否繪制百分比dataSet.valueLinePart1Length =0.45;//折線中第一段長度占比dataSet.valueLinePart2Length =0.3;//折線中第二段長度最大占比
dataSet.valueLineWidth =1;//折線的粗細dataSet.valueTextColor = [UIColorbrownColor];
dataSet.valueLineColor = [UIColorbrownColor];//折線顏色NSMutableArray*colors = [[NSMutableArrayalloc] init];
[colorsaddObjectsFromArray:ChartColorTemplates.vordiplom];
[colorsaddObjectsFromArray:ChartColorTemplates.joyful];
[colorsaddObjectsFromArray:ChartColorTemplates.colorful];
[colorsaddObjectsFromArray:ChartColorTemplates.liberty];
[colorsaddObjectsFromArray:ChartColorTemplates.pastel];
[colorsaddObject:[UIColorcolorWithRed:51/255.f green:181/255.fblue:229/255.falpha:1.f]];
dataSet.colors = colors;
PieChartData *datas = [[PieChartData alloc]initWithDataSet:dataSet];NSNumberFormatter*pFormatter = [[NSNumberFormatteralloc] init];
pFormatter.numberStyle =NSNumberFormatterPercentStyle;
pFormatter.maximumFractionDigits =1;
pFormatter.multiplier = @1.f;
pFormatter.percentSymbol =@" %";
[datassetValueFormatter:[[ChartDefaultValueFormatter alloc]initWithFormatter:pFormatter]];
[datassetValueFont:[UIFontfontWithName:@"HelveticaNeue-Light"size:11.f]];returndatas;
}
3.2柱狀圖的創(chuàng)建和數(shù)據(jù)
- (BarChartView *)createBarChart
{//柱狀圖BarChartView * barChartView = [[BarChartView alloc] initWithFrame:CGRectMake(0,0, kWidth,CGRectGetHeight(_contentView.frame))];//基本樣式barChartView.backgroundColor = [UIColorwhiteColor];
barChartView.noDataText =@"暫無數(shù)據(jù)";//沒有數(shù)據(jù)時的文字提示barChartView.drawValueAboveBarEnabled =YES;//數(shù)值顯示在柱形的上面還是下面barChartView.drawBarShadowEnabled =NO;//是否繪制柱形的陰影背景//barChartView的交互設(shè)置barChartView.scaleYEnabled =NO;//取消Y軸縮放barChartView.doubleTapToZoomEnabled =YES;//取消雙擊縮放barChartView.dragEnabled =YES;//啟用拖拽圖表barChartView.dragDecelerationEnabled =YES;//拖拽后是否有慣性效果barChartView.dragDecelerationFrictionCoef =0.9;//拖拽后慣性效果的摩擦系數(shù)(0~1)宣增,數(shù)值越小玫膀,慣性越不明顯
barChartView.leftAxis.axisMaxValue =10;//設(shè)置Y軸的最大值//X軸樣式ChartXAxis *xAxis = barChartView.xAxis;
xAxis.axisLineWidth =1;//設(shè)置X軸線寬xAxis.labelPosition = XAxisLabelPositionBottom;//X軸的顯示位置,默認是顯示在上面的xAxis.drawGridLinesEnabled =NO;//不繪制網(wǎng)格線
xAxis.labelTextColor = [UIColorbrownColor];//label文字顏色xAxis.shouldGroupAccessibilityChildren =NO;
xAxis.granularity =1.0;//保證放大的時候X軸就一個值//左邊Y軸ChartYAxis *leftAxis = barChartView.leftAxis;
barChartView.rightAxis.enabled =NO;//不繪制右邊軸
leftAxis.forceLabelsEnabled =NO;//不強制繪制制定數(shù)量的
labelleftAxis.axisMinValue =0;//設(shè)置Y軸的最小值
leftAxis.inverted =NO;//是否將Y軸進行上下翻轉(zhuǎn)
leftAxis.axisLineWidth =0.5;//Y軸線寬
leftAxis.axisLineColor = [UIColorblackColor];//Y軸顏色leftAxis.labelCount =5;
leftAxis.forceLabelsEnabled =NO;//設(shè)置Y軸上標簽的樣式
leftAxis.labelPosition = YAxisLabelPositionOutsideChart;//label位置leftAxis.labelTextColor = [UIColorbrownColor];//文字顏色
leftAxis.labelFont = [UIFontsystemFontOfSize:10.0f];//文字字體體//設(shè)置Y軸上網(wǎng)格線的樣式
leftAxis.gridLineDashLengths = @[@3.0f, @3.0f];//設(shè)置虛線樣式的網(wǎng)格線leftAxis.gridColor = [UIColorcolorWithRed:200/255.0f green:200/255.0f blue:200/255.0f alpha:1];//網(wǎng)格線顏色
leftAxis.gridAntialiasEnabled =YES;//開啟抗鋸齒/*
在Y軸上添加限制線
ChartLimitLine *limitLine = [[ChartLimitLine alloc] initWithLimit:80 label:@"限制線"];
limitLine.lineWidth = 2;
limitLine.lineColor = [UIColor greenColor];
limitLine.lineDashLengths = @[@5.0f, @5.0f];//虛線樣式
limitLine.labelPosition = ChartLimitLabelPositionRightTop;//位置
[leftAxis addLimitLine:limitLine];//添加到Y(jié)軸上
leftAxis.drawLimitLinesBehindDataEnabled = YES;//設(shè)置限制線繪制在柱形圖的后面
*/barChartView.legend.enabled =NO;//不顯示圖例說明
barChartView.descriptionText =@"";returnbarChartView;
}
- (NSString*)stringForValue:(double)value axis:(ChartAxisBase *)axis
{//X軸上面需要顯示的數(shù)據(jù)return[NSStringstringWithFormat:@"測試%d",(int)value];
}
- (BarChartData *)getBarChartData
{//對應(yīng)Y軸上面需要顯示的數(shù)據(jù)NSMutableArray*yVals = [[NSMutableArrayalloc] init];for(inti =0; i <5; i++) {
BarChartDataEntry *entry = [[BarChartDataEntry alloc]initWithX:i yValues:@[@6] label:@""];
[yVals addObject:entry];
}//創(chuàng)建BarChartDataSet對象爹脾,其中包含有Y軸數(shù)據(jù)信息帖旨,以及可以設(shè)置柱形樣式BarChartDataSet * set1 = [[BarChartDataSet alloc]initWithValues:yVals];
set1.drawValuesEnabled =YES;//是否在柱形圖上面顯示數(shù)值
set1.highlightEnabled =NO;//點擊選中柱形圖是否有高亮效果,(雙擊空白處取消選中)
[set1 setColors:ChartColorTemplates.material];//設(shè)置柱形圖顏色//將BarChartDataSet對象放入數(shù)組中NSMutableArray* dataSets = [[NSMutableArrayalloc] init];
[dataSets addObject:set1];//創(chuàng)建BarChartData對象, 此對象就是barChartView需要最終數(shù)據(jù)對象
BarChartData * datas = [[BarChartData alloc] initWithDataSet:set1];
[datas setValueFont:[UIFontfontWithName:@"HelveticaNeue-Light"size:10.f]];//文字字體[datas setValueTextColor:[UIColororangeColor]];//文字顏色returndatas;
}
X軸的設(shè)置和以前不一樣了灵妨,是放到一個代理方法里面了解阅。
3.3折線圖圖 的創(chuàng)建和數(shù)據(jù)
- (LineChartView *)createLineChart{ LineChartView * lineChartView = [[LineChartView alloc]initWithFrame:CGRectMake(0,0, kWidth,CGRectGetHeight(_contentView.frame))];?
?lineChartView.noDataText =@"暫無數(shù)據(jù)";
//設(shè)置交互
lineChartView.scaleYEnabled =NO;//取消Y軸縮放lineChartView.doubleTapToZoomEnabled =YES;//取消雙擊縮放lineChartView.dragEnabled =YES;//啟用拖拽圖標lineChartView.dragDecelerationEnabled =YES;//拖拽后是否有慣性效果lineChartView.dragDecelerationFrictionCoef =0.9;//拖拽后慣性效果的摩擦系數(shù)(0~1),數(shù)值越小泌霍,慣性越不明顯
lineChartView.descriptionText =@"";
?lineChartView.xAxis.granularity =1.0;//保證放大的時候X軸就一個值lineChartView.legend.enabled =NO;//是否顯示圖例lineChartView.leftAxis.axisMaxValue =5;//設(shè)置Y軸的最大值//設(shè)置X軸ChartXAxis *xAxis = lineChartView.xAxis; xAxis.axisLineWidth =1;//設(shè)置X軸線寬
xAxis.labelPosition = XAxisLabelPositionBottom;//X軸的顯示位置货抄,默認是顯示在上面的xAxis.drawGridLinesEnabled =NO;//不繪制網(wǎng)格線
xAxis.spaceMax =0;//設(shè)置label間隔
xAxis.labelCount =12; xAxis.axisLineColor = [UIColorlightGrayColor]; xAxis.labelTextColor = [UIColordarkGrayColor];//label文字顏色//設(shè)置Y軸lineChartView.rightAxis.enabled =NO;//不繪制右邊軸
ChartYAxis *leftAxis = lineChartView.leftAxis;//獲取左邊Y軸
leftAxis.labelCount =5;//Y軸label數(shù)量,數(shù)值不一定朱转,如果forceLabelsEnabled等于YES, 則強制繪制制定數(shù)量的label, 但是可能不平均
leftAxis.forceLabelsEnabled =NO;//不強制繪制指定數(shù)量的
labelleftAxis.axisMinValue =0;//設(shè)置Y軸的最小值
leftAxis.inverted =NO;//是否將Y軸進行上下翻轉(zhuǎn)
leftAxis.axisLineWidth =1;//Y軸線寬
leftAxis.axisLineColor = [UIColorlightGrayColor];//Y軸顏色
leftAxis.labelPosition = YAxisLabelPositionOutsideChart;//label位置leftAxis.labelTextColor = [UIColordarkGrayColor];//文字顏色
leftAxis.labelFont = [UIFontsystemFontOfSize:10.0f];//文字字體//設(shè)置網(wǎng)格leftAxis.gridLineDashLengths = @[@3.0f, @3.0f];//設(shè)置虛線樣式的網(wǎng)格線leftAxis.gridColor = [UIColorcolorWithRed:200/255.0f green:200/255.0f blue:200/255.0f alpha:1];//網(wǎng)格線顏色
leftAxis.gridAntialiasEnabled =YES;//開啟抗鋸齒
returnlineChartView;
}
- (NSDictionary*)getLineChartData{
NSMutableArray* lines = [NSMutableArrayarray];//多條折線(本模塊就一條)
//設(shè)置X軸數(shù)據(jù)
NSMutableArray* xValues = [NSMutableArrayarray];for(inti =0; i <5; i++)
?{
?????[xValues addObject:[NSStringstringWithFormat:@"%d月",i+1]]; }//設(shè)置Y軸數(shù)據(jù) ? ? ? ? ? NSArray* numArr = @[@5,@2,@4,@3,@2];
NSMutableArray* yValues = [NSMutableArrayarray];
for(inti =0; i <5; i++)?
{
NSIntegernum = [numArr[i] integerValue];?
?ChartDataEntry *entry = [[ChartDataEntry alloc] initWithX:i y:num];
?[yValues addObject:entry];?
?}?
?LineChartDataSet * dataSet = [[LineChartDataSet alloc] initWithValues:yValues label:@""];
?dataSet.circleRadius =4;?
?dataSet.drawCircleHoleEnabled =YES;//描點是否空心
dataSet.circleHoleRadius =3;//描點空心半徑
dataSet.drawFilledEnabled =YES;//是否繪制線填充色
dataSet.fillColor = [UIColororangeColor];//繪制線填充色
dataSet.circleColors = @[[UIColorwhiteColor]];//圓點顏色
dataSet.circleHoleColor = [UIColororangeColor];//圓點空心顏色
[dataSet setColor:[UIColororangeColor]];?
?dataSet.mode = LineChartModeLinear;//折線樣式
dataSet.drawValuesEnabled =NO;
?[lines addObject:dataSet];
?LineChartData *data = [[LineChartData alloc] initWithDataSets:lines];
return@{@"XValue":xValues,@"Data":data};}
X軸設(shè)置通過這個方法
lineCharView.xAxis.valueFormatter =[[ChartIndexAxisValueFormatter alloc] initWithValues:[dic objectForKey:@"XValue"]];//設(shè)置X軸
lineCharView.data = [dic objectForKey:@"Data"];
Demo鏈接:https://pan.baidu.com/s/1mirDFgk
提刃返亍:qb4x