我是Core-plot的新手.我想在CPBarPlot上的每个条形列上添加关于数字的标签.我知道我必须实现CPBarPlotDataSource委托的这个方法:
-(CPTextLayer *)barLabelForBarPlot:(CPBarPlot *)barPlot recordIndex:
(NSUInteger)index;
Run Code Online (Sandbox Code Playgroud)
但即使我实现了这种方法,也没有任何反应,图表在每个条形图的顶部都没有标签显示.我错过了什么吗?
这是我的代码:
- (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;
}
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激.
谢谢
标签方法最近有所改变.这只是一个名称变化; 它和以前一样工作.新声明是:
-(CPLayer *)dataLabelForPlot:(CPPlot *)plot recordIndex:(NSUInteger)index;
Run Code Online (Sandbox Code Playgroud)
此外,此方法应返回自动释放的对象以防止内存泄漏.