iphone - 制作一个没有核心图的简单线图

标签 iphone ios objective-c graph

我需要在我的应用程序中制作一个折线图来跟踪数据。我看了核心情节,似乎很复杂。有没有一种更简单的方法来制作可以水平移动的折线图,并且需要能够添加新的线段。并且不会导致巨大的内存过载,因为会频繁添加很多内容,但我可以做到这一点,以便它们在飞蛾或其他原因后删除。所以我的问题基本上是:是否有比核心图更简单的方法,以及是否有人可以引导我朝着能够使用自定义数据添加更多段的方向发展。提前致谢。


编辑

我试过了

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    [super drawRect:rect];
    // find min and max values of data
    float max = -HUGE_VALF, min = HUGE_VALF;
    for (int i = 0; i < 0; i ++)
    {
        min = MIN(min, 0);
        max = MAX(max, 10);
    }
    
    // build path
    for (int i = 0; i < 0; i ++)
    {
        // line spacing is the distance you want between line vertices
       float x = i * 1;
        // scale y to view height
       float y = ((1 - min) / (max - min)) * self.bounds.size.height;

        if (i == 0)
        {
            CGContextMoveToPoint(ctx, x, y);
        }
        else
        {
            CGContextAddLineToPoint(ctx, x, y);
        }
    }
    // stroke path (configure color, width, etc before this)
    CGContextStrokePath(nil);
}

@end

最佳答案

您可以使用 CoreGraphics 自己绘制它。

在 UIView(或自定义图像上下文)的 drawRect 中。

{ 
   // find min and max values of data 
   float max = -HUGE_VALF, min = HUGE_VALF;
   for (int i = 0; i < dataCount; i ++)
   {
       min = MIN(min, data[i]); 
       max = MAX(max, data[i]);
   }

   // build path
   for (int i = 0; i < dataCount; i ++)
   {
       // line spacing is the distance you want between line vertices
       float x = i * lineSpacing;
       // scale y to view height 
       float y = ((data[i] - min) / (max - min)) * self.bounds.size.height;

       if (i == 0)
       {
           CGContextMoveToPoint(ctx, x, y);
       }
       else
       {
           CGContextAddLineToPoint(ctx, x, y);
       }
   }
   // stroke path (configure color, width, etc before this)
   CGContextStrokePath(ctx);
}

这非常简单,但希望它能让您朝着正确的方向前进。

关于iphone - 制作一个没有核心图的简单线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18475290/

相关文章:

ios - 两种布局中的 Uibutton 约束

ios - 我们可以在 iOS 应用程序中按需添加 Twitter 权限吗?

ios - 使用自动布局在自定义 UITableViewCell 上分页 UIScrollView

ios - 核心数据堆栈的多个实例

ios - 如何在 SplitViewController 上显示 LoginViewController?

使用 as3 的 iOS 应用不适合屏幕尺寸

iphone - AVAudioSession endInterruption() 返回 NSOSStatusErrorDomain

iphone - 在iPhone上测试应用程序时出错,但在模拟器中没有错误

iphone - 添加到 UITableViewCell 的内容 View 中的 UIButton 仅在选择单元格时出现

objective-c - 由于 iOS9 升级 SpriteKit 图片不再正确显示