ios core plot设置xy坐标

#import "ViewController.h"

@interface ViewController ()
//要绘制基于x,y轴的图形
@property(nonatomic,retain)CPTXYGraph *graph;
@property(nonatomic,retain)NSMutableArray *dataForPlot;
//要绘制的view 必须为CPTGraphicView
@property(nonatomic,assign)CPTGraphHostingView *hostview;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self LoadInit];
   [self SetUpCoreplotViews];
}

-(void)LoadInit{
    self.hostview=[[[CPTGraphHostingView alloc] initWithFrame:CGRectMake(0, 0, 300, 200)] autorelease];
    
    self.dataForPlot=[NSMutableArray array];
    [self.view addSubview:_hostview];
  
}


-(void)SetUpCoreplotViews{
    
    //1:创建线性
    CPTMutableLineStyle *lineStyle=[CPTMutableLineStyle lineStyle];
    //基于x,y轴图形的画布
    self.graph=[[[CPTXYGraph alloc] initWithFrame:CGRectZero] autorelease];
    //设置主题
    CPTTheme *them=[CPTTheme themeNamed:kCPTStocksTheme];
    //把主题设置到画布上
    [self.graph applyTheme:them];
    
    //设置画布距离view的边距
    self.graph.paddingLeft=10.0f;
    self.graph.paddingTop=10.0f;
    self.graph.paddingRight=10.0f;
    self.graph.paddingBottom=10.0f;
    //然后把画布设置到指定view上
    self.hostview.hostedGraph=_graph;
    
    //设置画布在屏幕类可显示的x,y刻度
    CPTXYPlotSpace *plotSpace=(CPTXYPlotSpace *)_graph.defaultPlotSpace;
    //可以移动
    plotSpace.allowsUserInteraction=YES;
    plotSpace.xRange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromCGFloat(1.0) length:CPTDecimalFromCGFloat(2.0)];
    plotSpace.yRange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromCGFloat(1.0) length:CPTDecimalFromCGFloat(3.0)];
    
    
    //axes 设置x,y轴属性,如原点。
    //得到x,y轴的集合
    CPTXYAxisSet *axisSet=(CPTXYAxisSet *)self.graph.axisSet;
    lineStyle.miterLimit=1.0f;
    lineStyle.lineWidth=2.0f;
    lineStyle.lineColor=[CPTColor whiteColor];
    
    CPTXYAxis *x=axisSet.xAxis;
    x.orthogonalCoordinateDecimal=CPTDecimalFromString(@"3");//原点为3.(y=3)
    x.majorIntervalLength=CPTDecimalFromString(@"0.5");//主刻度之间检举
    x.minorTicksPerInterval=5;//主刻度中显示的细分刻度的数目
    x.minorTickLineStyle=lineStyle;
    //需要排除的不显示数字的主刻度
    
    NSArray *exclusionRange=[NSArray arrayWithObjects:[self CPTPlotRangeFromFloat:0.99 length:0.02],[self CPTPlotRangeFromFloat:2.99 length:0.02],nil];
    x.labelExclusionRanges=exclusionRange;
    
    
    //设置y 轴
    CPTXYAxis *y=axisSet.yAxis;
    y.orthogonalCoordinateDecimal=CPTDecimalFromString(@"3");
    y.majorIntervalLength=CPTDecimalFromString(@"0.5");
    y.minorTicksPerInterval=5;
    y.minorTickLineStyle=lineStyle;
    
    NSArray *yexclusionRange=[NSArray arrayWithObjects:[self CPTPlotRangeFromFloat:0.99 length:0.22],[self CPTPlotRangeFromFloat:2.99 length:0.22],nil];
    y.labelExclusionRanges=yexclusionRange;
    
    
}

-(CPTPlotRange *)CPTPlotRangeFromFloat:(float)location length:(float)length
{
    return [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(location) length:CPTDecimalFromFloat(length)];
}
                             
- (void)dealloc
{
    [_graph release];
    [_dataForPlot release];
    [super dealloc];
}

参考文章 http://www.cnblogs.com/kesalin/archive/2013/04/04/coreplot_xygrapha.html

画折线

//
//  ViewController.m
//  corePlot
//
//  Created by ganchaobo on 13-7-31.
//  Copyright (c) 2013年 ganchaobo. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
//要绘制基于x,y轴的图形
@property(nonatomic,retain)CPTXYGraph *graph;
@property(nonatomic,retain)NSMutableArray *dataForPlot;
//要绘制的view 必须为CPTGraphicView
@property(nonatomic,assign)CPTGraphHostingView *hostview;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self LoadInit];
   [self SetUpCoreplotViews];
}

-(void)LoadInit{
    self.hostview=[[[CPTGraphHostingView alloc] initWithFrame:CGRectMake(0, 0, 300, 200)] autorelease];
    
    self.dataForPlot=[NSMutableArray array];
    [self.view addSubview:_hostview];
    //_dataForPlot = [NSMutableArray arrayWithCapacity:100];
    NSUInteger i;
    for ( i = 0; i < 100; i++ ) {
        id x = [NSNumber numberWithFloat:0 + i * 0.05];
        id y = [NSNumber numberWithFloat:1.2 * rand() / (float)RAND_MAX + 1.2];
        [_dataForPlot addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x, @"x", y, @"y", nil]];
    }
  
}


-(void)SetUpCoreplotViews{
    
    //1:创建线性
    CPTMutableLineStyle *lineStyle=[CPTMutableLineStyle lineStyle];
    //基于x,y轴图形的画布
    self.graph=[[[CPTXYGraph alloc] initWithFrame:CGRectZero] autorelease];
    //设置主题
    CPTTheme *them=[CPTTheme themeNamed:kCPTStocksTheme];
    //把主题设置到画布上
    [self.graph applyTheme:them];
    
    //设置画布距离view的边距
    self.graph.paddingLeft=10.0f;
    self.graph.paddingTop=10.0f;
    self.graph.paddingRight=10.0f;
    self.graph.paddingBottom=10.0f;
    //然后把画布设置到指定view上
    self.hostview.hostedGraph=_graph;
    
    //设置画布在屏幕类可显示的x,y刻度
    CPTXYPlotSpace *plotSpace=(CPTXYPlotSpace *)_graph.defaultPlotSpace;
    //可以移动
    plotSpace.allowsUserInteraction=YES;
    plotSpace.xRange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromCGFloat(1.0) length:CPTDecimalFromCGFloat(2.0)];
    plotSpace.yRange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromCGFloat(1.0) length:CPTDecimalFromCGFloat(3.0)];
    
    
    //axes 设置x,y轴属性,如原点。
    //得到x,y轴的集合
    CPTXYAxisSet *axisSet=(CPTXYAxisSet *)self.graph.axisSet;
    lineStyle.miterLimit=1.0f;
    lineStyle.lineWidth=2.0f;
    lineStyle.lineColor=[CPTColor whiteColor];
    
    CPTXYAxis *x=axisSet.xAxis;
    x.orthogonalCoordinateDecimal=CPTDecimalFromString(@"3");//原点为3.(y=3)
    x.majorIntervalLength=CPTDecimalFromString(@"0.5");//主刻度之间检举
    x.minorTicksPerInterval=5;//主刻度中显示的细分刻度的数目
    x.minorTickLineStyle=lineStyle;
    //需要排除的不显示数字的主刻度
    
    NSArray *exclusionRange=[NSArray arrayWithObjects:[self CPTPlotRangeFromFloat:0.99 length:0.02],[self CPTPlotRangeFromFloat:2.99 length:0.02],nil];
    x.labelExclusionRanges=exclusionRange;
    
    
    //设置y 轴
    CPTXYAxis *y=axisSet.yAxis;
    y.orthogonalCoordinateDecimal=CPTDecimalFromString(@"3");
    y.majorIntervalLength=CPTDecimalFromString(@"0.5");
    y.minorTicksPerInterval=5;
    y.minorTickLineStyle=lineStyle;
    
    NSArray *yexclusionRange=[NSArray arrayWithObjects:[self CPTPlotRangeFromFloat:0.99 length:0.22],[self CPTPlotRangeFromFloat:2.99 length:0.22],nil];
    y.labelExclusionRanges=yexclusionRange;
    
    
    //画折线
    lineStyle.miterLimit=1.0f;
    lineStyle.lineWidth=3.0f;
    lineStyle.lineColor=[CPTColor blueColor];
    
    //折线的对象
    CPTScatterPlot *boundlinePlot=[[CPTScatterPlot alloc] init];
    boundlinePlot.dataLineStyle=lineStyle;
    boundlinePlot.identifier=@"blue";
    boundlinePlot.dataSource=self;
    
    [_graph addPlot:boundlinePlot];
    
    
}

-(CPTPlotRange *)CPTPlotRangeFromFloat:(float)location length:(float)length
{
    return [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(location) length:CPTDecimalFromFloat(length)];
}


#pragma mark -plot delegate
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot{
    return self.dataForPlot.count;
}

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
    NSString * key = (fieldEnum == CPTScatterPlotFieldX ? @"x" : @"y");
    NSNumber * num = [[_dataForPlot objectAtIndex:index] valueForKey:key];
    
    NSLog(@"%zi-->%@",[num intValue],key);
    
    return num;
}
                             
- (void)dealloc
{
    [_graph release];
    [_dataForPlot release];
    [super dealloc];
}
@end
原文地址:https://www.cnblogs.com/gcb999/p/3228855.html