注册

PNChart:一个简单漂亮的iOS图表库

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 依赖于下列框架,这些框架已经嵌入了 Xcode 开发工具:

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文件夹到你的工程中。

使用

#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];
#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];

[circleChart strokeChart];



```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];
# 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 允许在折线图和饼状图中添加图例,图例可以竖向堆叠布置或者横向并列布置。

#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];

更新数据

实时更新数据也非常简单。

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()0)];

}

代理回调

Objective-C

#import "PNChart.h"

//For LineChart

lineChart.delegate=self;

动画

默认绘制图表时使用动画,可以通过设置displayAnimation = NO来禁止动画。

Objective-C


#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);

}

开源协议

PNChart 在MIT开源协议下可以使用,也就是说,只要在项目副本中包含了版权声明和许可声明,用户就可以使用 PNChart 做任何想做的事情,而 PNChart 也无需承担任何责任。可以通过查看 LICENSE 文件来获取更多相关信息。

开源地址:https://github.com/kevinzhow/PNChart

链接:https://www.jianshu.com/p/9c162d6f8f14

0 个评论

要回复文章请先登录注册