PNChart是一個簡單漂亮的動畫圖表庫,Piner和CoinsMan的 iOS 客戶端中使用了這個框架。你也可以查看 Swift 版本(開源鏈接:https://github.com/kevinzhow/PNChart-Swift)。

要求
PNChart 適用于 iOS 7.0 或更高版本,與 ARC 項目兼容。如果需要支持 iOS 6 ,請使用 0.8.1 版本之前的 PNChart 。注意 0.8.2 版本僅支持 iOS 8.0+ ,0.8.3 及更新版本支持 iOS 7.0+ 。
PNChart 依賴于下列框架,這些框架已經(jīng)嵌入了 Xcode 開發(fā)工具:
Foundation.framework
UIKit.framework
CoreGraphics.framework
QuartzCore.framework
你需要 LLVM 3.0 或更高版本來建立 PNChart 。
安裝
通過CocoaPods安裝(推薦):
1、在你的 Podfile 文件中添加pod 'PNChart'。
2、運行pod install進行安裝。
3、按需導入頭文件#import "PNChart.h"。
手動安裝:
拷貝PNChart文件夾到你的工程中。
使用

Objective-C
27#import "PNChart.h"
//For Line Chart
PNLineChart*lineChart=[[PNLineChartalloc]initWithFrame:CGRectMake(0,135.0,SCREEN_WIDTH,200.0)];
[lineChartsetXLabels:@[@"SEP 1",@"SEP 2",@"SEP 3",@"SEP 4",@"SEP 5"]];
// Line Chart No.1
NSArray*data01Array=@[@60.1,@160.1,@126.4,@262.2,@186.2];
PNLineChartData*data01=[PNLineChartDatanew];
data01.color=PNFreshGreen;
data01.itemCount=lineChart.xLabels.count;
data01.getData=^(NSUIntegerindex){
CGFloatyValue=[data01Array[index]floatValue];
return[PNLineChartDataItemdataItemWithY:yValue];
};
// Line Chart No.2
NSArray*data02Array=@[@20.1,@180.1,@26.4,@202.2,@126.2];
PNLineChartData*data02=[PNLineChartDatanew];
data02.color=PNTwitterColor;
data02.itemCount=lineChart.xLabels.count;
data02.getData=^(NSUIntegerindex){
CGFloatyValue=[data02Array[index]floatValue];
return[PNLineChartDataItemdataItemWithY:yValue];
};
lineChart.chartData=@[data01,data02];
[lineChartstrokeChart];

Objective-C
#import "PNChart.h"
//For BarC hart
PNBarChart*barChart=[[PNBarChartalloc]initWithFrame:CGRectMake(0,135.0,SCREEN_WIDTH,200.0)];
[barChartsetXLabels:@[@"SEP 1",@"SEP 2",@"SEP 3",@"SEP 4",@"SEP 5"]];
[barChartsetYValues:@[@1,@10,@2,@6,@3]];
[barChartstrokeChart];

Objective-C
#import "PNChart.h"
//For Circle Chart
PNCircleChart*circleChart=[[PNCircleChartalloc]initWithFrame:CGRectMake(0,80.0,SCREEN_WIDTH,100.0)total:[NSNumbernumberWithInt:100]current:[NSNumbernumberWithInt:60]clockwise:NOshadow:NO];
circleChart.backgroundColor=[UIColorclearColor];
[circleChartsetStrokeColor:PNGreen];
[circleChartstrokeChart];

```Objective-C
# import "PNChart.h"
//For Pie Chart
NSArray*items=@[[PNPieChartDataItemdataItemWithValue:10color:PNRed],
[PNPieChartDataItemdataItemWithValue:20color:PNBluedescription:@"WWDC"],
[PNPieChartDataItemdataItemWithValue:40color:PNGreendescription:@"GOOL I/O"],
];
PNPieChart*pieChart=[[PNPieChartalloc]initWithFrame:CGRectMake(40.0,155.0,240.0,240.0)items:items];
pieChart.descriptionTextColor=[UIColorwhiteColor];
pieChart.descriptionTextFont=[UIFontfontWithName:@"Avenir-Medium"size:14.0];
[pieChartstrokeChart];

```Objective-C
# import "PNChart.h"
//For Scatter Chart
PNScatterChart*scatterChart=[[PNScatterChartalloc]initWithFrame:CGRectMake(SCREEN_WIDTH/6.0-30,135,280,200)];
[scatterChartsetAxisXWithMinimumValue:20andMaxValue:100toTicks:6];
[scatterChartsetAxisYWithMinimumValue:30andMaxValue:50toTicks:5];
NSArray*data01Array=[selfrandomSetOfObjects];
PNScatterChartData*data01=[PNScatterChartDatanew];
data01.strokeColor=PNGreen;
data01.fillColor=PNFreshGreen;
data01.size=2;
data01.itemCount=[[data01ArrayobjectAtIndex:0]count];
data01.inflexionPointStyle=PNScatterChartPointStyleCircle;
__blockNSMutableArray*XAr1=[NSMutableArrayarrayWithArray:[data01ArrayobjectAtIndex:0]];
__blockNSMutableArray*YAr1=[NSMutableArrayarrayWithArray:[data01ArrayobjectAtIndex:1]];
data01.getData=^(NSUIntegerindex){
CGFloatxValue=[[XAr1objectAtIndex:index]floatValue];
CGFloatyValue=[[YAr1objectAtIndex:index]floatValue];
return[PNScatterChartDataItemdataItemWithX:xValueAndWithY:yValue];
};
[scatterChartsetup];
self.scatterChart.chartData=@[data01];
/***
this is for drawing line to compare
CGPoint start = CGPointMake(20, 35);
CGPoint end = CGPointMake(80, 45);
[scatterChart drawLineFromPoint:start ToPoint:end WithLineWith:2 AndWithColor:PNBlack];
***/
scatterChart.delegate=self;
```
圖例
PNChart 允許在折線圖和餅狀圖中添加圖例,圖例可以豎向堆疊布置或者橫向并列布置。


Objective-C
#import "PNChart.h"
//For Line Chart
//Add Line Titles for the Legend
data01.dataTitle=@"Alpha";
data02.dataTitle=@"Beta Beta Beta Beta";
//Build the legend
self.lineChart.legendStyle=PNLegendItemStyleSerial;
self.lineChart.legendFontSize=12.0;
UIView*legend=[self.lineChartgetLegendWithMaxWidth:320];
//Move legend to the desired position and add to view
[legendsetFrame:CGRectMake(100,400,legend.frame.size.width,legend.frame.size.height)];
[self.viewaddSubview:legend];
//For Pie Chart
//Build the legend
self.pieChart.legendStyle=PNLegendItemStyleStacked;
self.pieChart.legendFontSize=12.0;
UIView*legend=[self.pieChartgetLegendWithMaxWidth:200];
//Move legend to the desired position and add to view
[legendsetFrame:CGRectMake(130,350,legend.frame.size.width,legend.frame.size.height)];
[self.viewaddSubview:legend];
更新數(shù)據(jù)
實時更新數(shù)據(jù)也非常簡單。
Objective-C
if([self.titleisEqualToString:@"Line Chart"]){
// Line Chart #1
NSArray*data01Array=@[@(arc4random()%300),@(arc4random()%300),@(arc4random()%300),@(arc4random()%300),@(arc4random()%300),@(arc4random()%300),@(arc4random()%300)];
PNLineChartData*data01=[PNLineChartDatanew];
data01.color=PNFreshGreen;
data01.itemCount=data01Array.count;
data01.inflexionPointStyle=PNLineChartPointStyleTriangle;
data01.getData=^(NSUIntegerindex){
CGFloatyValue=[data01Array[index]floatValue];
return[PNLineChartDataItemdataItemWithY:yValue];
};
// Line Chart #2
NSArray*data02Array=@[@(arc4random()%300),@(arc4random()%300),@(arc4random()%300),@(arc4random()%300),@(arc4random()%300),@(arc4random()%300),@(arc4random()%300)];
PNLineChartData*data02=[PNLineChartDatanew];
data02.color=PNTwitterColor;
data02.itemCount=data02Array.count;
data02.inflexionPointStyle=PNLineChartPointStyleSquare;
data02.getData=^(NSUIntegerindex){
CGFloatyValue=[data02Array[index]floatValue];
return[PNLineChartDataItemdataItemWithY:yValue];
};
[self.lineChartsetXLabels:@[@"DEC 1",@"DEC 2",@"DEC 3",@"DEC 4",@"DEC 5",@"DEC 6",@"DEC 7"]];
[self.lineChartupdateChartData:@[data01,data02]];
}
elseif([self.titleisEqualToString:@"Bar Chart"])
{
[self.barChartsetXLabels:@[@"Jan 1",@"Jan 2",@"Jan 3",@"Jan 4",@"Jan 5",@"Jan 6",@"Jan 7"]];
[self.barChartupdateChartData:@[@(arc4random()%30),@(arc4random()%30),@(arc4random()%30),@(arc4random()%30),@(arc4random()%30),@(arc4random()%30),@(arc4random()%30)]];
}
elseif([self.titleisEqualToString:@"Circle Chart"])
{
[self.circleChartupdateChartByCurrent:@(arc4random()%100)];
}
代理回調(diào)
Objective-C
1
2
3
4
5#import "PNChart.h"
//For LineChart
lineChart.delegate=self;
動畫
默認繪制圖表時使用動畫,可以通過設置displayAnimation = NO來禁止動畫。
Objective-C
1
2
3
4
5#import "PNChart.h"
//For LineChart
lineChart.displayAnimation=NO;
Objective-C
//For DelegateMethod
-(void)userClickedOnLineKeyPoint:(CGPoint)pointlineIndex:(NSInteger)lineIndexpointIndex:(NSInteger)pointIndex{
NSLog(@"Click Key on line %f, %f line index is %d and point index is %d",point.x,point.y,(int)lineIndex,(int)pointIndex);
}
-(void)userClickedOnLinePoint:(CGPoint)pointlineIndex:(NSInteger)lineIndex{
NSLog(@"Click on line %f, %f, line index is %d",point.x,point.y,(int)lineIndex);
}
開源協(xié)議
PNChart?在MIT開源協(xié)議下可以使用,也就是說,只要在項目副本中包含了版權聲明和許可聲明,用戶就可以使用 PNChart?做任何想做的事情,而 PNChart?也無需承擔任何責任。可以通過查看 LICENSE 文件來獲取更多相關信息。