【问题标题】:coreplot 2 plots in graphcoreplot 2 在图中绘制
【发布时间】:2012-03-22 09:48:28
【问题描述】:

我目前正在为一个应用程序创建一个图表,使用 coreplot 并且有一个我无法解释的问题。

我读过(它没有帮助): core-plot, unable to plot two graphs

问题:为什么只显示第二个添加的情节?如果我更改将它们添加到 hostView 的顺序,则仅显示我添加的最后一个。请参阅下面的代码和屏幕截图。

图表将包含 2 个不同的图

PROBLEM:
- both plots show perfectly if added alone.
- if i add both plots, only the second (last) one added gets displayed.
- to assure you that both are well on their own i added screenshots on the bottom.

首先绘制单0线

identifier : @"nullLine"
name: nullPlot

第二个图我存储的数据的散点图

identifier : @"balanceChart"
name: plot

这是我的一些代码:

    CPTGraphHostingView *host = [self buildGraphView];   //returns a viewwithframe
    [view addSubview:host];

    graph = [[CPTXYGraph alloc ]initWithFrame:host.frame];
    host.hostedGraph = graph;

    CPTScatterPlot *plot = [[CPTScatterPlot alloc]init ];
    plot.dataSource = self;
    [plot setIdentifier:@"balanceChart"];

    // Setup plot space
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
    plotSpace.allowsUserInteraction = YES;
    plotSpace.xRange                = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1.0) length:CPTDecimalFromFloat(20.0)];
    plotSpace.yRange                = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-200.0) length:CPTDecimalFromFloat(400.0)];

    CPTScatterPlot *nullPlot = [[CPTScatterPlot alloc]init ];
    nullPlot.dataSource = self;
    [nullPlot setIdentifier:@"nullLine"];
    [nullPlot setPlotSpace:plotSpace];
    [plot setPlotSpace:plotSpace];


    [graph addPlot:nullPlot];
    [graph addPlot:plot];

还有我的doubleForPlot 函数:

注意:我只添加这个函数来完成 - 它绝对 100% 返回两个图表的正确值!

-(double)doubleForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index{

// NULL CHART
if ([(NSString *)plot.identifier isEqualToString:@"nullLine"]) {
    if (fieldEnum == CPTScatterPlotFieldX) {
        NSLog(@"Plot: %@,fieldEnum: %d, index: %d",plot,fieldEnum,index);
        return index+1;
    }
    if (fieldEnum == CPTScatterPlotFieldY) {
        NSLog(@"Plot: %@,fieldEnum: %d, index: %d",plot,fieldEnum,index);
        double zero = 0;
        return zero;
    }
}


// BALANCE CHART
if ([(NSString *)plot.identifier isEqualToString:@"balanceChart"]) {

    if (fieldEnum == CPTScatterPlotFieldX) {

        NSLog(@"Axis: %d and Value: %d",fieldEnum, index+1);
        return (double)(index+1);
    }

    if (fieldEnum == CPTScatterPlotFieldY) {

        int dayInt = [dataHandler getDayNumber:[NSDate date]].intValue;
        double total = 0;
        for (Expense *exp in [allMonthExpenses objectAtIndex:index]) {
            total = total + exp.value.doubleValue;
        }

        NSDate *date = [NSDate dateWithTimeIntervalSinceNow:(-(dayInt-(int)index-1)*(60*60*24))];

        double budgetValue = [dataHandler getSpecificBudgetForDate:[NSDate dateWithTimeIntervalSinceNow:(-(dayInt-(int)index-1)*(60*60*24))] ];
        total = budgetValue+total;    

        NSLog(@"\n Axis:%d \n Record for Day: %@ \n IndexForPlot: %d \n Value: %.2f \n",fieldEnum, date,index,total);

        return total;
    }
}


return 0;
}

numberOfRecordsForPlot 方法来了:

注意两个图的记录数量相同。这也确实返回了正确的数字。

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot{    
return [dataHandler getDayNumber:[NSDate date]].intValue;

}

旁注请不要介意图表的格式;-)

【问题讨论】:

  • 问题到底是什么?
  • 在顶部的框中:问题:仅显示最后添加的图。我不能同时显示两者。问题显然是:我为什么以及哪里出错了=)我在上面添加了一个明确的问题-感谢您指出。

标签: iphone objective-c ios ios5 core-plot


【解决方案1】:

我在一个测试应用程序中尝试了您的代码,它运行良好——两个图都出现了。您的代码中一定还有其他内容。

【讨论】:

  • 那是我最害怕的事情。你知道是什么导致了这种行为吗?可能是问题在于我的托管视图作为 aubvie 添加到 tableview 中?
  • 我认为你让我找到了一个可能的解决方案。我在此处发布的代码是一个精简版本 - 我省略了所有轴声明等,显然没有它们它可以工作。生病尝试从这个起点(我的问题代码)逐步重建图表,直到它中断,然后我现在将是什么导致它。谢谢 =)
  • @SebastianFlückiger 您是否弄清楚是什么导致第一个图表无法正确显示?..因为我面临同样的问题..我有 3 个视图托管 3 个不同的图表,但只显示最后一个正确..任何线索都会很棒..谢谢!
  • @Eric Skroch 你能指导我解决这个问题吗..我从一天开始就一直在努力解决这个问题……真的很想知道答案,这样我才能继续前进..谢谢..
  • @ShikhaShah 提出一个新问题,详细说明您的情况。没有多少人会在此处看到您对已有一年多的问题的评论。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
相关资源
最近更新 更多