我整天都在研究这个,但仍然没有任何线索。我制作了两个数据集并将它们放入字典中,然后用作绘图的数据源。一切都应该工作,但它仍然只显示一个图表。
有趣的是,如果我更改 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];
}