【问题标题】:Creating Coreplot line graph with x and y axes in iOS在 iOS 中使用 x 和 y 轴创建 Coreplot 折线图
【发布时间】:2013-02-21 12:19:08
【问题描述】:

我查看了很多 Core plot 示例教程,但大多数都存在问题。如果任何人都可以提供一个工作教程来创建具有数据 X=(Sep,Oct,Nov,Dec) 和 Y=(20,40,80,30) 的线图,其中 X 和 Y 轴也在 iOS 中使用 Core Plot 框架?任何代码都会对我有很大帮助..

【问题讨论】:

    标签: ios core-plot linegraph


    【解决方案1】:

    如果您想在核心图中制作线性图,请记住一些事项。首先确保你的视图控制器能够绘制图形。您需要将其设为绘图委托、绘图数据源和绘图空间委托。

    @interface ViewController : UIViewController <CPTScatterPlotDelegate, CPTPlotSpaceDelegate, CPTPlotDataSource>
    

    这是在 .h 文件中添加的。 **不要忘记也导入 CorePlot-cocoaTouch.h!

    接下来,在视图中确实出现了您希望将变量放入数组的方法。这是我用来制作快速线性图的示例。

    - (void)viewDidAppear:(BOOL)animated
    {
    float b = 1;
    float c = 5;
    
    Xmax = 10;
    Xmin = -10;
    Ymax = 10;
    Ymin = -10;
    
    float inc = (Xmax - Xmin) / 100.0f;
    float l = Xmin;
    
    NSMutableArray *linearstuff = [NSMutableArray array];
    
    for (int i = 0; i < 100; i ++)
    {
        float y = (b * (l)) + c;
        [linearstuff addObject:[NSValue valueWithCGPoint:CGPointMake(l, y)]];
        NSLog(@"X and Y = %.2f, %.2f", l, y);
        l = l + inc;
    }
    
    self.data = linearstuff;
    [self initPlot];
    }
    

    对 [self initPlot] 的调用调用一个函数来实际制作图形。它与现有的所有示例代码非常相似。

    将数据放入数组后,接下来要做的就是按照您希望的方式制作图表。再次查看 configureHost、configure Graph 和类似内容的所有代码,它就在 Core Plot 网站上。另一个要记住的重要事情是 numberOfRecordsForPlot 方法。这是我的样本。这可以让您知道您有多少数据点。

    - (NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
    {
    return [_data count];
    }
    

    _data 是我用来存储所有内容的数组。接下来,您要绘制数据图表。使用 numberForPlot 方法。这里又是一个示例。

    - (NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
    {
    NSLog(@"numberForPlot");
    if ([plot.identifier isEqual:@"linear"])
    {
        NSValue *value = [self.data 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];
    
        }
        NSLog(@"x is %.2f", point.x);
        NSLog(@"y is %.2f", point.y);
    }
    return [NSNumber numberWithFloat:0];
    }
    

    希望这能让您入门。 Core Plot 是一种很好的图表方式,他们的网站充满了重要的信息。希望这会有所帮助。

    【讨论】:

    • 是的,是的。很有帮助。。谢谢
    猜你喜欢
    • 1970-01-01
    • 2023-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多