2012-03-18 102 views
4

我需要在不同時間在同一個圖中繪製多個圖。請看下圖:如何在coreplot中繪製多個圖

Need graph like this

除了地塊數量會動態改變。有時候,我只需要四次,一次只需要藍色和橙色數據集,而有些時候只需要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循環中比較它們([plot.identifier isEqual:@「mainplot」])。在上面的示例中,「mainplot」是硬編碼的。我將如何改變這是動態的。 – Rajashekar 2012-03-18 11:41:12

+0

我相信,如果你仔細想想,你可以自己弄清楚。 – MrMage 2012-03-18 11:49:54

+0

我在給我最好的猜測。 CPTPlot * tempPlot = [self.graph objectatindex:indexpath.row];如果([plot.identifier isEqual:tempPlot.identifier])//我確信self.graph不會採用objectatindex方法。我想知道它可以採取什麼方法? – Rajashekar 2012-03-18 13:00:46

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

如果我不知道我會有多少行?有什麼建議麼? – 2012-12-12 19:26:46

+0

@BorisGafurov爲'PLOT_DATA'使用'NSMutableArray'而不是'NSArray'! – 2012-12-15 07:05:49