2013-02-07 80 views
1

我正在使用自定義標籤,並且當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,但它不適用於自定義標籤。

回答

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