2012-05-28 2 views
2

다음과 비슷한 막대 그래프를 만들려고합니다. 즉, x 축에는 2 개의 다른 레이블이 있습니다. 하나는 막대 자체에 레이블을 지정하고 성능을 나타내며, 다른 하나는 축의 맨 아래에 연도를 나타냅니다. 그래프의 상단이 아닌 숫자 만 회전시켜야합니다.coreplot이있는 막대 그래프에서 개별 막대에 맞춤 라벨과 라벨을 동시에 사용하는 방법은 무엇입니까?

bar graph

나는 사용자 정의 레이블을 작성하여 년을 표시하고 있습니다. 또한 -dataLabelForPlot:recordIndex: 메서드를 구현하여 막대의 성능 수치를 표시 할 수 있습니다. 성능 수치는 수평으로 표시되지만 성능 수치를 90도 회전 할 수 있기를 원합니다. labelingPolicyCPTAxisLabelingPolicyNone으로 설정 했으므로 플롯에 labelRotation을 설정하면 도움이되지 않습니다. 맞춤 레이블을 추가 할 수 있습니다. 다음은

내 코드입니다 :

-(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

감사합니다. Eric. 플롯에서이 속성을 설정하면됩니다. – EmmKay

관련 문제