我是Core-plot的新手。我想在CPBarPlot上的每个条形列上添加关于数字的标签。我知道我必须实现CPBarPlotDataSource委托的这种方法:
-(CPTextLayer *)barLabelForBarPlot:(CPBarPlot *)barPlot recordIndex:
(NSUInteger)index;
但即使我实施了这种方法,也没有发生任何事情,图表在每个条形图的顶部都没有标签显示。我想念一下吗?
这是我的代码:
- (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;
}
任何帮助都将不胜感激。
由于
答案 0 :(得分:5)
标签方法最近有所改变。这只是一个名称变化;它和以前一样工作。新声明是:
-(CPLayer *)dataLabelForPlot:(CPPlot *)plot recordIndex:(NSUInteger)index;
此外,此方法应返回自动释放的对象以防止内存泄漏。
答案 1 :(得分:1)
希望有人会觉得这很有用,谁也不知道如何在栏顶上显示标签。我已经实现了CPTPlotDataSource,如下所示,
-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)idx{
CPTMutableTextStyle *ts = [[CPTMutableTextStyle alloc] init];
ts.textAlignment = CPTTextAlignmentCenter;
ts.color = [CPTColor redColor];
CPTTextLayer *tl = [[CPTTextLayer alloc] initWithText:@"5" style:ts];
return tl;
}
答案 2 :(得分:1)
试试这个。 (对于core plot 1.6)
-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index;
{
CPTMutableTextStyle *ts = [[CPTMutableTextStyle alloc] init];
ts.textAlignment = CPTTextAlignmentCenter;
ts.color = [CPTColor blackColor];
ts.fontSize = 12;
plot.labelOffset = 0;
CPTTextLayer *tl = [[CPTTextLayer alloc] initWithText:[NSString stringWithFormat:@"%@",index] style:ts];
return tl;
}
答案 3 :(得分:0)
方法签名现已更改为
- (CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index
或
- (NSArray *)dataLabelsForPlot:(CPTPlot *)plot recordIndexRange:(NSRange)indexRange