在上节中我们讲了如何在x-code中导入Core Plot 类库,接下来,我们用该类库绘制一个饼图出来:
建一个单视图工程
.h文件内容如下:
#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"
@interface QQViewController :UIViewController<CPTPlotDataSource,CPTPieChartDelegate>
@property(retain,nonatomic)NSMutableArray *arr;
@property(retain,nonatomic)CPTXYGraph *graph;
@property(retain,nonatomic)CPTPieChart *piePlot;
@end
- (void)viewDidLoad
{
[superviewDidLoad];
//初始化数组
self.arr = [[NSMutableArrayalloc]initWithObjects:@"0.2",@"0.3",@"0.1",@"0.2",@"0.2",nil];
//创建画布
self.graph = [[CPTXYGraphalloc]initWithFrame:self.view.bounds];
//设置画布主题
CPTTheme *theme = [CPTThemethemeNamed:kCPTPlainWhiteTheme];
[self.graphapplyTheme:theme];
//画布与周围的距离
self.graph.paddingBottom =10;
self.graph.paddingLeft =5;
self.graph.paddingRight =5;
self.graph.paddingTop =10;
//将画布的坐标轴设为空
self.graph.axisSet =nil;
//创建画板
CPTGraphHostingView *hostView = [[CPTGraphHostingViewalloc]initWithFrame:self.view.bounds];
//设置画板的画布
hostView.hostedGraph =self.graph;
//设置画布标题的风格
CPTMutableTextStyle *whiteText = [CPTMutableTextStyletextStyle];
whiteText.color = [CPTColorblackColor];
whiteText.fontSize =18;
whiteText.fontName =@"Helvetica-Bold";
self.graph .titleTextStyle = whiteText;
self.graph.title =@"饼状图";
//创建饼图对象
self.piePlot = [[CPTPieChartalloc]initWithFrame:CGRectMake(10,10, 200, 200)];
//设置数据源
self.piePlot.dataSource =self;
//设置饼图半径
self.piePlot.pieRadius =100.0;
//设置饼图表示符
self.piePlot.identifier =@"pie chart";
//饼图开始绘制的位置
self.piePlot.startAngle =M_PI_4;
//饼图绘制的方向(顺时针/逆时针)
self.piePlot.sliceDirection = CPTPieDirectionCounterClockwise;
//饼图的重心
self.piePlot.centerAnchor =CGPointMake(0.5,0.38);
//饼图的线条风格
self.piePlot.borderLineStyle = [CPTLineStylelineStyle];
//设置代理
self.piePlot.delegate =self;
//将饼图加到画布上
[self.graphaddPlot:self.piePlot];
//将画板加到视图上
[self.viewaddSubview:hostView];
//创建图例
CPTLegend *theLegeng = [CPTLegendlegendWithGraph:self.graph];
theLegeng.numberOfColumns =1;
theLegeng.fill = [CPTFillfillWithColor:[CPTColorwhiteColor]];
theLegeng.borderLineStyle = [CPTLineStylelineStyle];
theLegeng.cornerRadius =5.0;
theLegeng.delegate =self;
self.graph.legend = theLegeng;
self.graph.legendAnchor = CPTRectAnchorRight;
self.graph.legendDisplacement =CGPointMake(-10,100);
}
#pragma ===========================CPTPlotDelegate========================
//返回扇形数目
- (NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
return self.arr.count;
}
//返回每个扇形的比例
- (NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)idx
{
return [self.arrobjectAtIndex:idx];
}
//凡返回每个扇形的标题
- (CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)idx
{
CPTTextLayer *label = [[CPTTextLayeralloc]initWithText:[NSStringstringWithFormat:@"hello,%@",[self.arrobjectAtIndex:idx]]];
CPTMutableTextStyle *text = [ label.textStylemutableCopy];
text.color = [CPTColorwhiteColor];
return label;
}
#pragma ===========CPTPieChart Delegate========================
//选中某个扇形时的操作
- (void)pieChart:(CPTPieChart *)plot sliceWasSelectedAtRecordIndex:(NSUInteger)idx
{
self.graph.title = [NSStringstringWithFormat:@"比例:%@",[self.arrobjectAtIndex:idx]];
}
//返回图例
- (NSAttributedString *)attributedLegendTitleForPieChart:(CPTPieChart *)pieChart recordIndex:(NSUInteger)idx
{
NSAttributedString *title = [[NSAttributedString alloc]initWithString:[NSStringstringWithFormat:@"hi:%i",idx]];
return title;
}
点击某个扇形,“饼状图”标题内容回改变。
其他一些方法:
(1) -(CPTFill *)sliceFillForPieChart:(CPTPieChart *)pieChart recordIndex:(NSUInteger)idx;绘制某一扇形是的颜色。
(2)对于饼图,我们可以把某块扇形“切除”下来,以此突出该扇形区域。这需要实现数据源方法radialOffsetForPieChart:recordIndex: 方法。以下代码将饼图中第2块扇形“剥离”10个像素点:
-(CGFloat)radialOffsetForPieChart:(CPTPieChart*)piePlot recordIndex:(NSUInteger)index{
return (indexix==1?10:0);
}
(3)图例由 CPTLengend 对象表示。上述代码中,我们已经创建了图例。
CPTLengend 的一些属性:
numberOfColumns属性 - 图例的列数。有时图例太多,单列显示太长,可分为多列显示。
fill属性 - 图例的填充属性,CPTFill 类型。
borderLineStyle属性 - 图例外框的线条样式。
cornerRadiuss属性 - 图例外框的圆角半径。
把一个图例对象赋值给图形的 legend 属性,即可在绘制图形时加上图例。此外还图形对象还有两个和图例相关的重要属性:
legendAnchor属性 - 图例对齐于图框的位置,可以用 CPTRectAnchor 枚举类型,指定图例向图框的4角、4边(中点)对齐。
legendDisplacement属性 - 图例对齐时的偏移距离。注意,对齐时是使用图例的相同锚点和图框的相同锚点对齐。比如,图形的legendAnchor属性是右上角(CPTRectAnchorTopRight),则对齐时用图例的右上角和图框的右上角进行对齐。这个偏移坐标(x,y)就是这两个锚点之间的距离(用图例的锚点坐标减图框的锚点坐标)。默认legendDisplacement为(0,0)。注意这个值的正负关系,比如(-30,-30)和(30,30)是截然相反的。
图例上的描述文字,从数据本身是看不到的,默认情况下 CorePlot 会以“Pie Chart 1”、“Pie Char 2”来命名。
我们需要实现数据源方法legendTitleForPieChart:recordIndex:来覆盖默认的图例名称:
-(NSString*)legendTitleForPieChart:(CPTPieChart *)pieChart recordIndex:(NSUInteger)index;
编辑文Core Plot学习笔记(二):绘制饼图,布布扣,bubuko.com
原文:http://blog.csdn.net/u012890196/article/details/20909877