Hi everyone,
I am new with Core-plot. I would like to add label about the number on each of bar column on CPBarPlot. I knew that I must implement this method of CPBarPlotDataSource delegate:
-(CPTextLayer *)barLabelForBarPlot:(CPBarPlot *)barPlot recordIndex:
(NSUInteger)index;
But even I implemented that method, nothing happen, the graph displays without label on top of each bar. Do I miss something?
This is my code:
- (void) constructBarChart {
//create bar chart from theme
barChart = [[CPXYGraph alloc] initWithFrame:CGRectZero];
CPTheme *theme = [CPTheme themeNamed:kCPPlainWhiteTheme];
[barChart applyTheme:theme];
hostView.hostedLayer = barChart;
barChart.plotAreaFrame.masksToBorder = NO;
barChart.paddingTop = 30.0;
barChart.paddingBottom = 30.0;
barChart.paddingLeft = 30.0;
barChart.paddingRight = 30.0;
// setup plot space
CPXYPlotSpace *plotSpace = (CPXYPlotSpace*)barChart.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
plotSpace.delegate = self;
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0f) length:CPDecimalFromFloat(10.0f)];
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0f) length:CPDecimalFromFloat(10.0f)];
plotSpace.globalXRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0f) length:CPDecimalFromFloat(20.0f)];
plotSpace.globalYRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0f) length:CPDecimalFromFloat(20.0f)];
// Grid line styles
CPLineStyle *majorGridLineStyle = [CPLineStyle lineStyle];
majorGridLineStyle.lineWidth = 0.75;
majorGridLineStyle.lineColor = [[CPColor colorWithGenericGray:0.2]
colorWithAlphaComponent:0.75];
CPLineStyle *minorGridLineStyle = [CPLineStyle lineStyle];
minorGridLineStyle.lineWidth = 0.25;
minorGridLineStyle.lineColor = [[CPColor whiteColor]
colorWithAlphaComponent:0.1];
CPLineStyle *redLineStyle = [CPLineStyle lineStyle];
redLineStyle.lineWidth = 10.0;
redLineStyle.lineColor = [[CPColor redColor]
colorWithAlphaComponent:0.5];
//Axis
CPXYAxisSet *axisSet = (CPXYAxisSet*)barChart.axisSet;
CPXYAxis *x = axisSet.xAxis;
CPLineStyle *lineStyle = [CPLineStyle lineStyle];
lineStyle.lineColor = [CPColor blackColor];
x.axisLineStyle = lineStyle;
x.majorIntervalLength = CPDecimalFromString(@"5");
x.minorTicksPerInterval = 4;
CPXYAxis *y = axisSet.yAxis;
y.axisLineStyle = lineStyle;
y.majorIntervalLength = CPDecimalFromString(@"1");
y.majorGridLineStyle = majorGridLineStyle;
y.isFloatingAxis = YES;
CPBarPlot *barPlot = [CPBarPlot tubularBarPlotWithColor:[CPColor redColor] horizontalBars:NO];
barPlot.baseValue = CPDecimalFromString(@"0");
barPlot.dataSource = self;
barPlot.delegate = self;
barPlot.identifier = @"Bar Plot 1";
[barChart addPlot:barPlot toPlotSpace: plotSpace];
}
- (NSUInteger) numberOfRecordsForPlot:(CPPlot*)plot {
if ([plot isKindOfClass:[CPBarPlot class]]) {
return 16;
} else {
return [dataGraph count];
}
}
- (NSNumber *)numberForPlot: (CPPlot*) plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger) index {
NSDecimalNumber *num = nil;
if ([plot isKindOfClass:[CPBarPlot class]]) {
switch (fieldEnum) {
case CPBarPlotFieldBarLocation:
num = (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index];
break;
case CPBarPlotFieldBarLength:
num = (NSDecimalNumber *) [NSDecimalNumber numberWithUnsignedInteger:(index + 1)];
break;
}
} else {
num = [[dataGraph objectAtIndex:index] valueForKey:(fieldEnum == CPScatterPlotFieldX ? @"x" : @"y")];
}
return num;
}
#pragma mark -
#pragma mark Plot Space Delegate Methods
-(CGPoint)plotSpace:(CPPlotSpace *)space willDisplaceBy:(CGPoint)proposedDisplacementVector {
CGPoint result = CGPointMake(proposedDisplacementVector.x, 0.0);
return result;
}
#pragma mark -
#pragma mark Bar Plot Delegate Methods
- (CPTextLayer *) barLabelForBarPlot:(CPBarPlot *) barPlot recordIndex:(NSUInteger) index {
CPTextLayer *textLayer = [[CPTextLayer alloc] initWithText:[NSString stringWithFormat:@"%d", index+1]];
return textLayer;
}
Any help would be appreciated.
Thanks