在我的iPhone應用程序中,我有兩個單獨的條形圖的視圖。每個圖都有不同數量的數據元素。我想用適當的數據在每個圖中標出每個條,但顯然「dataLabelForPlot」方法只允許一個「CPTTextLayer」實例。我想讓CPTTextLayer成爲劇情及其數據的功能。Core-Plot:條形圖的數據標籤(2張圖,2套標籤)?
這些圖有獨立的標識符:「條形圖1」和「條形圖2」。我可以在「numberOfRecordsForPlot」和「numberForPlot」方法中使用條件if語句,但是它在「dataLabelForPlot」方法中失敗,錯誤表明「label」是未使用的變量。看到它在條件語句中被定義,似乎很奇怪。
我的代碼如下所示:
#pragma mark Plot Data Source Methods
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
if (plot.identifier == @"Bar Plot 1") {
return 5; }
else { return 4; }
}
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
NSDecimalNumber *num = nil;
if (plot.identifier == @"Bar Plot 1") {
switch (fieldEnum) {
case CPTBarPlotFieldBarLocation:
num = (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index];
break;
case CPTBarPlotFieldBarTip:
num = (NSDecimalNumber *)[dataTemp1 objectAtIndex:index];
break;
}
}
else {
switch (fieldEnum) {
case CPTBarPlotFieldBarLocation:
num = (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index];
break;
case CPTBarPlotFieldBarTip:
num = (NSDecimalNumber *)[dataTemp2 objectAtIndex:index];
break;
}
}
return num;
}
-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index {
if (plot.identifier == @"Bar Plot 1") {
CPTTextLayer *label = [[CPTTextLayer alloc] initWithText:[NSString stringWithFormat:@"%@", [dataTemp1 objectAtIndex:index]]];
}
else { CPTTextLayer *label = [[CPTTextLayer alloc] initWithText:[NSString stringWithFormat:@"%@", [dataTemp2 objectAtIndex:index]]]; }
CPTMutableTextStyle *textStyle = [label.textStyle mutableCopy];
textStyle.color = [CPTColor redColor];
label.textStyle = textStyle;
[textStyle release];
return [label autorelease];
}
驚人,埃裏克。我早些時候嘗試過,沒有運氣。現在,你的業力很好,它的作用。太感謝了。 –