ios:Coreplot,无法在一个图表上获得多个图

时间:2013-05-17 02:53:43

标签: ios objective-c graph core-plot

我整天都在努力,但仍然没有任何线索。我制作了两个数据集并将它们放入一个字典中,然后我将其用作我的情节的数据源。一切都应该工作,但它仍然只显示一个图表。

有趣的是,如果我更改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 个答案:

答案 0 :(得分:1)

在datasource方法中,您指定了“_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
   }
}

希望它有所帮助...