http://blog.csdn.net/mark_creative/article/details/9768827
/// @name Axis //x y 坐标
/// @{
@property (nonatomic,readwrite,copy)CPTLineStyle *axisLineStyle;
@property (nonatomic,readwrite,assign)CPTCoordinate coordinate;
@property (nonatomic,readwrite,assign)NSDecimal labelingOrigin;
@property (nonatomic,readwrite,assign)CPTSign tickDirection;
@property (nonatomic,readwrite,copy)CPTPlotRange *visibleRange;
@property (nonatomic,readwrite,copy)CPTLineCap *axisLineCapMin;
@property (nonatomic,readwrite,copy)CPTLineCap *axisLineCapMax;
/// @}
/// @name Title 标题
/// @{
@property (nonatomic,readwrite,copy)CPTTextStyle *titleTextStyle;
@property (nonatomic,readwrite,retain)CPTAxisTitle *axisTitle;
@property (nonatomic,readwrite,assign)CGFloat titleOffset;
@property (nonatomic,readwrite,copy)NSString *title;
@property (nonatomic,readwrite,assign)CGFloat titleRotation;
@property (nonatomic,readwrite,assign)NSDecimal titleLocation;
@property (nonatomic,readonly,assign)NSDecimal defaultTitleLocation;
/// @}
/// @name Labels 坐标轴的文字
/// @{
@property (nonatomic,readwrite,assign)CPTAxisLabelingPolicy labelingPolicy;
@property (nonatomic,readwrite,assign)CGFloat labelOffset;
@property (nonatomic,readwrite,assign)CGFloat minorTickLabelOffset;
@property (nonatomic,readwrite,assign)CGFloat labelRotation;
@property (nonatomic,readwrite,assign)CGFloat minorTickLabelRotation;
@property (nonatomic,readwrite,assign)CPTAlignment labelAlignment;
@property (nonatomic,readwrite,assign)CPTAlignment minorTickLabelAlignment;
@property (nonatomic,readwrite,copy)CPTTextStyle *labelTextStyle;
@property (nonatomic,readwrite,copy)CPTTextStyle *minorTickLabelTextStyle;
@property (nonatomic,readwrite,retain)NSNumberFormatter *labelFormatter;
@property (nonatomic,readwrite,retain)NSNumberFormatter *minorTickLabelFormatter;
@property (nonatomic,readwrite,retain)NSSet *axisLabels;
@property (nonatomic,readwrite,retain)NSSet *minorTickAxisLabels;
@property (nonatomic,readonly,assign)BOOL needsRelabel;
@property (nonatomic,readwrite,retain)NSArray *labelExclusionRanges;
@property (nonatomic,readwrite,retain)CPTShadow *labelShadow;
/// @}
/// @name Major Ticks 主刻度线
/// @{
@property (nonatomic,readwrite,assign)NSDecimal majorIntervalLength;
@property (nonatomic,readwrite,assign)CGFloat majorTickLength;
@property (nonatomic,readwrite,copy)CPTLineStyle *majorTickLineStyle;
@property (nonatomic,readwrite,retain)NSSet *majorTickLocations;
@property (nonatomic,readwrite,assign)NSUInteger preferredNumberOfMajorTicks;
/// @}
/// @name Minor Ticks 分刻度线
/// @{
@property (nonatomic,readwrite,assign)NSUInteger minorTicksPerInterval;
@property (nonatomic,readwrite,assign)CGFloat minorTickLength;
@property (nonatomic,readwrite,copy)CPTLineStyle *minorTickLineStyle;
@property (nonatomic,readwrite,retain)NSSet *minorTickLocations;
/// @}
/// @name Grid Lines 网格线
/// @{
@property (nonatomic,readwrite,copy)CPTLineStyle *majorGridLineStyle;
@property (nonatomic,readwrite,copy)CPTLineStyle *minorGridLineStyle;
@property (nonatomic,readwrite,copy)CPTPlotRange *gridLinesRange;
core-plot是一种OS X和iOS的绘图库,它与 Apple technologies like Core Animation, Core Data, and Cocoa Bindings紧密集成,提供2D的可视化数据图。下文将简要介绍在iOS开发中如何使用core-plot。
一、配置
1、从http://code.google.com/p/core-plot/下载最新版本,其中的readme有安装步骤。
2、拷贝CorePlot_1.0/Binaries/iOS文件夹中的CorePlotHeaders和libCorePlot-CocoaTouch.a到项目中
3、在项目的target build settings找到“Other Linker Flags”,添加”-ObjC“
(如果Xcode版本低于4.2,还要添加“-all_load”)
4、项目中添加QuartzCore.framework
完成配置,如下图
二、使用
在需要绘制图形的.h文件中,加上:#import"CorePlot-CocoaTouch.h"即可使用。另外需要实现CPTPlotDataSource和对应图形的代理(如曲线CPTScatterPlotDelegate和CPTScatterPlotDataSource)。
在需要绘制图形的.m文件中,
1、绘制数据图形需要一块画板CPTGraphHostingView和画布CPTXYGraph,定义并使用画板和画布如下:
2、设置画布相关属性和坐标系
3、定义图形并设置其样式——以曲线CPTScatterPlot为例
4、在画布中加上曲线
5、实现CPTPlotDataSource,为曲线添加数据。简单地需要实现以下两个函数
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot ; //返回曲线的数据点个数
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index ; //返回具体点坐标值
6、到第5步已经可以将图形画出来了。另外还有一些其他用于完善图形的代理函数。如
在曲线上方添加y值:-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index //CPTPlotDataSource
在曲线上标注坐标点:-(CPTPlotSymbol *)symbolForScatterPlot:(CPTScatterPlot *)plot recordIndex:(NSUInteger)index //CPTScatterPlotDataSource
点击各个数据点响应操作:-(void)scatterPlot:(CPTScatterPlot *)plot plotSymbolWasSelectedAtRecordIndex:(NSUInteger)index //CPTScatterPlotDelegate
一、柱状图
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:
![](https://img-my.csdn.net/uploads/201211/07/1352276296_3809.png)
![](https://img-my.csdn.net/uploads/201211/07/1352276315_1117.png)
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot ; //返回饼图的数据点个数
//返回具体每块扇形的值,系统将自动合计并划分扇形;可以全为0,此时不会绘制饼图
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
![](https://img-my.csdn.net/uploads/201211/07/1352276832_7332.png)
在扇形上方添加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的类库介绍: