2011-01-10 60 views
1

我在CorePlot中編寫的應用程序中有一個Graphing Calculator插件。並且有一個小錯誤 - 當圖形出現在屏幕上,而不是屏幕中心的點(0; 0)時,顯示接近(2; 3)的點。CorePlot圖形計算器不在(0; 0)點開始

下面的代碼:

-(void)viewDidLoad 
{ 

    [super viewDidLoad]; 

    graph = [[CPXYGraph alloc] initWithFrame:CGRectZero]; 
    CPTheme *theme = [CPTheme themeNamed:kCPStocksTheme]; 
    [graph applyTheme:theme]; 
    CPGraphHostingView *hostingView = (CPGraphHostingView *)self.view ; 
    hostingView.hostedGraph = graph; 

    graph.paddingLeft =5; 
    graph.paddingTop = 5; 
    graph.paddingRight = 5; 
    graph.paddingBottom = 5; 

    CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace; 
    plotSpace.allowsUserInteraction = YES; 
    PlotSpaceX=2; 
    PlotSpaceY=3; 
    plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(1.0) length:CPDecimalFromFloat(PlotSpaceX)]; 
    plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(1.0) length:CPDecimalFromFloat(PlotSpaceY)]; 

    CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet; 
    CPXYAxis *x = axisSet.xAxis; 
    x.majorIntervalLength = CPDecimalFromString(@"0.5"); 
    x.orthogonalCoordinateDecimal = CPDecimalFromString(@"0"); 
    x.minorTicksPerInterval = 2; 

    CPXYAxis *y = axisSet.yAxis; 
    y.majorIntervalLength = CPDecimalFromString(@"0.5"); 
    y.minorTicksPerInterval = 2; 
    y.orthogonalCoordinateDecimal = CPDecimalFromString(@"0"); 

    boundLinePlot = [[[CPScatterPlot alloc] init] autorelease]; 
    boundLinePlot.identifier = @"Blue Plot"; 
    boundLinePlot.dataLineStyle.miterLimit = 1.0f; 
    boundLinePlot.dataLineStyle.lineWidth = 3.0f; 
    boundLinePlot.dataLineStyle.lineColor = [CPColor redColor]; 
    boundLinePlot.dataSource = self; 
    [graph addPlot:boundLinePlot]; 

    NSMutableArray *contentArray = [NSMutableArray arrayWithCapacity:100000]; 
    NSUInteger i; 
    for (i = 0; i < 600000; i++) { 
     id x = [NSNumber numberWithFloat:i*0.05-1000]; 
     id y =[NSNumber numberWithFloat: [x floatValue] *[x floatValue]]; 
     [contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x, @"x", y, @"y", nil]]; 
    } 
    self.dataForPlot = contentArray; 

} 


#pragma mark - 
#pragma mark Plot Data Source Methods 

-(NSUInteger)numberOfRecordsForPlot:(CPPlot *)plot { 
    return [dataForPlot count]; 
} 

-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index 
{ 
    NSNumber *num = [[dataForPlot objectAtIndex:index] valueForKey:(fieldEnum == CPScatterPlotFieldX ? @"x" : @"y")]; 

    return num; 
} 

我怎樣才能解決這個問題,所以點(0,0)加載圖形時,會出現?

回答

2

正如你所寫,你的中心點應該是(2,2.5)。

繪圖範圍是作爲起始位置和長度給出的。你xRange從1到(1 + 2)= 3,你的yRange從1到(1 + 3)= 4。試試這樣:

plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-PlotSpaceX) length:CPDecimalFromFloat(PlotSpaceX * 2.0)]; 
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-PlotSpaceY) length:CPDecimalFromFloat(PlotSpaceY * 2.0)]; 
+0

非常感謝!有用! – Knodel 2011-01-11 06:58:23