Who has working example of Stacked Bars?
-(void)setupBars
{
// Add plot space for horizontal bar charts
CPXYPlotSpace *barPlotSpace = [[CPXYPlotSpace alloc] init];
barPlotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-180.0f) length:CPDecimalFromFloat(360)];
barPlotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-7.0f) length:CPDecimalFromFloat(15.0f)];
[graph addPlotSpace:barPlotSpace];
[barPlotSpace release];
barPlotSpace.allowsUserInteraction = YES;
// First bar plot
CPTextStyle *whiteTextStyle = [CPTextStyle textStyle];
whiteTextStyle.color = [CPColor whiteColor];
CPBarPlot *barPlot = [CPBarPlot tubularBarPlotWithColor:[CPColor darkGrayColor] horizontalBars:YES];
barPlot.shouldStackBars = YES;
[barPlot setBarsAreHorizontal:YES];
barPlot.dataSource = self;
barPlot.barOffset = -0.25f;
barPlot.identifier = @"Bar Plot 1";
barPlot.plotRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromDouble(0.0) length:CPDecimalFromDouble(7.0)];
barPlot.barLabelTextStyle = whiteTextStyle;
[graph addPlot:barPlot toPlotSpace:barPlotSpace];
// Second bar plot
barPlot = [CPBarPlot tubularBarPlotWithColor:[CPColor blueColor] horizontalBars:YES];
barPlot.shouldStackBars = YES;
[barPlot setBarsAreHorizontal:YES];
barPlot.dataSource = self;
barPlot.barOffset = 0.25f;
barPlot.cornerRadius = 2.0;
barPlot.identifier = @"Bar Plot 2";
barPlot.plotRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromDouble(0.0) length:CPDecimalFromDouble(7.0)];
barPlot.delegate = self;
[graph addPlot:barPlot toPlotSpace:barPlotSpace];
}
-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
NSNumber *num;
switch ( fieldEnum ) {
case CPBarPlotFieldBarLocation: {
num = [plot.identifier isEqual:@"Bar Plot 2"]?[NSDecimalNumber numberWithInt:1]:[NSDecimalNumber numberWithInt:0];
return num;
}
break;
case CPBarPlotFieldBarLength: {
num = [NSDecimalNumber numberWithInt:[[pointsArray objectAtIndex:index] intValue]];
if ( [plot.identifier isEqual:@"Bar Plot 2"] )
num = [NSDecimalNumber numberWithInt:[[pointsArray objectAtIndex:index] intValue]*-1];
return num;
}
default:
assert(false);
break;
}
return nil;
}
Where's mistake?