1

我正在使用自定义标签,当 x 轴标签施加在另一个 x 轴标签上时我遇到了问题,当用户缩小散点图(实时)时,我不知道如何隐藏这些标签。
请参阅下面的打印屏幕:我想隐藏“2012 年 8 月”标签。
我怎样才能做到这一点?

在此处输入图像描述

下面是我正在使用的代码:

    CPTXYAxis *x          = axisSet.xAxis;
    x.orthogonalCoordinateDecimal = CPTDecimalFromInteger(0);
    x.majorIntervalLength         = CPTDecimalFromInteger(150);
    x.minorTicksPerInterval       = 5;
    x.axisConstraints             = [CPTConstraints constraintWithLowerOffset:0.0f];
    x.labelingPolicy=CPTAxisLabelingPolicyNone;
    NSUInteger labelLocation = 0;
    NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[objects count]];
    NSMutableSet *xMajorLocations = [NSMutableSet setWithCapacity:[objects count]];
    for (NSInteger i = 0; i < [objects count]; i++) {
        NSManagedObject *theLine = [objects objectAtIndex:i];

        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        NSString *sPeriodText = @"";

        [dateFormatter setDateFormat:@"MMMM yyyy"];
        sPeriodText = [dateFormatter stringFromDate:[theLine valueForKey:@"period_start"]];

        CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:sPeriodText textStyle:labelTextStyle];
        newLabel.tickLocation  = CPTDecimalFromInteger(labelLocation++);
        newLabel.offset = x.labelOffset + x.majorTickLength;
        [customLabels addObject:newLabel];
        [xMajorLocations addObject:[NSNumber numberWithFloat:labelLocation-1]];
    }
    x.axisLabels =  [NSSet setWithArray:customLabels];
    x.majorTickLocations = xMajorLocations;

谢谢!

PS 我试图使用 CPTAxis 的 labelExclusionRanges,但它不适用于自定义标签。

4

2 回答 2

3

了解如何制作它。因为我使用日期格式作为 x 轴标签,所以我需要使用 CPTTimeFormatter + preferredNumberOfMajorTicks,而不是自定义标签!

这是下面的代码:

...
    NSManagedObject *theLineFirst = [objects objectAtIndex:0];
    NSManagedObject *theLineLast  = [objects objectAtIndex:[objects count]-1];
    NSDate *refDate = [NSDate dateWithTimeInterval:0 sinceDate:[theLineFirst valueForKey:@"period_start"]];
    NSTimeInterval secondsBetween = [[theLineLast valueForKey:@"period_start"] timeIntervalSinceDate:[theLineFirst valueForKey:@"period_start"]];
    NSTimeInterval onePart        = secondsBetween/[objects count];

    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
    plotSpace.delegate = self;
    plotSpace.allowsUserInteraction = YES;
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(-onePart) length:CPTDecimalFromInteger(secondsBetween+onePart*2)];
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(minCData) length:CPTDecimalFromInteger(abs(maxCData-minCData))];
    plotSpace.globalXRange = plotSpace.xRange;
    plotSpace.globalYRange = plotSpace.yRange;

    // Axes
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
    CPTXYAxis *x          = axisSet.xAxis;
    x.orthogonalCoordinateDecimal = CPTDecimalFromInteger(0);
    x.majorIntervalLength         = CPTDecimalFromInteger(5);
    x.minorTicksPerInterval       = 0;
    x.axisConstraints             = [CPTConstraints constraintWithLowerOffset:0.0f];
    x.labelingPolicy              = CPTAxisLabelingPolicyAutomatic;
    // added for date
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MMMM yyyy"];
    CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter];
    timeFormatter.referenceDate     = refDate;
    axisSet.xAxis.labelFormatter    = timeFormatter;
    x.preferredNumberOfMajorTicks = 4;
...

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
    return [objects count];
}

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
    NSManagedObject *theLineFirst = [objects objectAtIndex:0];
    NSManagedObject *theLine = [objects objectAtIndex:index];
    NSTimeInterval secondsBetween = [[theLine valueForKey:@"period_start"] timeIntervalSinceDate:[theLineFirst valueForKey:@"period_start"]];

    switch (fieldEnum) {
        case CPTScatterPlotFieldX:
            return [NSNumber numberWithDouble:secondsBetween];

        case CPTScatterPlotFieldY:
            return [NSNumber numberWithDouble:[[theLine valueForKey:@"cdata"] doubleValue]];
    }
    return [NSDecimalNumber zero];
}

就这样!

于 2013-02-09T00:47:55.297 回答
0

Core Plot 还没有自动处理这个问题。您必须使用绘图区域的可用宽度和标签的大小来决定何时显示每个刻度线的标签并在适当的位置省略自定义标签。

于 2013-02-08T01:48:19.527 回答