2010-05-25 27 views

回答

35

不幸的是,由於框架的API尚未穩定,所以沒有那麼多的教程,其中一些已經存在的教程已經過時。示例應用程序確實是瞭解如何使用框架的最佳資源。

例如,要提供自定義軸標籤,請查看CPTestApp-iPhone的CPTestAppBarChartController.m源文件。在這個例子柱形圖通過下面的代碼定義的自定義X軸標籤:

// Define some custom labels for the data elements 
x.labelRotation = M_PI/4; 
x.labelingPolicy = CPAxisLabelingPolicyNone; 
NSArray *customTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:1], [NSDecimalNumber numberWithInt:5], [NSDecimalNumber numberWithInt:10], [NSDecimalNumber numberWithInt:15], nil]; 
NSArray *xAxisLabels = [NSArray arrayWithObjects:@"Label A", @"Label B", @"Label C", @"Label D", @"Label E", nil]; 
NSUInteger labelLocation = 0; 
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]]; 
for (NSNumber *tickLocation in customTickLocations) { 
    CPAxisLabel *newLabel = [[CPAxisLabel alloc] initWithText: [xAxisLabels objectAtIndex:labelLocation++] textStyle:x.labelTextStyle]; 
    newLabel.tickLocation = [tickLocation decimalValue]; 
    newLabel.offset = x.labelOffset + x.majorTickLength; 
    newLabel.rotation = M_PI/4; 
    [customLabels addObject:newLabel]; 
    [newLabel release]; 
} 

x.axisLabels = [NSSet setWithArray:customLabels]; 

首先,設置軸標籤政策CPAxisLabelingPolicyNone讓框架知道你會提供自定義標籤,然後創建一個最後您將該標籤數組分配給X軸上的axisLabels屬性。

+0

我們如何爲餅圖中的每個切片添加標籤?謝謝 – Pooja 2011-05-18 14:42:44

+0

customTickLocations包含5,10 ...它與x軸範圍有關嗎?因爲我有幾秒鐘的x軸包含值,那麼在我的情況下customTickLocations中的值是什麼..謝謝 – Pooja 2011-06-16 15:57:55

+1

@Pooja自定義標籤可以以與答案完全相同的方式添加到餅圖中...您是歡迎。你可以在[這個SO答案]中提到的自定義勾號位置工作(http://stackoverflow.com/questions/3208025/core-plot-still-do-not-understand-how-to-have-custom-label-和 - 滴答聲)...不客氣! – tipycalFlow 2012-03-19 10:59:53

0

要提供自定義格式化標籤,您可以將一個格式化程序分配給軸的labelFormatter屬性。您可以使用配置您的喜好NSNumberFormatter,例如:

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; 
// ... configure formatter 

CPTXYAxis *y = axisSet.yAxis; 
y.labelFormatter = formatter; 

或者,如果你需要更具體的格式,你可以繼承NSNumberFormatter並重寫stringForObjectValue:方法做你想確切的格式。