2013-05-17 66 views
1

我一直在研究這一整天,但仍然沒有線索。我製作了兩個數據集,並將它們放入字典中,然後用作我的情節的數據源。一切都應該工作,但它仍然只顯示一個圖表。ios:Coreplot,無法在一個圖表上獲得多個圖塊

有趣的是,如果我更改PLOT_IDENTIFIER的順序,則該圖的數據是顯示的數據,而不是其他數據。

這裏就是我使數據和字典:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    //This is just so the graph has data without the buttons before them. 
    data1 = [NSMutableArray array]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(-10.5, 8.5)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(-10, 29.2)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(-9.5, 39.6)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(-9, 48.6)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(-8.5, 47.8)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(-8, 51.5)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(-7.5, 59.5)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(-7, 59.4)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(-6.5, 83.4)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(-6, 78.4)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(-5.5, 120.7)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(-5, 109.1)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(-4.5, 112.4)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(-4, 115.3)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(-3.5, 111.8)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(-3, 108.8)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(-2.5, 131.5)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(-2, 152.2)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(-1.5, 150.2)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(-1, 172)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(-0.5, 186.4)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(0, 175.1)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(0.5, 164.3)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(1, 165)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(1.5, 159.2)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(2, 146.8)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(2.5, 147.4)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(3, 143.2)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(3.5, 153.8)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(4, 156.7)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(4.5, 159.8)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(5, 144.5)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(5.5, 136.1)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(6, 107.3)]]; 
    [data1 addObject:[NSValue valueWithCGPoint:CGPointMake(6.5, 78.7)]]; 

    //same here 
    data2 = [NSMutableArray array]; 
    [data2 addObject:[NSValue valueWithCGPoint:CGPointMake(-6, 1.5)]]; 
    [data2 addObject:[NSValue valueWithCGPoint:CGPointMake(-5.5, 15.6)]]; 
    [data2 addObject:[NSValue valueWithCGPoint:CGPointMake(-5, 19.5)]]; 
    [data2 addObject:[NSValue valueWithCGPoint:CGPointMake(-4.5, 20.2)]]; 
    [data2 addObject:[NSValue valueWithCGPoint:CGPointMake(-4, 16.5)]]; 
    [data2 addObject:[NSValue valueWithCGPoint:CGPointMake(-3.5, 15.1)]]; 
    [data2 addObject:[NSValue valueWithCGPoint:CGPointMake(-3, 16.6)]]; 
    [data2 addObject:[NSValue valueWithCGPoint:CGPointMake(-2.5, 19.9)]]; 
    [data2 addObject:[NSValue valueWithCGPoint:CGPointMake(-2, 19.9)]]; 
    [data2 addObject:[NSValue valueWithCGPoint:CGPointMake(-1.5, 20)]]; 
    [data2 addObject:[NSValue valueWithCGPoint:CGPointMake(-1, 23.6)]]; 
    [data2 addObject:[NSValue valueWithCGPoint:CGPointMake(-0.5, 28.7)]]; 
    [data2 addObject:[NSValue valueWithCGPoint:CGPointMake(0, 30.6)]]; 
    [data2 addObject:[NSValue valueWithCGPoint:CGPointMake(0.5, 28)]]; 
    [data2 addObject:[NSValue valueWithCGPoint:CGPointMake(1, 27.2)]]; 
    [data2 addObject:[NSValue valueWithCGPoint:CGPointMake(1.5, 26.7)]]; 
    [data2 addObject:[NSValue valueWithCGPoint:CGPointMake(2, 24.5)]]; 
    [data2 addObject:[NSValue valueWithCGPoint:CGPointMake(2.5, 23.5)]]; 
    [data2 addObject:[NSValue valueWithCGPoint:CGPointMake(3, 21.7)]]; 
    [data2 addObject:[NSValue valueWithCGPoint:CGPointMake(3.5, 25.5)]]; 
    [data2 addObject:[NSValue valueWithCGPoint:CGPointMake(4, 23.2)]]; 
    [data2 addObject:[NSValue valueWithCGPoint:CGPointMake(4.5, 21.2)]]; 

NSDictionary *firstLineDic = [NSDictionary dictionaryWithObjectsAndKeys:@"firstLine", @"PLOT_IDENTIFIER", data1, @"PLOT_DATA", nil]; 
    NSDictionary *secondLineDic = [NSDictionary dictionaryWithObjectsAndKeys:@"secondLine", @"PLOT_IDENTIFIER", data2, @"PLOT_DATA", nil]; 
    NSMutableArray *dictionarydata = [NSMutableArray arrayWithObjects:firstLineDic, secondLineDic, nil]; 
    self.scatterPlot = [[psuScatterSimpleScatterPlot alloc] initWithHostingView:_graphHostingView andData:dictionarydata]; 
    [self.scatterPlot initialisePlot]; 

此處,我使圖表:

NSArray* data = [[NSArray alloc] initWithArray:_graphData]; 


    for (NSDictionary *dic in data) { 
     CPTScatterPlot *plot = [[[CPTScatterPlot alloc] init] autorelease]; 
     plot.dataSource = self; 
     plot.identifier = [dic objectForKey:@"PLOT_IDENTIFIER"]; 
     plot.plotSymbol = plotSymbol; 
     plot.dataLineStyle = lineStyle; 

     [self.graph addPlot:plot]; 

     } 



} 

// Delegate method that returns the number of points on the plot 




-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot 
{ 
    NSArray* data = [[NSArray alloc] initWithArray:_graphData]; 

    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; 
} 


// Delegate method that returns a single X or Y value for a given plot. 
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index 
{ 
    if ([plot.identifier isEqual:@"firstLine"] || [plot.identifier isEqual:@"secondLine"]) 
    { 
     NSDictionary*jay = [_graphData objectAtIndex:0]; 

     NSArray *jay2 = [jay objectForKey:@"PLOT_DATA"]; 
     NSValue *value = [jay2 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]; 

} 

回答

1

在數據源的方法,你指定「_graphData」陣列的第一對象。這是pblm。變化下面的代碼:

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex: (NSUInteger)index 
{ 

    if ([plot.identifier isEqual:@"firstLine"]) 
    { 
     NSDictionary *firstDataSet = [_graphData objectAtIndex:0]; 
     //Code for processing the points here 

    } 
    else if([plot.identifier isEqual:@"secondLine"]) 
    { 
     NSDictionary *secondDataSet = [_graphData objectAtIndex:1]; 
     //Code for processing the points here 
    } 
} 

希望它可以幫助...

+0

LIFE.SAVER。謝謝!! – pj409

相關問題