2012-03-18 26 views
4

同じグラフに異なる時間に複数のプロットを描画する必要があります。プロットの数が動的に変化することを除いてcoreplotに複数のプロットを描画する方法

Need graph like this

:下の画像をご覧下さい。時には、ブルーとオレンジのデータセットを4回と3回だけ必要とすることもあります。このようなバープロットを1つ管理することができます。私の場合は

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'を使用してください! –

関連する問題