在我的iPad应用程序中实现简单的折线图的最佳方式是什么?我只希望绘制4-8点和图表确实需要花哨的外观。HTML5将足够好地使用我需要的东西。我该如何实现HTML代码来绘制折线图?我一直在考虑使用Google图表工具,但有什么其他建议可以使用吗?
发布于 2012-07-17 13:59:04
这是一个非常基本的类,它将提供基本的代码行。Y轴的NSArray为NSNumbers。它应该为您提供了一个很好的起点,因为它非常简单。您可以考虑将标签添加到点和轴标记,以提供缩放感。
标题:
#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
https://stackoverflow.com/questions/11516560
复制相似问题