core plot的学习与应用
2015-09-19 10:31
633 查看
基本概念说明
core-plot的UML类图如上
从上图可以看出CPTGraph类是比较核心的类,它与CPTAnnotation,CPTLegend,CPTPlotAreaFrame,CPTAxisSet,CPTPlot,CPTPlotSpace成关联关系,通过设置CPTGraph的各项参数并将其添加到视图中,即可实现统计图的绘制与显示。
关键类的功能与作用
因为,无论是绘制什么样的图,实际上就是针对性地设置以上五个类对应的对象的各项参数。
经典例程分析
条形图
结合代码注释以及最下面的界面说明图能基本了解如何绘制图片。
core-plot的UML类图如上
从上图可以看出CPTGraph类是比较核心的类,它与CPTAnnotation,CPTLegend,CPTPlotAreaFrame,CPTAxisSet,CPTPlot,CPTPlotSpace成关联关系,通过设置CPTGraph的各项参数并将其添加到视图中,即可实现统计图的绘制与显示。
关键类的功能与作用
CPTAnnotation | CPTAnnotation是抽象类,它的两个子类分别是CPTLayerAnnotation与CPTPlotSpaceAnnotation,分别用于为基本图层与绘图图层添加文字注释。 |
CPTLegend | 用于添加图例。 |
CPTPlotAreaFrame | 其内部的核心类为 CPTPlotArea,可以理解为plot的容器,通过设置可以定制plot以外的图形,如网格效果,背景填充颜色等,还定义了交互行为协议。 |
CPTAxisSet | 用于管理CPTAxis类与其子类,实现坐标轴的定制与显示。 |
CPTPlot | CPTPlot是抽象的绘图类,其有CPTBarPlot、CPTPieChart、CPTRangePlot、CPTScatterPlot、CPTTradingRangePlot五个子类,通过实现五个子类,可以分别实现条状图、圆饼图、幅度图、散点图、交易幅度图的现实。其设计思路与tableView十分相似,也是通过datasource实现数据的填充,delegate实现交互行为与特殊数据填充。 |
CPTPlotSpace | 通过定义CPTPlotSpace实现图空间的定制,因为坐标轴中的图画通常是无限大的,因此需要配置相关的参数决定需要显示的部分。 |
经典例程分析
条形图
结合代码注释以及最下面的界面说明图能基本了解如何绘制图片。
//将graph添加到指定的hostingView,普通的view无法添加graph CPTGraph *graph = [[CPTXYGraphalloc]initWithFrame:bounds]; [selfaddGraph:graphtoHostingView:hostingView]; //设置主题,效果如手机设置主题。有如下主题kCPTDarkGradientTheme、kCPTPlainBlackTheme、kCPTPlainWhiteTheme、kCPTSlateTheme、kCPTStocksTheme [selfapplyTheme:themetoGraph:graphwithDefault:[CPTThemethemeNamed:kCPTPlainWhiteTheme]]; //设置plotAreaFrame的大小,plotAreaFrame是graph上的一张子图。设置padding参数是用来设置子图的四个边缘与父视图的距离 graph.plotAreaFrame.paddingLeft += self.titleSize* CPTFloat(2.5); graph.plotAreaFrame.paddingTop += self.titleSize* CPTFloat(1.25); graph.plotAreaFrame.paddingRight += self.titleSize; graph.plotAreaFrame.paddingBottom+= self.titleSize; graph.plotAreaFrame.masksToBorder = NO; //创建grid line styles,定义了majorGridLineStyle、minorGridLineStyle两种网格格式 CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStylelineStyle]; majorGridLineStyle.lineWidth= 1.0; majorGridLineStyle.lineColor= [[CPTColorblackColor]colorWithAlphaComponent:0.75]; CPTMutableLineStyle *minorGridLineStyle = [CPTMutableLineStylelineStyle]; minorGridLineStyle.lineWidth= 1.0; minorGridLineStyle.lineColor= [[CPTColorblackColor]colorWithAlphaComponent:0.25]; //获取graph的坐标轴集合 CPTXYAxisSet *axisSet = (CPTXYAxisSet*)graph.axisSet; //获取graph.axisSet中的x轴 CPTXYAxis *x = axisSet.xAxis; { //设置x轴方向的网格格式 x.majorGridLineStyle = majorGridLineStyle; x.minorGridLineStyle = minorGridLineStyle; //主要间隔长度 x.majorIntervalLength =CPTDecimalFromInteger(1); //间隔最少标签数 x.minorTicksPerInterval =0; //x正交坐标位置 x.orthogonalCoordinateDecimal= CPTDecimalFromInteger(0); x.axisLineStyle =nil; x.majorTickLineStyle =nil; x.minorTickLineStyle =nil; x.labelFormatter =nil; } //获取graph.axisSet中的y轴 CPTXYAxis *y = axisSet.yAxis; { //设置y轴方向的网格风格 y.majorGridLineStyle = majorGridLineStyle; y.minorGridLineStyle = minorGridLineStyle; //主要间隔长度 y.majorIntervalLength =CPTDecimalFromInteger(1); //间隔最少标签数 y.minorTicksPerInterval =9; //y轴自动布局约束 y.axisConstraints = [CPTConstraintsconstraintWithLowerOffset:0.0]; //主轴的数字标签数量 y.preferredNumberOfMajorTicks= 16; //轴标签偏移量 y.labelOffset =self.titleSize* CPTFloat(0.375); //轴每个数字标签旋转角度 y.labelRotation =CPTFloat(M_PI_4); //标签规则 y.labelingPolicy =CPTAxisLabelingPolicyAutomatic; y.axisLineStyle =nil; y.majorTickLineStyle =nil; y.minorTickLineStyle =nil; y.title =@"Y Axis"; //标题 y.titleOffset= self.titleSize* CPTFloat(1.25); } // Create a bar line style CPTMutableLineStyle *barLineStyle = [[CPTMutableLineStylealloc]init]; barLineStyle.lineWidth= 1.0; //bar 线框宽度 barLineStyle.lineColor= [CPTColorwhiteColor];//bar 线框颜色 // Create bar plot CPTBarPlot *barPlot = [[CPTBarPlotalloc]init]; barPlot.lineStyle = barLineStyle; barPlot.barWidth =CPTDecimalFromFloat(0.75f);// bar is 75% of the available space barPlot.barCornerRadius =10.0; barPlot.barsAreHorizontal= NO; barPlot.dataSource =self; barPlot.identifier =@"Bar Plot 1"; [graph addPlot:barPlot]; //setting Plot space:设置显示的范围 //获取x轴的范围并额外添加1.05的长度 CPTMutablePlotRange *barRange = [[barPlotplotRangeEnclosingBars]mutableCopy]; [barRange expandRangeByFactor:CPTDecimalFromDouble(1.05)]; CPTXYPlotSpace *barPlotSpace = (CPTXYPlotSpace*)graph.defaultPlotSpace; barPlotSpace.xRange= barRange; barPlotSpace.yRange= [CPTPlotRangeplotRangeWithLocation:CPTDecimalFromFloat(0.0f)length:CPTDecimalFromFloat(16.0f)]; // Add legend:添加图例 CPTLegend *theLegend = [CPTLegendlegendWithGraph:graph]; //设置填充颜色 theLegend.fill = [CPTFillfillWithColor:[CPTColorcolorWithGenericGray:CPTFloat(0.15)]]; //设置边框风格 theLegend.borderLineStyle= barLineStyle; theLegend.cornerRadius =2.0; //设置文本风格 CPTMutableTextStyle *whiteTextStyle = [CPTMutableTextStyletextStyle]; whiteTextStyle.color = [CPTColorwhiteColor]; theLegend.textStyle = whiteTextStyle; theLegend.numberOfRows= 2; graph.legend = theLegend; //设置图例放置的位置 graph.legendAnchor =CPTRectAnchorTop; graph.legendDisplacement= CGPointMake( 0.0, self.titleSize* CPTFloat(-2.625) ); } #pragma mark - #pragma mark Plot Data Source Methods -(NSUInteger)numberOfRecordsForPlot:(CPTPlot*)plot { //返回plot的数量,本例中有八个 return self.plotData.count; } -(NSArray*)numbersForPlot:(CPTPlot*)plot field:(NSUInteger)fieldEnum recordIndexRange:(NSRange)indexRange { NSArray *nums = nil; //通过fieldEnum分别返回每个bar的位置数组与数据数组 switch ( fieldEnum ) { //返回bar plot的位置图 case CPTBarPlotFieldBarLocation: nums = [NSMutableArrayarrayWithCapacity:indexRange.length]; for ( NSUInteger i = indexRange.location; i <NSMaxRange(indexRange); i++ ) { [(NSMutableArray*)nums addObject : @(i)]; } break; //获取bar plot的数据 case CPTBarPlotFieldBarTip: nums = [self.plotDataobjectsAtIndexes:[NSIndexSetindexSetWithIndexesInRange:indexRange]]; break; default: break; } return nums; } -(CPTFill*)barFillForBarPlot:(CPTBarPlot*)barPlot recordIndex:(NSUInteger)index { CPTColor *color = nil; switch ( index ) { case 0: color = [CPTColorredColor]; break; case 1: color = [CPTColorgreenColor]; break; case 2: color = [CPTColorblueColor]; break; case 3: color = [CPTColoryellowColor]; break; case 4: color = [CPTColorpurpleColor]; break; case 5: color = [CPTColorcyanColor]; break; case 6: color = [CPTColororangeColor]; break; case 7: color = [CPTColormagentaColor]; break; default: break; } //设置渐变填充风格对象,渐变风格由{以上代码设置的color}渐变到{黑色}结束。 CPTGradient *fillGradient = [CPTGradientgradientWithBeginningColor:colorendingColor:[CPTColorblackColor]]; //返回设置填充效果的对象 return [CPTFillfillWithGradient:fillGradient]; } -(NSString*)legendTitleForBarPlot:(CPTBarPlot*)barPlot recordIndex:(NSUInteger)index { //设置图例中每个单元的名称 return [NSStringstringWithFormat:@"Bar %lu", (unsignedlong)(index +1)]; } |
相关文章推荐
- static关键字作用总结
- cursor属性
- 网站后台模板
- Java知识整理(二十)之session
- STM32F030学习之串口收发程序
- 设计模式学习笔记(一)工厂模式
- 数据结构之自建算法库——链串
- Java知识整理(十九)之cookie
- the method d(string string) is undefined for the type log
- nefu494深海机器人问题【网络流24题】最大费用流
- warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries f
- vs2013 x64 编译汇编代码
- Java知识整理(十八)之JSP
- FEL-表达式
- 包装设计模式2
- 周鸿祎创业以来的“六大战役”
- Java基础知识强化28:Scanner类之Scanner类的概述
- 基于xmpp客户端与服务器的快速重连方法、终端和服务器
- Java知识整理(十七)之Servlet
- UVA - 674 Coin Change(完全背包)