我需要在不同时间在同一图表中绘制多个图。请看下面的图片:
除了图表的数量会动态变化。有时我只需要蓝色和橙色数据集,有时候全部四次,有时只需要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 :(得分: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;
}
答案 1 :(得分:0)
检查不同的地图标识符,然后返回其他数据。