2012-03-18 2 views
4

같은 그래프에서 다른 시간에 여러 개의 그림을 그릴 필요가 있습니다. 아래의 이미지를보고하십시오 :coreplot에서 여러 개의 그림 그리는 방법

Need graph like this

를 플롯의 수를 동적으로 변경할 것을 제외하고. 때로는 파란색과 주황색 데이터 집합을 4 번과 3 번만 필요로하는 경우도 있습니다. 이처럼 하나의 막대 그림을 관리 할 수 ​​있습니다. 내 경우

CPTScatterPlot *plot = [[[CPTScatterPlot alloc] init] autorelease]; 
plot.dataSource = self; 
plot.identifier = @"mainplot"; 
plot.dataLineStyle = lineStyle; 
plot.plotSymbol = plotSymbol; 
[self.graph addPlot:plot]; 

나는 그들 for 루프에 넣고 각 반복에 [self.graph addplot:plot]을 할 수 있습니다. 그러나 데이터 소스를 어떻게 관리해야합니까? 데이터 세트 수가 동적으로 변경되면 아래 코드를 어떻게 관리해야합니까?

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index { 
    if ([plot.identifier isEqual:@"mainplot"]) 
    { 
     NSValue *value = [self.graphData objectAtIndex:index]; 
     CGPoint point = [value CGPointValue]; 

     // FieldEnum determines if we return an X or Y value. 
     if (fieldEnum == CPTScatterPlotFieldX) 
     { 
      return [NSNumber numberWithFloat:point.x]; 
     } 
     else // Y-Axis 
     { 
      return [NSNumber numberWithFloat:point.y]; 
     } 
    } 

    return [NSNumber numberWithFloat:0]; 
} 

답변

0

다른 플롯 식별자를 확인한 다음 다른 데이터를 반환하십시오.

+0

안녕 MrMage, 당신의 quichk 응답을 주셔서 감사합니다, 당신은 내가 동적 식별자를 할당 할 방법을 말해 줄 수 및 더 어려운 질문은 if 루프에서 그들을 비교하는 방법이다. if ([plot.identifier isEqual : @ "mainplot"]). 위의 샘플에서 "mainplot"은 하드 코딩되어 있습니다. 어떻게 내가 이것을 동적으로 바꿀 수 있을까. – Rajashekar

+0

나는 조금 생각하면 스스로 알아낼 수있을 것이라고 확신한다. – MrMage

+0

나는 최고의 추측을하고있다. CPTPlot * tempPlot = [self.graph objectatindex : indexpath.row]; if ([plot.identifier isEqual : tempPlot.identifier]) // self.graph가 objectatindex 메소드를 사용하지 않을 것이라고 확신합니다. 나는 그것이 취할 수있는 방법을 알고 싶다? – Rajashekar

2

나는 그것을 해냈다. 플롯에 NSArray을 사용하고 플롯 데이터를 만들고이를 객체로 추가하면 NSDictionary이됩니다. 이 함수를 작성

NSDictionary *firstLineDic = [NSDictionary dictionaryWithObjectsAndKeys:@"firstLine", PLOT_IDENTIFIER, firstLineData, PLOT_DATA, nil]; 
NSDictionary *secondLineDic = [NSDictionary dictionaryWithObjectsAndKeys:@"secondLine", PLOT_IDENTIFIER, secondLineData, PLOT_DATA, nil]; 
NSArray *arrayData = [NSArray arrayWithObjects:firstLineDic, secondLineDic, nil]; 
scatterPlot = [[ScatterPlot alloc] initWithHostingView:plotView data:arrayData]; 
[scatterPlot initialisePlot]; 

이제 ScatterPlot 클래스 : 자세한 내용에 대해이 샘플을 볼 수 있습니다

-(id)initWithHostingView:(CPTGraphHostingView *)_hostingView data:(NSArray *)_data{ 
    self = [super init]; 
    if (self != nil) { 
     self.hostingView = _hostingView; 
     data = [[NSArray alloc] initWithArray:_data]; 
     self.graph = nil; 
    } 
    return self; 
} 

-(void)initialisePlot 
{ 

... 

    for (NSDictionary *dic in data) { 
     CPTScatterPlot *plot = [[[CPTScatterPlot alloc] init] autorelease]; 
     plot.dataSource = self; 
     plot.identifier = [dic objectForKey:PLOT_IDENTIFIER]; 
     plot.dataLineStyle = [lineStyles objectAtIndex:[dic objectForKey:PLOT_COLOR]]; 
     plot.plotSymbol = plotSymbol; 
     [self.graph addPlot:plot]; 
    } 

... 

} 

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot 
{ 
    for (NSDictionary *dic in data) { 
     NSString *identity = [dic objectForKey:PLOT_IDENTIFIER]; 
     if([plot.identifier isEqual:identity]){ 
      NSArray *arr = [dic objectForKey:PLOT_DATA]; 
      return [arr count]; 
     } 
    } 
    return 0; 
} 
+0

만약 내가 얼마나 많은 선을 가지고 있을지 모른다면? 어떤 제안? –

+0

@BorisGafurov'PLOT_DATA'에 NSArray 대신 NSMutableArray를 사용하십시오! –

관련 문제