2011-09-07 6 views
1

저는 CorePlot을 처음 사용했으며 이제 막대 그래프를 사용자 지정하려고합니다. 당신은이 그림에서 그것을 봅니다.막대 그래프 (CorePlot Framework)에 대한 사용자 지정 레이블

enter image description here

는 지금 바

의 상단에있는 모든 줄을 통해 번호를 추가 할 희망 당신은 내가 무슨 뜻인지 알아.

어떻게 알 수 있습니까? 당신은 단순한 숫자 레이블, 플롯에 labelTextStylelabelFormatter 속성을 설정하려면

이 내 코드

-(void) generateDataSamples 
{ 
    int rawSamples [] = {1,3,6,2,1,1,3,15,2,1}; 
    int numSamples = sizeof(rawSamples)/sizeof(int); 

    samples = [[NSMutableArray alloc] initWithCapacity:numSamples]; 

    for (int i = 0; i < numSamples; i++){ 
     double x = i; 
     double y = rawSamples[i]; 
     NSDictionary *sample = [NSDictionary dictionaryWithObjectsAndKeys: 
           [NSNumber numberWithDouble:x],X_VAL, 
           [NSNumber numberWithDouble:y],Y_VAL, 
           nil]; 
     [samples addObject:sample]; 
    } 
} 

- (void)viewDidLoad { 

    [super viewDidLoad]; 

    [self generateDataSamples]; 

    double xAxisStart = 0; 
    double xAxisLength = [samples count]; 

    double yAxisStart = 0; 
    double yAxisLength = [[samples valueForKeyPath:@"@max.Y_VAL"] doubleValue]; 


    CPGraphHostingView *hostingView = [[CPGraphHostingView alloc] initWithFrame:self.view.bounds]; 
    [self.view addSubview:hostingView]; 

    CPXYGraph *graph = [[CPXYGraph alloc] initWithFrame:self.view.bounds]; 
    hostingView.hostedGraph = graph; 



    CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace; 
    plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromDouble(0) 
                length:CPDecimalFromDouble(xAxisLength +1)]; 

    plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromDouble(0) 
                length:CPDecimalFromDouble(yAxisLength + 0.5)]; 

    CPBarPlot *plot = [[CPBarPlot alloc] initWithFrame:CGRectZero]; 
    plot.plotRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromDouble(0.0) 
               length:CPDecimalFromDouble(xAxisLength)]; 

    CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet; 
    CPLineStyle *lineStyle = [CPLineStyle lineStyle]; 
    lineStyle.lineColor = [CPColor whiteColor]; 
    lineStyle.lineWidth = 1.0f; 
    CPTextStyle *cyanStyle = [CPTextStyle textStyle]; 
    cyanStyle.color = [CPColor cyanColor]; 
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; 
    [formatter setMaximumFractionDigits:0]; 

    plot.barOffset = .5; 
    plot.dataSource = self; 


    [graph addPlot:plot]; 

    [plot release]; 
    [graph release]; 
    [hostingView release]; 
} 

답변

1

입니다. 좀 더 복잡한 레이블이 필요한 경우 -dataLabelForPlot:recordIndex: 메서드를 데이터 소스에 추가하고 사용자 정의 레이블을 만들 수 있습니다.

샘플 코드는 Core Plot에 포함 된 예제 응용 프로그램을 참조하십시오. 라벨링 메커니즘은 모든 플롯 유형에서 동일합니다. 플롯 갤러리 앱의 '수직 막대 차트'는 첫 번째 기술을 보여줍니다. 플롯 갤러리의 다른 플롯 중 일부는 데이터 소스 기술을 사용합니다.

+0

@TeamNoX가 질문을 해결하는 데 도움이되는 훌륭한 지식과 훌륭한 답변을 제공합니다. 저에게 도움이되는 부분은'dataLabelForPlot'입니다. 고마워 +1. – Jacksonkr