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

标签 ios core-plot linegraph

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

最佳答案

如果要在core plot中做线性图,有几点需要注意。首先确保你的 View Controller 能够绘制图形。您需要将其设为绘图委托(delegate)、绘图数据源和绘图空间委托(delegate)。

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

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

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

- (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 是一种很好的图形化方式,他们的网站上有很多很棒的信息。希望这会有所帮助。

关于ios - 在 iOS 中使用 x 轴和 y 轴创建 Coreplot 线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15002108/

相关文章:

ios - 我可以控制两个 View Controller 之间的转换(即通过用户滑动)吗?

javascript - 多线序数 d3 图表

ios - JSON Decodable 问题

ios - 当用户在 iOS 订阅中输入计费重试时应采取什么操作?

ios - URLSession 和 UI 相关任务

iphone - 如何在Graph中自定义CorePlot的绘图区

iPhone CorePlot : Chart with user Touch Event

ios - 如何强制核心绘图库不对标签进行本地化?

graphael - Raphael 上的悬停列和工具提示显示有问题吗?