2012-05-28 73 views
2

我正在尝试创建一个类似于下图的条形图 - x轴有两个不同的标签 - 一个是条形自身的标签,表示性能,另一个位于轴的底部,表示年份。只有数字需要显示在图上而不是顶部。如何在带有coreplot的条形图上同时使用自定义标签和单独条形标签?

bar graph

我通过创建自定义的标签显示了多年。通过实施-dataLabelForPlot:recordIndex:方法,我也能够显示酒吧的表现数据。性能数据显示但水平,但我想能够旋转90度的性能数字。 在图上设置labelRotation并不会帮助,因为我已将labelingPolicy设置为CPTAxisLabelingPolicyNone,因此我可以添加自定义标签。

下面是我的代码:

-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index 
{ 
    CPTMutableTextStyle *axisTitleTextStyle = [CPTMutableTextStyle textStyle]; 
    axisTitleTextStyle.fontSize = 10.0; 
    axisTitleTextStyle.color = [CPTColor blackColor]; 

    CPTTextLayer *label = [[CPTTextLayer alloc] init]; 
    label.textStyle = axisTitleTextStyle; 
    if ([plot isKindOfClass:[CPTBarPlot class]]) { 
     if ([plot.identifier isEqual:@"Fund"]) { 
      label.text = [[[self dataGroup1] objectAtIndex:index] stringValue]; 
     } 
     else if ([plot.identifier isEqual:@"Benchmark"]) { 
      label.text = [[[self dataGroup2] objectAtIndex:index] stringValue]; 
     } 

    } 
    return [label autorelease]; 
} 


- (void) drawGraph 
{ 
    graphHostingView_ = [[CPTGraphHostingView alloc] initWithFrame:self.bounds]; 

    calPerfGraph_ = [[CPTXYGraph alloc] initWithFrame:self.bounds]; 
    graphHostingView_.hostedGraph = calPerfGraph_; 

    [self addSubview:graphHostingView_]; 

    calPerfGraph_.plotAreaFrame.masksToBorder = NO; 

    calPerfGraph_.paddingLeft = 5.0; 
    calPerfGraph_.paddingTop = 20.0; 
    calPerfGraph_.paddingRight = 10.0; 
    calPerfGraph_.paddingBottom =20.0; 

    calPerfGraph_.plotAreaFrame.paddingTop = 10.0; 
    calPerfGraph_.plotAreaFrame.paddingBottom = 50.0; 
    calPerfGraph_.plotAreaFrame.paddingLeft = 10.0; 
    calPerfGraph_.plotAreaFrame.paddingRight = 35.0; 

    // Y axis scale - round to nearest ten 
    int maxYScale = (([[self getPositiveMax] intValue]/10) + 1) * 10; 
    int minYScale = (([[self getNegativeMax] intValue]/10) + 1) * 10; 

    // Styles 
    CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle]; 
    lineStyle.lineColor = [CPTColor grayColor]; 
    lineStyle.lineWidth = 2.0f; 

    CPTMutableLineStyle *gridLineStyle = [CPTMutableLineStyle lineStyle]; 
    gridLineStyle.lineColor = [CPTColor grayColor]; 
    gridLineStyle.lineWidth = 1.0f; 

    // X axis 
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)calPerfGraph_.axisSet; 
    axisSet.xAxis.minorTicksPerInterval = 0; 
    axisSet.xAxis.majorTickLineStyle = lineStyle; 
    axisSet.xAxis.axisLineStyle = lineStyle; 
    axisSet.xAxis.majorTickLength = 0; 
    axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyNone; 
    axisSet.xAxis.axisConstraints = [CPTConstraints constraintWithLowerOffset:0]; // Bring labels to the bottom on the plot 
    axisSet.xAxis.labelRotation = M_PI/2; 

    int currentYear = [self getCurrentYear]; 
    NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:6]; 
    for (int i=currentYear-5,count=1; currentYear>=i; i++,count++) { 
     CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:@"%d",i] textStyle:axisSet.xAxis.labelTextStyle]; 
     newLabel.tickLocation = CPTDecimalFromInt(count); 
     newLabel.offset = axisSet.xAxis.labelOffset + axisSet.xAxis.majorTickLength; 
     [customLabels addObject:newLabel]; 
     [newLabel release]; 
    } 
    axisSet.xAxis.axisLabels = [NSSet setWithArray:customLabels]; 

    // Y axis 
    if (maxYScale >= 40 || minYScale >= 40) { 
     axisSet.yAxis.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:@"20"] decimalValue]; 
    } 
    else { 
     axisSet.yAxis.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:@"10"] decimalValue]; 
    } 

    axisSet.yAxis.minorTicksPerInterval = 0; 
    axisSet.yAxis.majorTickLength = 0; 
    axisSet.yAxis.majorGridLineStyle = gridLineStyle; 
    axisSet.yAxis.axisLineStyle = nil; 
    axisSet.yAxis.axisConstraints = [CPTConstraints constraintWithUpperOffset:0]; // Bring labels to the right end of the plot 
    axisSet.yAxis.tickDirection = CPTSignPositive; 

    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)calPerfGraph_.defaultPlotSpace; 
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0) length:CPTDecimalFromDouble([[self calPerfFundData] count]+1)]; 
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(-minYScale) length:CPTDecimalFromDouble(minYScale + maxYScale)]; 

    CPTBarPlot *barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor greenColor] horizontalBars:NO];  
    barPlot.baseValue = CPTDecimalFromString(@"0"); 
    barPlot.dataSource = self; 
    barPlot.barOffset = CPTDecimalFromFloat(.7); 
    barPlot.identifier = @"Fund"; 
    barPlot.barWidth = CPTDecimalFromFloat(.25); 
    [barPlot addAnimation:[self animateVerticalBars] forKey:@"FundAnimation"]; 
    [calPerfGraph_ addPlot:barPlot toPlotSpace:plotSpace]; 

    CPTBarPlot *bbarPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor colorWithComponentRed:0.9 green:0.9 blue:0.9 alpha:0.8] horizontalBars:NO];  
    bbarPlot.baseValue = CPTDecimalFromString(@"0"); 
    bbarPlot.dataSource = self; 
    bbarPlot.barOffset = CPTDecimalFromFloat(1); 
    bbarPlot.barWidth = CPTDecimalFromFloat(.25); 
    bbarPlot.identifier = @"Benchmark"; 
    [bbarPlot addAnimation:[self animateVerticalBars] forKey:@"BenchmarkAnimation"]; 
    [calPerfGraph_ addPlot:bbarPlot toPlotSpace:plotSpace]; 
} 

如何旋转上杠表现人物?任何帮助表示赞赏。 谢谢。

回答

3

这两个图和轴都有自己的独立labelRotation属性。您将负责设置自定义轴标签的旋转,但条形图的labelRotation属性应该为条上方的数据标签设置旋转角度。

+0

谢谢埃里克。在剧情上设置此属性的作品。 – EmmKay