2013-06-18 182 views
0

我碰到的與iOS和核心情節正確顯示我的Y軸標籤的一些問題...核心情節和顯示ÿ標籤

我會告訴你兩種不同的情景我現在有,並希望有人可以幫助我得到正常工作其中之一......

好了,所以第一種方案: 代碼:

// Configure y-axis 
CPTAxis *y = axisSet.yAxis; 
y.axisLineStyle = axisLineStyle; 
y.majorGridLineStyle = customGridStyle; 
y.labelingPolicy = CPTAxisLabelingPolicyAutomatic; 
y.labelTextStyle = axisTextStyle; 
y.majorTickLineStyle = axisLineStyle; 
y.majorTickLength = 0.0f; 
y.minorTickLength = 0.0f; 
y.tickDirection = CPTSignNegative; 
NSInteger majorIncrement = [self findStep:(maxValue - minValue)/7]; 
CGFloat yMax = [self findMaximumValue:maxValue]; 
NSMutableSet *yLabels = [NSMutableSet set]; 
NSMutableSet *yMajorLocations = [NSMutableSet set]; 

int maxLocation = 0; 

for (NSInteger j = 0; j <= yMax + majorIncrement; j += majorIncrement) { 
    CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:@"%i", j] textStyle:y.labelTextStyle]; 
    NSDecimal location = CPTDecimalFromInteger(j); 
    label.tickLocation = location; 
    label.offset = 125; 

    if (label) { 
     [yLabels addObject:label]; 
    } 

    [yMajorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:location]]; 
    if (maxLocation < [[NSDecimalNumber decimalNumberWithDecimal:location] intValue]) { 
     maxLocation = [[NSDecimalNumber decimalNumberWithDecimal:location] intValue]; 
    } 
} 

y.gridLinesRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(0) length:CPTDecimalFromInteger(maxLocation)]; 
y.axisLabels = yLabels; 
y.majorTickLocations = yMajorLocations; 

現在這個代碼的工作在一定程度上...查看我下面的截圖: Core PlotCore Plot 2

在這裏,我很想改變部分是小數......我想改變小數點的位置到現在顯示.0 ...

  • 場景(我真的很喜歡的那個) 爲了節省閱讀下面的代碼的麻煩,我可以指出這裏的大變化是:

    y.labelingPolicy = CPTAxisLabelingPolicyNone;

  • 代碼:

    // Configure y-axis 
    CPTAxis *y = axisSet.yAxis; 
    y.axisLineStyle = axisLineStyle; 
    y.majorGridLineStyle = customGridStyle; 
    y.labelingPolicy = CPTAxisLabelingPolicyNone; 
    y.labelTextStyle = axisTextStyle; 
    y.majorTickLineStyle = axisLineStyle; 
    y.majorTickLength = 0.0f; 
    y.minorTickLength = 0.0f; 
    y.tickDirection = CPTSignPositive; 
    NSInteger majorIncrement = [self findStep:(maxValue - minValue)/7]; 
    CGFloat yMax = [self findMaximumValue:maxValue]; 
    NSMutableSet *yLabels = [NSMutableSet set]; 
    NSMutableSet *yMajorLocations = [NSMutableSet set]; 
    
    int maxLocation = 0; 
    
    for (NSInteger j = 0; j <= yMax + majorIncrement; j += majorIncrement) { 
        CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:@"%i", j] textStyle:y.labelTextStyle]; 
        NSDecimal location = CPTDecimalFromInteger(j); 
        label.tickLocation = location; 
        label.offset = -25; 
    
        if (label) { 
         [yLabels addObject:label]; 
        } 
    
        [yMajorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:location]]; 
        if (maxLocation < [[NSDecimalNumber decimalNumberWithDecimal:location] intValue]) { 
         maxLocation = [[NSDecimalNumber decimalNumberWithDecimal:location] intValue]; 
        } 
    } 
    
    y.gridLinesRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(0) length:CPTDecimalFromInteger(maxLocation)]; 
    y.axisLabels = yLabels; 
    y.majorTickLocations = yMajorLocations; 
    x.gridLinesRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(0) length:CPTDecimalFromInteger(maxLocation)]; 
    

    ,情節出來爲:

    Core Plot 3Core Plot 4Core Plot 5

    現在這裏有很多Ÿ軸系失蹤......

    任何幫助將不勝感激!

    回答

    2

    使用CPTAxisLabelingPolicyNone標籤政策,您可以完全控制勾號位置和標籤。您需要決定要製作多少個刻度和標籤,以及將它們放在哪裏。

    如果你想要做的是改變數字格式的軸,保持CPTAxisLabelingPolicyAutomatic標籤策略(或除CPTAxisLabelingPolicyNone其他的任何一個),刪除,創建刻度位置和標籤的代碼,並更改labelFormatter。您可以使用任何NSFormatter。創建一個NSNumberFormatter,將其配置爲顯示所需的數字格式,並將其設置爲labelFormatter屬性。

    +0

    謝謝,埃裏克!我真的很想用無策略創建標籤,在那裏我可以更好地控制步驟等,但現在我使用了格式化labelFormatter的解決方案,直到我使其他部分更好地工作......謝謝! – user969043