核心情节并显示y标签

时间:2013-06-18 15:23:53

标签: ios core-plot

我在使用iOS和Core Plot正确显示我的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 Plot Core Plot 2

这里我想改变的部分是十进制...我想改变位置十进制现在显示一个.0 ......

  1. 场景(我真的很想修复的那个) 为了省去阅读以下代码的麻烦,我可以指出这里的重大变化是

    y.labelingPolicy = CPTAxisLabelingPolicyNone;

  2. 代码:

    // 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 3 Core Plot 4 Core Plot 5

    现在这里有很多y轴缺失...

    非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

使用CPTAxisLabelingPolicyNone标签政策,您可以完全控制刻度线位置和标签。由您来决定要制作多少刻度和标签以及放置它们的位置。

如果您要做的只是更改轴上的数字格式,请保留CPTAxisLabelingPolicyAutomatic标签政策(或除CPTAxisLabelingPolicyNone之外的其他任何一项),删除创建代码位置的代码,标签,并更改labelFormatter。您可以使用任何NSFormatter。创建NSNumberFormatter,将其配置为显示所需的数字格式,并将其设置为labelFormatter属性。