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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM