更改核心图中的y轴标签间隔

时间:2012-11-09 09:16:51

标签: iphone objective-c ios xcode core-plot

我在我的应用中为散点图实现了核心绘图库。但我想要显示Y轴标签,如1.0,2.0,3.0,4.0,5.0,..并相应地绘制图形。

当我更改y Label的间隔时,如下所示乘以25

NSInteger majorIncrement = 2;

NSInteger minorIncrement = 1;   

CGFloat yMax = 10.0f;// should determine dynamically based on max price    
NSMutableSet *yLabels = [NSMutableSet set];
NSMutableSet *yMajorLocations = [NSMutableSet set];
NSMutableSet *yMinorLocations = [NSMutableSet set];
for (NSInteger j = minorIncrement; j <= yMax; j += minorIncrement) {
    NSUInteger mod = j % majorIncrement;


    if (mod == 0) {
        CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:@"%i", j] textStyle:y.labelTextStyle];
        NSDecimal location = CPTDecimalFromInteger(j*25);  **//multiply with 25**`



       label.tickLocation = location;
        label.offset = -y.majorTickLength - y.labelOffset;

        if (label) {

            [yLabels addObject:label];
        }
        [yMajorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:location]];

    } else {
        [yMinorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromInteger(j)]];

    }
}
y.axisLabels = yLabels;    
y.majorTickLocations = yMajorLocations;
y.minorTickLocations = yMinorLocations;

但是当绘制图表时,它显示我错了。

例如如果我的分数是150,则它开始绘制为150/25 =第6点。

1 个答案:

答案 0 :(得分:2)

您可以设置orthogonalCoordinateDecimal,以确定图表开始的位置

 CPTXYAxis *y = axisSet.yAxis;
   y.orthogonalCoordinateDecimal = CPTDecimalFromFloat(150/25);

更进一步,设置plotRange,

 CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;   
   plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:(150/25) length:25];