2013-02-07 2 views
1

사용자 정의 레이블을 사용하고 있으며 x 축 레이블이 다른 x 축 레이블에 부과 될 때 문제가 발생하며 사용자가 축소 할 때 해당 레이블을 숨기는 방법을 찾을 수 없습니다 (실시간).
아래의 프린트 스크린 참조 : "August 2012"레이블을 숨기고 싶습니다.
어떻게하면됩니까? 코드 아래에 다음코어 플롯 : 축 레이블이 서로 부과합니다

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. CPTAxis의 labelExclusionRanges를 사용하려했지만 사용자 정의 레이블에서 작동하지 않았습니다.

답변

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]; 
} 

그리고 그게 다야!

+0

이 솔루션은 레이블이 라인 노드와 일치하지 않는다는 점에 유의하고 싶습니다. 그래서 사용자 정의 라벨을 사용해야 할 필요가 있다고 생각합니다. – AlexeyVMP

0

코어 플롯이 자동으로 처리하지 않습니다. 플롯 영역의 너비와 레이블의 크기를 사용하여 각 눈금에 레이블을 표시 할시기를 결정하고 적절한 위치에서 사용자 정의 레이블을 생략해야합니다.

+0

맞춤 라벨에 대해서는 더 이상 다루지 않습니다. 사용자가 플롯을 확대 할 경우 사용자 정의 레이블 세트를 다시 설정해야합니다. 음, 라벨에 날짜를 사용하기 때문에 솔루션이 훨씬 쉽습니다. – Stern87

관련 문제