Sebastian Flückiger
Sebastian Flückiger

Reputation: 5555

coreplot 2 plots in graph

im currently creating a graph for an app, using coreplot and have a problem i cant explain.

i read (and it didnt help): core-plot, unable to plot two graphs

QUESTION: why does only the second added plot get displayed? if i change the order i add them to the hostView, only the last one i add is displayed. see code & screenshots below.

the graph will hold 2 different plots

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.

First Plot a single 0 line

identifier : @"nullLine"
name: nullPlot

Second Plot a scatterplot of data i have stored

identifier : @"balanceChart"
name: plot

here is some of my code:

    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];

and my doubleForPlot function:

NOTE: i only add this function for completion - it does absolutely to 100% return the right values for both graphs!

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

here comes the numberOfRecordsForPlot method:

NOTE both plots have the same amount of records. this does also return the right number.

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

}

balanceChart

nullLine

sidenote dont mind the formatting of the plots please ;-)

Upvotes: 2

Views: 1108

Answers (1)

Eric Skroch
Eric Skroch

Reputation: 27381

I tried your code in a test app and it worked fine—both plots showed up. There must be something else going on in your code.

Upvotes: 1

Related Questions