核心图:标签策略不是自动时,不会出现网格线

时间:2013-05-22 02:51:00

标签: ios core-plot

我试图在iPhone App中使用Core Plot绘制简单的统计数据。我最初使用标签政策作为两个轴的CPTAxisLabelingPolicyAutomatic,并且网格线在这里看起来很好:

enter image description here

您可以看到图表包含垂直和水平网格线。然后,我将标签政策更改为

    axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyNone;
    axisSet.yAxis.labelingPolicy = CPTAxisLabelingPolicyAutomatic;

NSUInteger labelLocation = 0;
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:xAxisLabels.count];
CPTMutableTextStyle *textStyle = [[CPTMutableTextStyle alloc] init];
textStyle.color = [CPTColor lightGrayColor];

for (NSNumber *tickLocation in customTickLocations) {
    CPTAxisLabel *newLabel = [[CPTAxisLabel alloc]
                              initWithText:[NSString stringWithFormat:@"%@",(NSDate *)[xAxisLabels objectAtIndex:labelLocation++]] textStyle:textStyle];
    newLabel.tickLocation = [tickLocation decimalValue];
    newLabel.rotation = 25.0;
    newLabel.offset = 10.0;
    [customLabels addObject:newLabel];
}
axisSet.xAxis.axisLabels =  [NSSet setWithArray:customLabels];

但是,因为当我为X轴添加自定义标签时,X轴网格线(即垂直线)没有出现。只能看到水平网格线,如下面的App截图屏幕截图所示:

enter image description here

2 个答案:

答案 0 :(得分:8)

CPTAxisLabelingPolicyNone标签政策不会创建任何标签或刻度线。您需要创建一组位置并设置majorTickLocations和/或minorTickLocations

以下是 Plot Gallery 示例应用程序中的一些示例代码:

NSSet *majorTickLocations = [NSSet setWithObjects:[NSDecimalNumber zero],
                             [NSDecimalNumber numberWithUnsignedInteger:30],
                             [NSDecimalNumber numberWithUnsignedInteger:50],
                             [NSDecimalNumber numberWithUnsignedInteger:85],
                             [NSDecimalNumber numberWithUnsignedInteger:100],
                             nil];
xAxis.majorTickLocations = majorTickLocations;

刻度线位置通常与标签位置相同,但不一定是。

答案 1 :(得分:3)

谢谢你,@ Erik Skroch!对我有用的是标签政策的简单改变

axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyNone;

 axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyFixedInterval;

这是由你的声明煽动的: “CPTAxisLabelingPolicyNone标签政策不会创建任何标签或刻度线。”