核心图:轴标签相互强加

时间:2013-02-07 01:58:11

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

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

enter image description here

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

    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;

谢谢!

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

2 个答案:

答案 0 :(得分: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];
}

就是这样!

答案 1 :(得分:0)

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