2010-07-08 50 views
6

我有這個問題,因爲很長一段時間,我找不到任何東西來解決這個問題。我讀了幾個論壇,但無法找到工作解決方案。我有以下代碼:核心劇情:仍然不明白如何有自定義標籤和剔

// Adjust graph using above data     
CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace; 
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-10) length:CPDecimalFromFloat(xmax + 17)]; 
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-450) length:CPDecimalFromFloat(3750)]; 

// Setup axis 
CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet; 
CPLineStyle *lineStyle = [CPLineStyle lineStyle]; 
lineStyle.lineColor = [CPColor whiteColor]; 
lineStyle.lineWidth = 1.0f; 
CPTextStyle *cyanStyle = [CPTextStyle textStyle]; 
cyanStyle.color = [CPColor cyanColor]; 
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; 
[formatter setMaximumFractionDigits:0]; 

// x axis with custom labels 
axisSet.xAxis.labelingPolicy = CPAxisLabelingPolicyNone; 
axisSet.xAxis.majorTickLineStyle = lineStyle; 
axisSet.xAxis.axisLineStyle = lineStyle; 
axisSet.xAxis.majorTickLength = 3.0f; 
axisSet.xAxis.labelOffset = 3.0f; 

axisSet.xAxis.visibleRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromInteger(0) length:CPDecimalFromInteger(xmax)]; 
axisSet.xAxis.labelExclusionRanges = [NSArray arrayWithObjects: 
             [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-10) 
                    length:CPDecimalFromFloat(10)], 
             nil]; 

NSArray *customTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:0], 
           [NSDecimalNumber numberWithInt:6], 
           [NSDecimalNumber numberWithInt:12], 
           [NSDecimalNumber numberWithInt:18], 
           [NSDecimalNumber numberWithInt:24], 
           [NSDecimalNumber numberWithInt:30], 
           [NSDecimalNumber numberWithInt:36], 
           [NSDecimalNumber numberWithInt:41],            
           [NSDecimalNumber numberWithInt:47],            
           nil]; 

    // This return the labels (11h, 14h, ..., 22h, ..) 
NSArray *xAxisLabels = [GPHelpers getHoursList:fromStr to:toStr]; 

NSUInteger labelLocation = 0; 
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]]; 
for (NSNumber *tickLocation in customTickLocations) { 
    CPAxisLabel *newLabel = [[CPAxisLabel alloc] initWithText: [xAxisLabels objectAtIndex:labelLocation++] textStyle:axisSet.xAxis.labelTextStyle]; 
    newLabel.tickLocation = [tickLocation decimalValue]; 
    //newLabel.majorTickLocations = [tickLocation decimalValue]; 
    newLabel.offset = axisSet.xAxis.labelOffset + axisSet.xAxis.majorTickLength;     
    [customLabels addObject:newLabel]; 
    [newLabel release]; 
} 
axisSet.xAxis.axisLabels = [NSSet setWithArray:customLabels]; 

我看到標籤,但在x上沒有任何刻度。

+0

使用上面的代碼我看到蜱但不是標籤。你有什麼建議嗎? (@Cesare問) – jjnguy 2011-02-14 17:51:15

回答

8

就關閉這個問題,我有一個答案從核心情節的球員之一:

只需要設置刻度的位置,添加以下語句:

axisSet.xAxis.majorTickLocations = [NSSet setWithArray:customTickLocations]; 

問候, 呂克

+0

在哪裏我要添加這一行,我得到同樣的錯誤,我已經添加了5個標籤我能夠在屏幕上看到只有2個標籤,PLZ幫助我從這個問題出來,我已經花了很多時間。 – 2013-10-21 06:29:58