首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在iOS中实现简单折线图

在iOS中实现简单折线图
EN

Stack Overflow用户
提问于 2012-07-17 13:35:27
回答 1查看 1.4K关注 0票数 0

在我的iPad应用程序中实现简单的折线图的最佳方式是什么?我只希望绘制4-8点和图表确实需要花哨的外观。HTML5将足够好地使用我需要的东西。我该如何实现HTML代码来绘制折线图?我一直在考虑使用Google图表工具,但有什么其他建议可以使用吗?

EN

回答 1

Stack Overflow用户

发布于 2012-07-17 13:59:04

这是一个非常基本的类,它将提供基本的代码行。Y轴的NSArray为NSNumbers。它应该为您提供了一个很好的起点,因为它非常简单。您可以考虑将标签添加到点和轴标记,以提供缩放感。

标题:

代码语言:javascript
运行
复制
#import <UIKit/UIKit.h>

@interface SOGenericGraphView : UIView {
    NSArray *yValues;
}

@property (strong, atomic) NSArray *yValues;

@end

实施:

代码语言:javascript
运行
复制
#import "SOGenericGraphView.h"

@implementation SOGenericGraphView

@synthesize yValues;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    [super drawRect:rect];
    CGRect insetRect = CGRectInset(self.frame, 10, 15);
    double maxSpeed = [[yValues valueForKeyPath:@"@max.doubleValue"] doubleValue];
    CGFloat yRatio = insetRect.size.height/maxSpeed;
    CGFloat xRatio = insetRect.size.width/(yValues.count-1);
    UIBezierPath *sparkline = [UIBezierPath bezierPath];
    for (int x = 0; x< yValues.count; x++) {
        CGPoint newPoint = CGPointMake(x*xRatio + insetRect.origin.x, insetRect.size.height - (yRatio*[[yValues objectAtIndex:x] doubleValue] - insetRect.origin.y));
        if (x == 0) {
            [sparkline moveToPoint:newPoint];
        }
        else {
            [sparkline addLineToPoint:newPoint];
        }
    }
    [[UIColor redColor] set];
    [sparkline stroke];
}

@end
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11516560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档