2012-06-26 2 views
1

코어 플롯을 사용하여 ios에 도넛 형 파이 차트를 그려 넣으려고합니다. 각 슬라이스는 동일한 너비를 갖지만 슬라이스는 색상으로 코드화됩니다. 각 슬라이스의 중앙에 컬러 점을 배치하려고하지만 점의 위치를 ​​올바르게 얻는 방법을 알 수 없습니다.도넛 형 CPTPiePlot의 조각에 라벨을 붙이면 오른쪽으로 정렬되지 않습니다.

파이 차트의 labelOffset 속성을 사용하여 대략 중앙에 배치 할 수있었습니다 (현재 시도 및 오류로 결정된 -35의 라벨 오프셋 사용). 그러나 나는 그 (것)들을 정확하게 중심에두기 위하여 빚질 수없고, 가장 안쪽에있는 그래프에서 그들은 틀린 조각 안에 멀리 떨어져 끝날 수있다. 텍스트 레이어의 rectAnchor 속성과 패딩 설정을 시도했지만 어느 쪽도 효과가없는 것 같습니다. 플롯을 만들기위한

코드 :

CGFloat startAngle = [self degreesToRadians:90 + ((360/[self.dangerRoseData numberOfSectors]) /2.0)]; 

    NSInteger levels = [self.dangerRoseData numberOfLevels]; 
    for(int i = 0; i < levels; i++){ 
     CGFloat radiusD = (i + 1.0)/ levels; 
     CGFloat radius = MIN(radiusD * (graphHostingView.frame.size.height - 2 * graph.paddingLeft)/2.8, 
           radiusD * (graphHostingView.frame.size.width - 2 * graph.paddingTop)/2.8); 

     CPTPieChart *lastPie = [plots lastObject]; 
     CGFloat innerRadius; 
     if(lastPie != nil) 
      innerRadius = lastPie.pieRadius; 
     else 
      innerRadius = 0; 

     CPTPieChart *piePlot = [[CPTPieChart alloc] init]; 
     piePlot.backgroundColor = [UIColor clearColor].CGColor; 
     piePlot.dataSource = self; 
     piePlot.delegate = self; 
     piePlot.pieRadius = radius; 
     piePlot.pieInnerRadius = innerRadius; 
     piePlot.identifier  = [self.dangerRoseData nameForLevel:i]; 
     piePlot.borderLineStyle = lineStyle; 
     piePlot.startAngle  = startAngle; 
     piePlot.sliceDirection = CPTPieDirectionClockwise; 
     piePlot.labelOffset = -35;//brute force trial and error 
     piePlot.labelRotationRelativeToRadius = NO;//new property after 1.0 

     [graph addPlot:piePlot]; 
     [plots addObject:piePlot]; 
    } 

과 라벨 :

-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index{ 
.... 

CPTMutableTextStyle *textStyle = [[CPTMutableTextStyle alloc] init]; 
textStyle.color = color; 
textStyle.fontSize = 36; 
CPTTextLayer *label = [[CPTTextLayer alloc] initWithText:@"•" style:textStyle]; 
return label; 
} 

나는 CPTPiePlot labelRotationRelativeToRadius에 새로운 속성을 추가 코어 음모의 최신 버전으로 업데이트를 시도하는 이 문제에 대한 해답이되어야하는 것처럼 보입니다. 그러나 그렇지 않습니다. 새로운 버전으로는 올바른 점에 가까운 곳에 도트를 배치 할 수 없습니다.

레이블을 배치 할 때 뭔가가 누락 되었습니까?

나는 완전히 잘못된 접근법을 사용하여 조각에 점들을 넣을 수 있습니까?

답변

0

글쎄, 텍스트 레이블 대신 이미지를 사용해 보도록 결정했는데 위치가 올바르게 작동하기 때문에 텍스트 레이블처럼 불규칙하지는 않습니다. 내부 원형에 도트를 맞출 수 있으려면 크기를 변경해야했기 때문에 전체 반경의 동일한 양을 차지하는 대신 내부 파이는 나머지 두 배의 공간을 차지합니다. 플롯을 생성 코드 :

NSInteger levels = [self.dangerRoseData numberOfLevels]; 
    for(int i = 0; i < levels; i++){ 
     CPTPieChart *lastPie = [plots lastObject]; 
     CGFloat innerRadius; 
     if(lastPie != nil) 
      innerRadius = lastPie.pieRadius; 
     else 
      innerRadius = 0; 

     CGFloat ringThickness; 
     ringThickness = ((graphHostingView.frame.size.width - 2 * graph.paddingTop)/((levels + 1) * 2)) * .85; 
     if(i == 0)//make the inner circle twice the thickness of the rings 
      ringThickness += ringThickness; 

     CGFloat radius = innerRadius + ringThickness; 

     int labelOffset = -14; 
     if(i == 0) 
      labelOffset = -15; 

     CPTPieChart *piePlot = [[CPTPieChart alloc] init]; 
     piePlot.backgroundColor = [UIColor clearColor].CGColor; 
     piePlot.dataSource = self; 
     piePlot.delegate = self; 
     piePlot.pieRadius = radius; 
     piePlot.pieInnerRadius = innerRadius; 
     piePlot.identifier  = [self.dangerRoseData nameForLevel:i]; 
     piePlot.borderLineStyle = lineStyle; 
     piePlot.startAngle  = startAngle; 
     piePlot.sliceDirection = CPTPieDirectionClockwise; 
     piePlot.labelOffset = labelOffset; 

     [graph addPlot:piePlot]; 
     [plots addObject:piePlot]; 
    } 

와 라벨의 경우 : CPTImageLayer 이미지

를 사용의 복잡성을 줄일 수있는 표준 핵심 플롯에 추가이라고

-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index 
{ 
    ... 

    CPTImageLayer *layer = [[CPTImageLayer alloc] initWithImage:[UIImage imageNamed:filename]]; 
    layer.anchorPoint = CGPointMake(0.5, 0.5); 
    return layer; 
} 

관련 문제