core-plot - 如何在coreplot中绘制多个图

标签 core-plot

我需要在不同时间在同一个图中绘制多个图。请看下图:

Need graph like this

除了地块的数量会动态变化。有时我只需要蓝色和橙色数据集,有时需要全部 4 个,有时只需要 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];
}  

最佳答案

我以前做过,而且有效!您可以使用 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;
}

关于core-plot - 如何在coreplot中绘制多个图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9757722/

相关文章:

ios - 显示 Coreplot 条形图值而不进行选择

Swift 4,CorePlot,通过邮件应用程序发送图表(使用 MessageUI)

ios - 在 iOS 中使用 x 轴和 y 轴创建 Coreplot 线图

ios - 我可以为 Core Plot 散点图的范围设置动画吗?

iphone - 使用 CorePlot 绘制饼图

iphone - 如何缩放图形以适合可见空间

iOS Core Plot CPTAxisLabel 与 CPTPlot 对齐

xcode - 缺少用于将 core-plot 文档安装到 xCode 中的 com.CorePlot.Framework.docset 包

ios - 从JSON数据创建NSArray

ios - 核心剧情: Make x-axis ticks evenly spaced