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:
我是Core-plot的新手。我想在CPBarPlot上的每个条形列上添加关于数字的标签。我知道我必须实现CPBarPlotDataSource委托的这个方法:
-(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
4 个解决方案
#1
5
The labeling method changed recently. It's just a name change; it works the same as before. The new declaration is:
标签方法最近有所改变。这只是一个名称变化;它和以前一样工作。新声明是:
-(CPLayer *)dataLabelForPlot:(CPPlot *)plot recordIndex:(NSUInteger)index;
Also, this method should return autoreleased objects to prevent memory leaks.
此外,此方法应返回自动释放的对象以防止内存泄漏。
#2
1
Hope Someone will find this useful, Who doesn't know that how to display the label on top of the bar. I have implemented the CPTPlotDataSource as below,
希望有人会觉得这很有用,谁也不知道如何在栏顶上显示标签。我已经实现了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;
}
#3
1
Try this. (for core plot 1.6)
尝试这个。 (核心情节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;
}
#4
0
Method signature now changed to
方法签名现在更改为
-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index
- (CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index
or
-(NSArray *) dataLabelsForPlot:(CPTPlot *)plot recordIndexRange:(NSRange)indexRange
- (NSArray *)dataLabelsForPlot:(CPTPlot *)plot recordIndexRange:(NSRange)indexRange
#1
5
The labeling method changed recently. It's just a name change; it works the same as before. The new declaration is:
标签方法最近有所改变。这只是一个名称变化;它和以前一样工作。新声明是:
-(CPLayer *)dataLabelForPlot:(CPPlot *)plot recordIndex:(NSUInteger)index;
Also, this method should return autoreleased objects to prevent memory leaks.
此外,此方法应返回自动释放的对象以防止内存泄漏。
#2
1
Hope Someone will find this useful, Who doesn't know that how to display the label on top of the bar. I have implemented the CPTPlotDataSource as below,
希望有人会觉得这很有用,谁也不知道如何在栏顶上显示标签。我已经实现了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;
}
#3
1
Try this. (for core plot 1.6)
尝试这个。 (核心情节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;
}
#4
0
Method signature now changed to
方法签名现在更改为
-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index
- (CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index
or
-(NSArray *) dataLabelsForPlot:(CPTPlot *)plot recordIndexRange:(NSRange)indexRange
- (NSArray *)dataLabelsForPlot:(CPTPlot *)plot recordIndexRange:(NSRange)indexRange