導(dǎo)語(yǔ):
這是一款可以制作各種類型的圖表庫(kù)入偷,折線圖,柱狀圖械哟,圓狀圖盯串,餅狀圖,同時(shí)伴有動(dòng)畫效果戒良,代碼簡(jiǎn)潔易懂体捏。
1.安裝
推薦使用cocoapods, 即 pod "PNChart",導(dǎo)入頭文件 #import "PNChart.h"
當(dāng)然你也可以手動(dòng)導(dǎo)入糯崎,需要添加系統(tǒng)依賴庫(kù):
Foundation.framework
UIKit.framework
CoreGraphics.framework
QuartzCore.framework
2.使用
1.折線圖
使用的類為PNLineChart几缭,屬性比較多,以下幾種類型的圖沃呢,屬性就不一一介紹了年栓,可以參考著折線圖
- (void)makeLineChart{
PNLineChart *lineChart = [[PNLineChartalloc] initWithFrame:CGRectMake(0, 100, SCREEN_WIDTH, 200)];
lineChart.yFixedValueMax =300;
lineChart.yFixedValueMin =0;
// lineChart.yLabelNum = 2;//設(shè)置Y軸有幾個(gè)等級(jí)數(shù)值,默認(rèn)自動(dòng)計(jì)算
//是否顯示X軸數(shù)值
lineChart.showLabel = YES;
//是否顯示Y軸的數(shù)值
lineChart.showGenYLabels = YES;
//是否顯示橫向曲線薄霜, Y軸網(wǎng)絡(luò)線
lineChart.showYGridLines = YES;
//是否顯示平滑的曲線
lineChart.showSmoothLines = NO;
//是否顯示 xy 坐標(biāo)軸
lineChart.showCoordinateAxis = YES;
//動(dòng)畫
lineChart.displayAnimated =YES;
//軸的顏色
lineChart.axisColor = [UIColor blueColor];
//軸的寬度
lineChart.axisWidth =2.0f;
//縱坐標(biāo)上是否顯示小數(shù)
lineChart.thousandsSeparator =YES;
//設(shè)置Y軸坐標(biāo)值的顏色
lineChart.yLabelColor = [UIColor redColor];
//x軸單位
lineChart.xUnit =@"月份";
lineChart.yUnit =@"銷量";
//設(shè)置X軸上的坐標(biāo)內(nèi)容
[lineChart setXLabels:@[@"1月",@"2月",@"3月",@"4月",@"5月"]];
//line Chart No 1
NSArray *dataAry1 =@[@"60.1", @"160.1", @"126.4",@"262.2", @"186.2"];
PNLineChartData *data1 = [PNLineChartDatanew];
data1.color = PNYellow;
// data1.dataTitle = @"Helloworld";
//設(shè)置點(diǎn)的樣式
data1.inflexionPointStyle = PNLineChartPointStyleCircle;
data1.inflexionPointColor = [UIColorpurpleColor];
//坐標(biāo)值是否顯示
data1.showPointLabel =YES;
//坐標(biāo)值顯示的顏色
data1.pointLabelColor = [UIColor redColor];
//坐標(biāo)值的字體大小
data1.pointLabelFont = [UIFontsystemFontOfSize:12];
//坐標(biāo)值顯示幾位數(shù)
data1.pointLabelFormat =@"%1.1f";
//設(shè)置折線有幾個(gè)值
data1.itemCount = lineChart.xLabels.count;
data1.getData = ^PNLineChartDataItem*(NSUInteger item) {
CGFloat yValue = [dataAry1[item] floatValue];
//設(shè)置X軸對(duì)應(yīng)的Y軸的值
return[PNLineChartDataItem dataItemWithY:yValue];
};
//Line Chart No 2
NSArray *dataAry2 =@[@"20.1", @"280.1", @"102.4",@"202.2", @"49.3"];
PNLineChartData *data2 = [PNLineChartDatanew];
data2.color = PNTwitterColor;
data2.itemCount = lineChart.xLabels.count;
data2.getData = ^PNLineChartDataItem*(NSUInteger item) {
CGFloat yValue = [dataAry2[item]floatValue];
return[PNLineChartDataItem dataItemWithY:yValue];
};
// lineChart.delegate = self;
//注意:此句要在添加圖例之前寫
lineChart.chartData =@[data1, data2];
[self.view addSubview: lineChart];
//添加圖例
data1.dataTitle =@"蘋果銷量";
data2.dataTitle =@"香蕉銷量";
//橫向顯示
lineChart.legendStyle =PNLegendItemStyleStacked;
lineChart.legendFontColor = [UIColorblackColor];
lineChart.legendFont = [UIFontsystemFontOfSize:25.0];
//圖例所在位置
UIView *legend = [lineChartgetLegendWithMaxWidth:200];
// legend.backgroundColor =[UIColor redColor];
[legend setFrame:CGRectMake(10, 350,
legend.frame.size.width, legend.frame.size.height)];
//顯示比例
lineChart.hasLegend =YES;
//顯示位置
lineChart.legendPosition =PNLegendPositionBottom;
//繪制出來
[lineChart strokeChart];
[self.view addSubview: legend];
}
2.圓狀圖
- (void)makeCircleChart
{
/*參數(shù):
clocwise:逆時(shí)針還是順時(shí)針
shadow:剩下的百分?jǐn)?shù)現(xiàn)顯示的顏色
overrideLineWidth:寬度
*/
PNCircleChart *circleChart =[[PNCircleChart alloc] initWithFrame:CGRectMake(0,100, SCREEN_WIDTH, 250)
total:@100
current:@10
clockwise:YES
shadow:YESshadowColor:[UIColor grayColor]
displayCountingLabel:YES
overrideLineWidth:@10];
circleChart.chartType =PNChartFormatTypePercent;
circleChart.strokeColor = [UIColorgreenColor];
circleChart.duration =3;//進(jìn)度條持續(xù)時(shí)間
[circleChart strokeChart];
[self.view addSubview: circleChart];
}
3.柱狀圖
- (void)makeBarChart{
PNBarChart *barChart = [[PNBarChart alloc]initWithFrame:CGRectMake(0, 100, SCREEN_WIDTH, 250)];
//是否顯示xy軸的數(shù)字
barChart.showLabel =YES;
//是否顯示水平線某抓,把柱子壓低上移了
// barChart.showLevelLine = YES;
//是否顯示xy軸
barChart.showChartBorder =YES;
//是否顯示柱子上的數(shù)值
barChart.isShowNumbers =YES;
//立體顯示
barChart.isGradientShow =YES;
//設(shè)置柱子的圓角
barChart.barRadius =5;
barChart.labelTextColor = [UIColorblueColor];
// barChart.xLabelWidth = 10.f;
barChart.yChartLabelWidth =10;
barChart.chartMarginLeft =10;
barChart.chartMarginRight =10;
barChart.chartMarginTop =5;
barChart.chartMarginBottom =10;
barChart.labelMarginTop =5.0;//X坐標(biāo)刻度的上邊距
//設(shè)置bar Color
barChart.strokeColor = [UIColor redColor];
barChart.xLabels =@[@"1", @"2",@"3", @"4",@"5"];
barChart.yValues =@[@"20", @"36",@"78", @"60",@"92"];
barChart.yLabelFormatter = ^NSString*(CGFloat yLabelValue) {
return[NSString stringWithFormat:@"%f", yLabelValue];
};
//開始繪制
[barChart strokeChart];
[self.view addSubview:barChart];
}
4.餅狀圖
- (void)makePieChart{
NSArray*items = @[
[PNPieChartDataItem dataItemWithValue:30 color:PNPinkGrey description:@"cat"],
[PNPieChartDataItem dataItemWithValue:20 color:PNDarkBlue description:@"pig"],
[PNPieChartDataItem dataItemWithValue:40 color:PNRed description:@"dog"]];
PNPieChart*pieChart = [[PNPieChart alloc] initWithFrame:CGRectMake(20, 100, 300, 300) items:items];
// pieChart.delegate = self;
pieChart.descriptionTextColor= [UIColor yellowColor];
pieChart.descriptionTextFont= [UIFont fontWithName:@"Avenir-Medium" size:20];
//陰影顏色
pieChart.descriptionTextShadowColor= [UIColor redColor];
//顯示實(shí)際數(shù)值,不顯示實(shí)際比例
pieChart.showAbsoluteValues= NO;
//只顯示數(shù)值纸兔,不顯示內(nèi)容描述
pieChart.showOnlyValues= NO;
pieChart.innerCircleRadius= 0;
pieChart.outerCircleRadius= 0;
[pieChartstrokeChart];
//加到父視圖上
[self.view addSubview:pieChart];
//顯示比例
pieChart.hasLegend= YES;
//橫向顯示
pieChart.legendStyle= PNLegendItemStyleSerial;
pieChart.legendFont= [UIFont boldSystemFontOfSize:20];
//顯示位置
pieChart.legendPosition= PNLegendPositionTop;
//獲得圖例,當(dāng)橫向排布不下另起一行
UIView*legend = [pieChart getLegendWithMaxWidth:100];
legend.frame= CGRectMake(100,30, legend.bounds.size.width, legend.bounds.size.height);
[self.view addSubview:legend];
}