ios
ios

Reputation: 6164

CORE PLOT add one more Graph in Vertical BarChart

Hi I want to make bar chart as shown here

enter image description here

I want to plot graph using coreplot example : (coreplot gallery- Vertical Bar Chart)

Currently default code plots the graph as shown below

enter image description here

How can I achieve this?

I tried adding another graph but didn't work out well.

Please Help and Suggest

Thanks

Upvotes: 0

Views: 1809

Answers (2)

Charles
Charles

Reputation: 414

This is how I do this:

    // Define some custom labels for the data elements

customTickLocations = [NSArray arrayWithObjects: 
                           [NSDecimalNumber numberWithInt:10], 
                           [NSDecimalNumber numberWithInt:20],
                           [NSDecimalNumber numberWithInt:30],
                           [NSDecimalNumber numberWithInt:40],
                           [NSDecimalNumber numberWithInt:50],
                           [NSDecimalNumber numberWithInt:60],
                           [NSDecimalNumber numberWithInt:70],
                           [NSDecimalNumber numberWithInt:80],
                           [NSDecimalNumber numberWithInt:90],
                           [NSDecimalNumber numberWithInt:100],
                           nil];

NSArray *xAxisLabels = [NSArray arrayWithObjects:@"10%", @"20%", @"30%",
                            @"40%", @"50%", @"60%",@"70%",@"80%",@"90%",@"100%", nil];

labelLocation = 0;
customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]];

for (NSNumber *tickLocation in customTickLocations) {
    CPAxisLabel *newLabel = [[CPAxisLabel alloc] initWithText: [xAxisLabels objectAtIndex:labelLocation++] 
                                                    textStyle:y.labelTextStyle];
    newLabel.tickLocation = [tickLocation decimalValue];
    newLabel.offset = y.labelOffset;
    [customLabels addObject:newLabel];
    //[newLabel release];
}

y.axisLabels =  [NSSet setWithArray:customLabels];
y.majorTickLocations =  [NSSet setWithArray:customTickLocations];

You define 2 array, one with the labels and one with the values where to put them, define "CPXAxisLabel"s and put them in another array, and this array you can put on your graph. Hope this helps.

Upvotes: 1

Charles
Charles

Reputation: 414

Here is my solution: enter image description here

In your code you need to add the following:

-(NSNumber *)numberForPlot:(CPPlot *)plot
                 field:(NSUInteger)fieldEnum
           recordIndex:(NSUInteger)index
{
    if (plot.identifier==@"locked") {
        switch ( fieldEnum ) {
            case CPBarPlotFieldBarLocation:
                return (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index];
            case CPBarPlotFieldBarLength:
                return [self.lockedPercentage objectAtIndex:index];
            }
    }else if (plot.identifier==@"occupied") {
        switch ( fieldEnum ) {
            case CPBarPlotFieldBarLocation:
                return (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index];
            case CPBarPlotFieldBarLength:
                return [self.occupiedPercentage objectAtIndex:index];
        }
    }else if (plot.identifier==@"empty") {
        switch ( fieldEnum ) {
            case CPBarPlotFieldBarLocation:
                return (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index];
            case CPBarPlotFieldBarLength:
                return [self.emptyPercentage objectAtIndex:index];
        }
    }
    return nil;
}

The lines plot.identifier are here important, they check which data to take, here occupied empty or locked.

Of course you need to set these identifiers, I do this in a -(void) loadgraph:

//  Bar plot Locked
CPBarPlot *barPlot = [CPBarPlot tubularBarPlotWithColor:[CPColor blueColor] horizontalBars:NO];
barPlot.dataSource = self;
barPlot.baseValue = CPDecimalFromString(@"0");
barPlot.barOffset = 0.0f+viewOffset;
barPlot.barWidth = 15.0f;
barPlot.identifier = @"occupied";

[graph addPlot:barPlot toPlotSpace:plotSpace];

Here one of my identifier is set. To display values below the x-axe you need to change your range:

    plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0f) length:CPDecimalFromFloat(100.0f)];

Hope this helps.

Upvotes: 1

Related Questions