繁体   English   中英

在iOS中实现简单的线图

[英]Implementing a Simple Line Graph in iOS

什么是在我的iPad应用程序中植入简单折线图的最佳方式。 我只想要绘制4-8点,图表确实需要花哨看。 HTML5足以满足我的需求。 我如何实现HTML代码来绘制折线图? 我一直在考虑使用谷歌图表工具,但我可以使用任何其他建议吗?

这是一个非常基础的课程,它将为您提供基本的课程。 对于Y轴,它需要NSArray的NSNumber。 它应该为您提供一个良好的起点,因为它很简单。 您可以考虑为点和轴标记添加标签以给出比例感。

标题:

#import <UIKit/UIKit.h>

@interface SOGenericGraphView : UIView {
    NSArray *yValues;
}

@property (strong, atomic) NSArray *yValues;

@end

执行:

#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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM