kaiserfarrell / core-plot

Automatically exported from code.google.com/p/core-plot
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Second bar chart not drawing second plot #432

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
HI,

Developing financial modelling app, on first view have a graph that has two 
plot areas. Followed I think Eric's suggestion to make it look like a stacked 
bar and it works and looks as required. (see Image 1.png)

On second view (which is modelling view) exact same graph needs to be 
reproduced  but only one of the plot draws. Doesn't seem to be drawing the 
first plot which should be the greater (in height). (see Image 2.png).

I have copied the code that produces graph on Image 1 and still no resolution. 
I have checked and the correct data is being returned.

I am sure it is something I have done, any ideas would be great. 

Thanks Steve 

Code Below:

- (void)constructBarChart
{   
    int i = 0;
    double fAmtRequired = 0;
    int fTerm;
    int fIntervalAmount;

    //NSString *amtRequired;
    NSMutableArray *customTickLocations = [[NSMutableArray alloc]init];
    NSMutableArray *xAxisLabels= [[NSMutableArray alloc]init];

    // Create barChart from theme
    barChartBase = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
    CPTTheme *theme = [CPTTheme themeNamed:kCPTSlateTheme];
    [barChartBase applyTheme:theme];
    _graphHostingViewBase.hostedGraph = barChartBase;
    barChartBase.plotAreaFrame.masksToBorder = NO;

    barChartBase.paddingLeft = 75.0;
    barChartBase.paddingTop = 20.0;
    barChartBase.paddingRight = 20.0;
    barChartBase.paddingBottom = 80.0;

    //clear data arrays
    [xAxisLabels removeAllObjects];
    [customTickLocations removeAllObjects];
    [dataArray removeAllObjects];
    [dataArray2 removeAllObjects];

    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
    [numberFormatter setMinimumFractionDigits:0];
    ////    NSString *numberAsString = @"0";    
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    fTerm = appDelegate.modelTerm;

    if (([appDelegate.arrayGraphData count] /12) < fTerm) {
        fTerm = [appDelegate.arrayGraphData count] /12;   
    }

    LoanRecord *tRec = [[LoanRecord alloc]init];

    //Build data and Labels for graph
    fAmtRequired = 0;
    float fTotal = 0,fPrincipleTotal = 0;

    for (i=0; i< fTerm; i++) {
        [xAxisLabels addObject:[NSString stringWithFormat:@"%d",i+1]];
        [customTickLocations addObject :[NSDecimalNumber numberWithInt:i]];
        [dataArray addObject: @"0"];
        [dataArray2 addObject:@"0"];
    } 

    for (i=0; i< fTerm*12; i++) {
        tRec = [appDelegate.arrayGraphData objectAtIndex:i]; 

        //Graph Outstanding Balance

        fTotal  = fTotal + [tRec.Interest floatValue];
        fPrincipleTotal = fPrincipleTotal+[tRec.Principle floatValue];
        if (((i+1) % 12 == 0)&& (i > 0)){
            [dataArray replaceObjectAtIndex:((i+1)/12)-1 withObject:[NSString stringWithFormat:@"%f",fTotal]];
            [dataArray2 replaceObjectAtIndex:((i+1)/12)-1 withObject:[NSString stringWithFormat:@"%f",fPrincipleTotal]];
            if((fPrincipleTotal+fTotal)> fAmtRequired){
                fAmtRequired = (fPrincipleTotal+fTotal);
                fIntervalAmount = 100*(((int)(fAmtRequired/12)+50)/100);
            }
            fTotal = 0;
            fPrincipleTotal = 0;
        }      
    }

    // Add plot space for horizontal bar charts
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)barChartBase.defaultPlotSpace;
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(fAmtRequired+(fAmtRequired * 0.10))];
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-0.50f) length:CPTDecimalFromInt([dataArray count])];

    CPTXYPlotSpace *plotSpace2 = (CPTXYPlotSpace *)barChartBase.defaultPlotSpace;
    plotSpace2.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(fAmtRequired+(fAmtRequired * 0.10))];
    plotSpace2.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-0.50f) length:CPTDecimalFromInt([dataArray count])];

    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)barChartBase.axisSet;
    CPTXYAxis *x = axisSet.xAxis;

    x.axisLineStyle = nil;
    x.majorTickLineStyle = nil;
    x.minorTickLineStyle = nil;
    x.majorIntervalLength = CPTDecimalFromString(@"5");
    x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0");
    x.title = @"";
    x.titleLocation = CPTDecimalFromFloat(7.5f);
    x.titleOffset = 55.0f;
    x.labelRotation = M_PI/2;
    x.labelOffset = 2.0f;

    // Define some custom labels for the data elements
    x.labelingPolicy = CPTAxisLabelingPolicyNone;

    // Build X Axis Labels

    NSUInteger labelLocation = 0;
    NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]];  
    for (NSNumber *tickLocation in customTickLocations) {
        CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText: [xAxisLabels objectAtIndex:labelLocation++] textStyle:x.labelTextStyle];
        newLabel.tickLocation = [tickLocation decimalValue];
        newLabel.offset = x.labelOffset + x.majorTickLength;
        newLabel.rotation = M_PI/2;
        [customLabels addObject:newLabel];
    }

    x.axisLabels =  [NSSet setWithArray:customLabels];

    CPTXYAxis *y = axisSet.yAxis;
    y.axisLineStyle = nil;
    y.majorTickLineStyle = nil;
    y.minorTickLineStyle = nil;
    //    y.majorIntervalLength = CPDecimalFromFloat(fAmtRequired/[appDelegate.dataArray count]);

    y.majorIntervalLength = CPTDecimalFromInt(fIntervalAmount);
    y.labelFormatter = numberFormatter;
    y.orthogonalCoordinateDecimal = CPTDecimalFromString(@"-0.5");
    y.title = @"";
    y.titleOffset = 45.0f;
    y.titleLocation = CPTDecimalFromFloat(150.0f);

    CPTBarPlot *barPlot = [[CPTBarPlot alloc]init];
    //BarPlot - Red Interest
    //BarPlot2 - Green Principle + Interest

    CPTBarPlot *barPlot2 = [[CPTBarPlot alloc]init];

    // First bar plot
    barPlot2 = [CPTBarPlot tubularBarPlotWithColor:[CPTColor greenColor] horizontalBars:NO];
    barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor redColor] horizontalBars:NO];

    barPlot2.baseValue = CPTDecimalFromString(@"0");
    barPlot2.dataSource = self;
    barPlot2.barOffset = CPTDecimalFromFloat(0.00f);
    barPlot2.identifier = @"Bar Plot 2";
    barPlot2.barBasesVary = YES;
    barPlot2.title = @"Principle";
    [barChartBase addPlot:barPlot2];

    barPlot.baseValue = CPTDecimalFromString(@"0");
    barPlot.dataSource = self;
    barPlot.barOffset = CPTDecimalFromFloat(0.00f);
    barPlot.identifier = @"Bar Plot 1";
    barPlot.title = @"Interest";
    [barChartBase addPlot:barPlot];
}

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot 
{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    int fTerm;
    fTerm = appDelegate.modelTerm;

    if (([appDelegate.arrayGraphData count]/12) < fTerm) {
        fTerm = [appDelegate.arrayGraphData count]/12;   
    } 

    return fTerm;
}

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum 
recordIndex:(NSUInteger)index 
{
    NSDecimalNumber *num = nil;
    float Num1,num2 = 0;

    if ([plot isKindOfClass:[CPTBarPlot class]]){
        switch ( fieldEnum ) {
            case CPTBarPlotFieldBarLocation:{
                num = (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index];
                break;
            }   
            case CPTBarPlotFieldBarTip:
            {
                if(plot.identifier == @"Consolidated Graph"){
                   num = (NSDecimalNumber *)[newLoanDataArray objectAtIndex:index]; 
                }

                if (plot.identifier == @"Bar Plot 2"){
                   Num1 = [[dataArray2 objectAtIndex:index]doubleValue];
                   num2 = [[dataArray objectAtIndex:index]doubleValue];
                   num = (NSDecimalNumber *)[NSDecimalNumber numberWithFloat:Num1+num2];
                }

                if(plot.identifier == @"Bar Plot 1"){
                  num = (NSDecimalNumber *)[dataArray objectAtIndex:index];
                }

                if(plot.identifier == @"Consolidated Graph 1"){
                    Num1 = [[newLoanDataArray objectAtIndex:index]doubleValue];
                    num2 = [[newLoanDataArray objectAtIndex:index]doubleValue];
                    num = (NSDecimalNumber *)[NSDecimalNumber numberWithFloat:300000.00]; 
                }
                break;
            }
        }
    }
    return num;
}

-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index
{
    static CPTMutableTextStyle *whiteText = nil;

    if ( !whiteText ) {
        whiteText = [[CPTMutableTextStyle alloc] init];
        whiteText.color = [CPTColor whiteColor];
    }

    CPTTextLayer *newLayer = nil;

    if ( [plot isKindOfClass:[CPTPieChart class]] ) {
        switch ( index ) {
            case 0:
                newLayer = (id)[NSNull null];
                break;
            default:
                newLayer = [[CPTTextLayer alloc] initWithText:[NSString stringWithFormat:@"%lu", index] style:whiteText];
                break;
        }
    }
    else if ( [plot isKindOfClass:[CPTScatterPlot class]] ) {
        newLayer = [[CPTTextLayer alloc] initWithText:[NSString stringWithFormat:@"%lu", index] style:whiteText];
    }

    return newLayer;
}

Original issue reported on code.google.com by steve.be...@gmail.com on 6 May 2012 at 1:14

Attachments:

GoogleCodeExporter commented 8 years ago
When checking the plot identifier in the datasource methods, you should use 
-isEqualToString: instead of ==. It's possible one of those tests are failing 
because the compiler created more than one string constant with the same text.

Original comment by eskr...@mac.com on 6 May 2012 at 5:00

GoogleCodeExporter commented 8 years ago
HI
Yeah tried using -isEqualToString: instead of == and still the same.  As I said 
in the initial post I am sure it is something I have stuffed up but just can't 
see what. Thanks for the suggestion...

Original comment by steve.be...@gmail.com on 9 May 2012 at 12:38

GoogleCodeExporter commented 8 years ago
When barBasesVary is YES, the datasource will ask for three values: 
CPTBarPlotFieldBarLocation, CPTBarPlotFieldBarTip, and CPTBarPlotFieldBarBase. 
You're not providing the base values for the second bar plot. When the 
datasource returns nil for a data value, the plot won't draw that point.

Original comment by eskr...@mac.com on 9 May 2012 at 1:06

GoogleCodeExporter commented 8 years ago
Hi,

I said it was something I had done. Thanks for your help and yes that haas 
solved the issue. Can't believe that I missed it.

thanks

Original comment by steve.be...@gmail.com on 9 May 2012 at 1:29

GoogleCodeExporter commented 8 years ago

Original comment by eskr...@mac.com on 9 May 2012 at 10:55