文档章节

iOS图形库CorePlot

 山里来的鱼
发布于 2014/12/30 09:48
字数 809
阅读 3404
收藏 8

官网http://code.google.com/p/core-plot/

https://github.com/core-plot

Using Core Plot in an Application

https://github.com/core-plot/core-plot/wiki/Using-Core-Plot-in-an-Application

1. 将Framework目录中的CorePlot-CocoaTouch.xcodeproj拖到工程中。

2. Build Phase/Target Dependencies添加CorePlot-CocoaTouch,这样,在编译工程时会同时编译CorePlot库

3. 拖动libCorePlot-CocoaTouch.a到Build Phase/Link Binary with Libraries,如下图

Insert Static Lib

4. 添加其他库:QuartzCore、Accelerate

5.添加头文件

HeaderSearch

6. 设置Other Linker Flags为-all_load -ObjC

7.#import "CorePlot-CocoaTouch.h"

Core Plot on the web

https://github.com/core-plot/core-plot/wiki/Core-Plot-on-the-Web

绘制曲线

http://www.johnwordsworth.com/2011/10/adding-charts-to-your-iphone-ipad-app-using-core-plot/

1. 添加UIView到xib/Storyboard中,将class修改为CPTGraphHostingView

2.关联@property (strong, nonatomic) IBOutletCPTGraphHostingView *hostingView;

3. 添加属性、方法和协议

<CPTPlotDataSource>{

    CPTXYGraph *_graph;

    NSMutableArray *_graphData;

}

 

@property (nonatomic, retain) CPTXYGraph *graph;

@property (nonatomic, retain) NSMutableArray *graphData;

 

// Specific code that creates the scatter plot.

-(void)initialisePlot;

4.实现方法

@synthesize graph;

 

@synthesize graphData;

 

// This does the actual work of creating the plot if we don't already have a graph object.

-(void)initialisePlot

{

    // Start with some simple sanity checks before we kick off

    if ( (self.hostingView == nil) || (self.graphData == nil) ) {

        NSLog(@"TUTSimpleScatterPlot: Cannot initialise plot without hosting view or data.");

        return;

    }

    

    if ( self.graph != nil ) {

        NSLog(@"TUTSimpleScatterPlot: Graph object already exists.");

        return;

    }

    

    // Create a graph object which we will use to host just one scatter plot.

    CGRect frame = [self.hostingView bounds];

    self.graph = [[CPTXYGraph alloc] initWithFrame:frame];

    

    // Add some padding to the graph, with more at the bottom for axis labels.

    self.graph.plotAreaFrame.paddingTop = 20.0f;

    self.graph.plotAreaFrame.paddingRight = 20.0f;

    self.graph.plotAreaFrame.paddingBottom = 50.0f;

    self.graph.plotAreaFrame.paddingLeft = 20.0f;

    

    // Tie the graph we've created with the hosting view.

    self.hostingView.hostedGraph = self.graph;

    

    // If you want to use one of the default themes - apply that here.

    //[self.graph applyTheme:[CPTTheme themeNamed:kCPTDarkGradientTheme]];

    

    // Create a line style that we will apply to the axis and data line.

    CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];

    lineStyle.lineColor = [CPTColor orangeColor]; //线条颜色

    lineStyle.lineWidth = 2.0f;

    

    // Create a text style that we will use for the axis labels.

    CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];

    textStyle.fontName = @"Helvetica";

    textStyle.fontSize = 14;

    textStyle.color = [CPTColor orangeColor]; //文字颜色

    

    // Create the plot symbol we're going to use.

    CPTPlotSymbol *plotSymbol = [CPTPlotSymbol crossPlotSymbol];

    plotSymbol.lineStyle = lineStyle;

    plotSymbol.size = CGSizeMake(8.0, 8.0);

    

    // Setup some floats that represent the min/max values on our axis.

    float xAxisMin = -10;

    float xAxisMax = 10;

    float yAxisMin = 0;

    float yAxisMax = 100;

    

    // We modify the graph's plot space to setup the axis' min / max values.

    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;

    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xAxisMin) length:CPTDecimalFromFloat(xAxisMax - xAxisMin)];

    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(yAxisMin) length:CPTDecimalFromFloat(yAxisMax - yAxisMin)];

    

    // Modify the graph's axis with a label, line style, etc.

    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;

    

    axisSet.xAxis.title = @"Data X";

    axisSet.xAxis.titleTextStyle = textStyle;

    axisSet.xAxis.titleOffset = 30.0f;

    axisSet.xAxis.axisLineStyle = lineStyle;

    axisSet.xAxis.majorTickLineStyle = lineStyle;

    axisSet.xAxis.minorTickLineStyle = lineStyle;

    axisSet.xAxis.labelTextStyle = textStyle;

    axisSet.xAxis.labelOffset = 3.0f;

    axisSet.xAxis.majorIntervalLength = CPTDecimalFromFloat(2.0f);

    axisSet.xAxis.minorTicksPerInterval = 1;

    axisSet.xAxis.minorTickLength = 5.0f;

    axisSet.xAxis.majorTickLength = 7.0f;

    

    axisSet.yAxis.title = @"Data Y";

    axisSet.yAxis.titleTextStyle = textStyle;

    axisSet.yAxis.titleOffset = 40.0f;

    axisSet.yAxis.axisLineStyle = lineStyle;

    axisSet.yAxis.majorTickLineStyle = lineStyle;

    axisSet.yAxis.minorTickLineStyle = lineStyle;

    axisSet.yAxis.labelTextStyle = textStyle;

    axisSet.yAxis.labelOffset = 3.0f;

    axisSet.yAxis.majorIntervalLength = CPTDecimalFromFloat(10.0f);

    axisSet.yAxis.minorTicksPerInterval = 1;

    axisSet.yAxis.minorTickLength = 5.0f;

    axisSet.yAxis.majorTickLength = 7.0f;

    

    // Add a plot to our graph and axis. We give it an identifier so that we

    // could add multiple plots (data lines) to the same graph if necessary.

    CPTScatterPlot *plot = [[CPTScatterPlot alloc] init];

    plot.dataSource = self;

    plot.identifier = @"mainplot";

    plot.dataLineStyle = lineStyle;

    plot.plotSymbol = plotSymbol;

    [self.graph addPlot:plot];

}

4.delegate

// Delegate method that returns the number of points on the plot

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot

{

    if ( [plot.identifier isEqual:@"mainplot"] )

    {

        return [self.graphData count];

    }

    

    return 0;

}

 

// Delegate method that returns a single X or Y value for a given plot.

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

{

    if ( [plot.identifier isEqual:@"mainplot"] )

    {

        NSValue *value = [self.graphData objectAtIndex:index];

        CGPoint point = [value CGPointValue];

        

        // FieldEnum determines if we return an X or Y value.

        if ( fieldEnum == CPTScatterPlotFieldX )

        {

            return [NSNumber numberWithFloat:point.x];

        }

        else// Y-Axis

        {

            return [NSNumber numberWithFloat:point.y];

        }

    }

    

    return [NSNumber numberWithFloat:0];

}

5.测试代码

-(void)Test{

    [_graphData addObject:[NSValue valueWithCGPoint:CGPointMake(-10, 100)]];

    [_graphData addObject:[NSValue valueWithCGPoint:CGPointMake(-8, 50)]];

    [_graphData addObject:[NSValue valueWithCGPoint:CGPointMake(-6, 20)]];

    [_graphData addObject:[NSValue valueWithCGPoint:CGPointMake(-4, 10)]];

    [_graphData addObject:[NSValue valueWithCGPoint:CGPointMake(-2, 5)]];

    [_graphData addObject:[NSValue valueWithCGPoint:CGPointMake(0, 0)]];

    [_graphData addObject:[NSValue valueWithCGPoint:CGPointMake(2, 4)]];

    [_graphData addObject:[NSValue valueWithCGPoint:CGPointMake(4, 16)]];

    [_graphData addObject:[NSValue valueWithCGPoint:CGPointMake(6, 36)]];

    [_graphData addObject:[NSValue valueWithCGPoint:CGPointMake(8, 64)]];

    [_graphData addObject:[NSValue valueWithCGPoint:CGPointMake(10, 100)]];

    [selfinitialisePlot];

}

6.viewDidLoad中调用Test

viewDidLoad

[graphDataaddObject:[NSValuevalueWithCGPoint:CGPointMake(-10, 100)]];

    [graphDataaddObject:[NSValuevalueWithCGPoint:CGPointMake(-8, 50)]];

    [graphDataaddObject:[NSValuevalueWithCGPoint:CGPointMake(-6, 20)]];

    [graphDataaddObject:[NSValuevalueWithCGPoint:CGPointMake(-4, 10)]];

    [graphDataaddObject:[NSValuevalueWithCGPoint:CGPointMake(-2, 5)]];

    [graphDataaddObject:[NSValuevalueWithCGPoint:CGPointMake(0, 0)]];

    [graphDataaddObject:[NSValuevalueWithCGPoint:CGPointMake(2, 4)]];

    [graphDataaddObject:[NSValuevalueWithCGPoint:CGPointMake(4, 16)]];

    [graphDataaddObject:[NSValuevalueWithCGPoint:CGPointMake(6, 36)]];

    [graphDataaddObject:[NSValuevalueWithCGPoint:CGPointMake(8, 64)]];

    [graphDataaddObject:[NSValuevalueWithCGPoint:CGPointMake(10, 100)]];

© 著作权归作者所有

上一篇: iOS定时器
下一篇: VS错误
粉丝 8
博文 156
码字总数 28874
作品 0
珠海
程序员
私信 提问
CorePlot SDK的安装以及应用

一周了,总算是把曲线图和柱状图整出来了。记笔记,防忘记。 CorePlot SDK的安装:参照http://zani.iteye.com/blog/1135883 试用第一种方法安装,我下载的是CorePlot_1.0.zip。只要照着第一种...

长平狐
2012/10/25
195
0
一些iOS高效开源类库

因为iOS SDK相对比较底层,所以开发者就得受累多做一些体力活。不过幸运的是,有很多第三方的类库可以用来简化很多不必要的工作。笔者整理了一下在本人学习过程中用到的一些比较有用Objecti...

junwong
2012/02/29
1K
1
一些iOS高效开源类库

一些iOS高效开源类库 因为iOS SDK相对比较底层,所以开发者就得受累多做一些体力活。不过幸运的是,有很多第三方的类库可以用来简化很多不必要的工作。整理了一下在本人学习过程中用到的一些...

长平狐
2012/11/01
98
0
本人开发的第一款ios:比特币实时行情+挖矿收益计算器

亲爱的水果机用户, 本人开发的一款ios软件已登录AppStore,参考了bitcoinity.org/clarkmoody/btc123的重要功能,具备声音提醒,包含ticker、depth、trades及charts数据,同时带有挖矿收益估算...

_EKC
2013/08/16
1K
6
IOS 开源项目汇总【不断更新中】

UI界面类项目: Panoramagl —— 720全景展示 Panorama viewer library for iPhone, iPad and iPod touch MBProgressHUD —— 进度指示 一种优雅的,半透明的进度显示效果。同时还提供了其他...

junwong
2012/03/23
4.6K
0

没有更多内容

加载失败,请刷新页面

加载更多

总结

一、设计模式 简单工厂:一个简单而且比较杂的工厂,可以创建任何对象给你 复杂工厂:先创建一种基础类型的工厂接口,然后各自集成实现这个接口,但是每个工厂都是这个基础类的扩展分类,spr...

BobwithB
22分钟前
2
0
java内存模型

前言 Java作为一种面向对象的,跨平台语言,其对象、内存等一直是比较难的知识点。而且很多概念的名称看起来又那么相似,很多人会傻傻分不清楚。比如本文我们要讨论的JVM内存结构、Java内存模...

ls_cherish
25分钟前
2
0
友元函数强制转换

友元函数强制转换 p522

天王盖地虎626
昨天
5
0
js中实现页面跳转(返回前一页、后一页)

本文转载于:专业的前端网站➸js中实现页面跳转(返回前一页、后一页) 一:JS 重载页面,本地刷新,返回上一页 复制代码代码如下: <a href="javascript:history.go(-1)">返回上一页</a> <a h...

前端老手
昨天
4
0
JAVA 利用时间戳来判断TOKEN是否过期

import java.time.Instant;import java.time.LocalDateTime;import java.time.ZoneId;import java.time.ZoneOffset;import java.time.format.DateTimeFormatter;/** * @descri......

huangkejie
昨天
4
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部