2013-05-08 60 views
1

我在核心地块使用自定义标签时遇到一个问题:如何动态地改变核心地块的标签

  1. 我的核心情节支持变焦。

  2. x轴标记是定制x.axisLabels

在第一负载中,接口是正常的,但是当我缩小核心剧情,轴线标签重叠在一起。

如何设计标签上的水平?例如,当最低级别仅显示一年时,在级别显示日期中,最高级别显示日期。

- (void)initPlotGraph 
{ 
    graph = [[CPTXYGraph alloc] initWithFrame:CGRectZero]; 
    self.hostedGraph = graph; 
    locationLabels = [[NSMutableArray alloc]init]; 

    graph.plotAreaFrame.paddingLeft += 5.0; 
    graph.plotAreaFrame.paddingTop += 5.0; 
    graph.plotAreaFrame.paddingRight += 5.0; 
    graph.plotAreaFrame.paddingBottom += 17.5; 

    //[self setAllowPinchScaling:NO]; 

    // Setup scatter plot space 
    plotSpace      = (CPTXYPlotSpace *)graph.defaultPlotSpace; 
    plotSpace.allowsUserInteraction = YES; 
    plotSpace.delegate    = self; 

    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0)length:CPTDecimalFromFloat(1+0.3)]; 
    plotSpace.globalYRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0)length:CPTDecimalFromFloat(1+0.3)]; 

    [self initPlotAxis]; 
} 

- (void)initPlotAxis 
{ 
    CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle]; 
    axisLineStyle.lineWidth   = 2; 
    axisLineStyle.lineColor   = [CPTColor colorWithComponentRed:CPTFloat(0.11765) green:CPTFloat(0.49804) blue:CPTFloat(0.87451) alpha:CPTFloat(1)]; 

    CPTMutableLineStyle *axisTickLineStyle = [CPTMutableLineStyle lineStyle]; 
    axisTickLineStyle.lineWidth   = 0; 

    CPTLineCap *lineCap = [CPTLineCap sweptArrowPlotLineCap]; 
    lineCap.size  = CGSizeMake(10, 10); 
    lineCap.lineCapType = CPTLineCapTypeNone; 

    // Axes 
    // Label x axis with a fixed interval policy 
    lineCap.lineStyle  = axisLineStyle; 
    lineCap.fill   = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:CPTFloat(0.29019) green:CPTFloat(0.54118) blue:CPTFloat(0.76471) alpha:CPTFloat(1)]]; 
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet; 
    CPTXYAxis *x   = axisSet.xAxis; 
    x.majorIntervalLength = CPTDecimalFromInt(1); 
    x.axisLineStyle  = axisLineStyle; 
    x.axisConstraints  = [CPTConstraints constraintWithRelativeOffset:0]; 
    x.axisLineCapMax  = lineCap; 
    x.axisLabels   = [self buildLabelTitle]; 
    x.labelingPolicy  = CPTAxisLabelingPolicyNone; 
    x.minorTickLocations = [NSSet setWithArray:locationLabels]; 

    // Label y with an automatic label policy. 
    lineCap.lineStyle  = axisLineStyle; 
    lineCap.fill   = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:CPTFloat(0.29019) green:CPTFloat(0.54118) blue:CPTFloat(0.76471) alpha:CPTFloat(1)]]; 
    CPTXYAxis *y = axisSet.yAxis; 
    // y.majorIntervalLength = CPTDecimalFromDouble(5); 
    y.majorTickLineStyle = axisTickLineStyle; 
    y.minorTickLineStyle = axisTickLineStyle; 
    y.axisLineStyle   = axisLineStyle; 
    y.axisLineCapMax  = lineCap; 
    y.axisConstraints  = [CPTConstraints constraintWithRelativeOffset:0]; 

    // Set axes 
    graph.axisSet.axes = [NSArray arrayWithObjects:x, y, nil]; 
} 

- (NSMutableSet*)buildLabelTitle 
{ 
    NSMutableSet *newAxisLabels = [NSMutableSet set]; 

    CPTMutableTextStyle *textStyleB = [CPTMutableTextStyle textStyle]; 
    textStyleB.color = [CPTColor colorWithComponentRed:CPTFloat((float)0x09/0xFF) green:CPTFloat((float)0x31/0xFF) blue:CPTFloat((float)0x4A/0xFF) alpha:CPTFloat(1)]; 

    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(corePlotNums - 9)length:CPTDecimalFromInt(10)]; 
    plotSpace.globalXRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInt(corePlotNums + 1)]; 

    for (NSUInteger i = 1; i < corePlotNums + 1; i++) 
    { 
     CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:[[m_labelStr objectAtIndex:i-1] substringWithRange:NSMakeRange(5, 11)] textStyle:textStyleB]; 
     newLabel.tickLocation = CPTDecimalFromUnsignedInteger(i); 
     newLabel.offset = 5; 
     [newAxisLabels addObject:newLabel]; 
     [newLabel release]; 
    } 

    return newAxisLabels; 
} 

enter image description here

enter image description here

回答

0

如果用户被允许改变范围情节(即,由夹送变焦)时,使用的曲线图代表空间以监视变化的情节的空间。使用新的绘图区域和绘图区域的大小来确定要使用的时间刻度和要显示的标签数量。根据需要调整轴标签属性。