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

发布于 2024-10-11 15:07:04 字数 2563 浏览 4 评论 0原文

我的应用程序中有一个用 CorePlot 编写的图形计算器插件。还有一个小错误 - 当图形出现在屏幕上时,显示的不是屏幕中心的点 (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)?

I have a Graphing Calculator plugin in my app written in CorePlot. And there's a small bug - when the graphic appears at the screen, instead of point (0;0) in the centre of the screen, point close to (2;3) is displayed.

Here's the code:

-(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;
}

How can I fix it, so point (0;0) appears when the graphic is loaded?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

物价感观 2024-10-18 15:07:04

正如所写,你的中心点应该是 (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)];

As written, your center point should be (2, 2.5).

Plot ranges are given as a starting location and length. You xRange goes from 1 to (1+2)=3 and your yRange goes from 1 to (1+3)=4. Try something like this:

plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-PlotSpaceX) length:CPDecimalFromFloat(PlotSpaceX * 2.0)];
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-PlotSpaceY) length:CPDecimalFromFloat(PlotSpaceY * 2.0)];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文