0

我在使用 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;

现在这段代码在一定程度上可以工作......见下面我的截图: 核心情节 核心情节2

这里我想更改的部分是小数点...我想将位置小数点更改为现在显示 .0...

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

    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)];

情节如下:

核心情节3核心情节4核心情节5

现在这里很多y轴都不见了......

任何帮助将不胜感激!

4

1 回答 1

2

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

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

于 2013-06-19T01:43:22.703 回答