core-plot——简单使用(续)

来源:互联网 发布:淘宝双十一规则 编辑:程序博客网 时间:2024/05/19 13:59

吐舌头   转载或引用请标明出处哦   ~~~~~


      在《core-plot——简单使用》中,介绍了如何配置和使用core-plot,建立画布和坐标系,并绘制曲线图,在这里简单地介绍一下柱状图饼图的绘制。

一、柱状图

1、建立并设置画板CPTGraphHostingView、画布CPTXYGraph、轴空间CPTXYPlotSpace、坐标轴集CPTXYAxisSet,具体参考《core-plot——简单使用》的代码。


2、定义柱状图并设置其具体样式



3、添加柱状图到画布上



4、实现CPTPlotDataSource,为柱状图添加数据,简单地需要实现以下两个函数:

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot ;              //返回柱状图的数据点个数

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index ;   //返回具体点坐标值





5、到第4步柱状图已经花出来了,以下还有一些完善图形的函数

在柱子上方添加y值:-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index   //CPTPlotDataSource

设置某个柱子的样式(fill 或line): //CPTBarPlotDataSource

-(CPTFill *)barFillForBarPlot:(CPTBarPlot *)barPlot recordIndex:(NSUInteger)index

-(CPTLineStyle *)barLineStyleForBarPlot:(CPTBarPlot *)barPlot recordIndex:(NSUInteger)index

点击某个柱子响应操作:-(void)barPlot:(CPTBarPlot *)plot barWasSelectedAtRecordIndex:(NSUInteger)index  //CPTBarPlotDelegate


二、饼图

1、建立并设置画板CPTGraphHostingView、画布CPTXYGraph,具体参考《core-plot——简单使用》的代码。

2、由于饼图没有坐标轴,因此需要将画布的坐标轴集设置为nil



3、定义饼图并设置其具体样式:



4、添加饼图到画布上



5、实现CPTPlotDataSource,为饼图添加数据,简单地需要实现以下两个函数:

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot ;              //返回饼图的数据点个数


//返回具体每块扇形的值,系统将自动合计并划分扇形;可以全为0,此时不会绘制饼图

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index 



6、到第5步饼图已经花出来了,以下还有一些完善图形的函数

在扇形上方添加y值:-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index   //CPTPlotDataSource

设置某个扇形的颜色:-(CPTFill *)sliceFillForPieChart:(CPTPieChart *)pieChart recordIndex:(NSUInteger)index  //CPTPieChartDataSource

点击某个扇形响应操作:-(void)pieChart:(CPTPieChart *)plot sliceWasSelectedAtRecordIndex:(NSUInteger)index  //CPTPieChartDelegate


各种图形的绘制过程和原理相似,可以多查看CorePlotHeaders文件夹的文件。

关于core-plot的类库介绍:

https://sharmanavnidhi-navnidhi.googlecode.com/hg-history/534191a0e4881407994489291ba0df14e616fcbc/documentation/html/iOS/index.html

原创粉丝点击