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

CorePlotはまだこれを自動的に処理しません。プロット領域の使用可能な幅とラベルのサイズを使用して、各目盛りのラベルを表示するタイミングを決定し、適切な場所でカスタムラベルを省略する必要があります。

于 2013-02-08T01:48:19.527 に答える