CorePlot图形计算器不在(0; 0)点开始

时间:2011-01-10 21:21:14

标签: objective-c cocoa core-plot

我的应用程序在CorePlot中编写了一个Graphing Calculator插件。并且有一个小错误 - 当图形出现在屏幕上而不是屏幕中心的点(0; 0)时,会显示接近(2; 3)的点。

以下是代码:

-(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)?

1 个答案:

答案 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)];