2013-06-18 143 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