2010-08-18 80 views
1

我是新的Core-plot。我想添加有關CPBarPlot上每個欄列上的數字的標籤。我知道,我必須實現CPBarPlotDataSource委託的這個方法:Core-plot標籤不顯示在每個欄柱的頂部

-(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; 
} 

任何幫助,將不勝感激。

謝謝

回答

5

標籤方法最近改變了。這只是一個名稱的變化;它和以前一樣工作。新的聲明是:

-(CPLayer *)dataLabelForPlot:(CPPlot *)plot recordIndex:(NSUInteger)index; 

此外,此方法應返回自動釋放對象以防止內存泄漏。

+0

不,它仍然是「barLabelForBarPlot」方法。因爲我剛從CorePlot網站下載它。我尋找CPBarPlot類,它有「barLabelForBarPlot」方法。無論如何,謝謝你的回覆。 – haisergeant 2010-08-18 14:06:26

+0

感謝Eric對我的完美工作。 – Pooja 2011-06-30 15:58:56

+0

是否有可能與標籤顏色相同的條形圖與核心圖,我可以顯示條形圖,但有一些默認的藍色。非常感謝您的閱讀。我希望有一些事情與此相關你知道。 – Pooja 2011-06-30 16:28:46

0

方法簽名現在改爲

- (CPTLayer *)dataLabelForPlot:(CPTPlot *)情節recordIndex:(NSUInteger)指數

- (NSArray的* )dataLabelsForPlot:(CPTPlot *)plot recordIndexRange:(NSRange)indexRange

1

希望有人會覺得這很有用,誰不知道如何在酒吧頂部顯示標籤。我已經實現了CPTPlotDataSource如下,

enter image description here

-(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; 
} 
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; 
}