我正在使用带有 iOS 4 SDK 的 Xcode 3.2.3。
我正在准备一个用于在图表上绘制值的小应用程序。为此,我使用了 ECGraph 库。我下载了示例应用程序并对其进行了一些调整以适合我的目的。我希望图表在 X 轴上以小时为单位绘制时间,在 Y 轴上绘制一个简单的 int 值。
我的问题:当我选择在 ECGraph 中创建一个点作为(日期,值)的选项时,如果 X 轴上的比例即时间单位是日期,我可以看到该图表。如果我将时间单位更改为 HOUR,则图形是 Y 轴上的一条线,其中 Y 轴图是正确的,但 X 轴点始终是原点。
我在这里添加了我的 UIView 文件中的代码。它只是从演示应用程序中获取的一个,附带 ECGraph 库下载,然后针对我的东西进行了调整。
显示视图.m:
//
// DisplayView.m
// ECGraphic
//
// Created by ris on 10-4-17.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "DisplayView.h"
#import "ECCommon.h"
#import "ECGraphPoint.h"
#import "ECGraphLine.h"
#import "ECGraphItem.h"
@implementation DisplayView
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialization code
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
CGContextRef _context = UIGraphicsGetCurrentContext();
ECGraph *graph = [[ECGraph alloc] initWithFrame:CGRectMake(10, 50, 300, 350) withContext:_context isPortrait:YES];
ECGraphPoint *point7 = [[ECGraphPoint alloc] init];
point7.yValue = 7;
point7.xDateValue = [ECCommon dOfS:@"08" withFormat:kDEFAULT_HOUR_FORMAT];
ECGraphPoint *point8 = [[ECGraphPoint alloc] init];
point8.yValue = 13;
point8.xDateValue = [ECCommon dOfS:@"09" withFormat:kDEFAULT_HOUR_FORMAT];
ECGraphPoint *point9 = [[ECGraphPoint alloc] init];
point9.yValue = 1;
point9.xDateValue = [ECCommon dOfS:@"10" withFormat:kDEFAULT_HOUR_FORMAT];
ECGraphPoint *point10 = [[ECGraphPoint alloc] init];
point10.yValue = 3;
point10.xDateValue = [ECCommon dOfS:@"11" withFormat:kDEFAULT_HOUR_FORMAT];
//NSArray *pts3 = [[NSArray alloc] initWithObjects:point7,point8,point9, point10, nil];
NSArray *pts3 = [[NSArray alloc] initWithObjects:point7,point8, nil];
ECGraphLine *line3 = [[ECGraphLine alloc] init];
line3.isXDate = YES;
line3.points = pts3;
NSArray *lines = [[NSArray alloc] initWithObjects:line3,nil];
[graph setXaxisTitle:@"Hours"];
[graph setYaxisTitle:@"Energy consumed in Watts"];
[graph setGraphicTitle:@"Weekly consumption of your device"];
[graph setXaxisDateFormat:kDEFAULT_HOUR_FORMAT];
[graph setDelegate:self];
[graph setBackgroundColor:[UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1]];
[graph setPointType:ECGraphPointTypeCircle];
[graph drawCurveWithLines:lines lineWidth:2 color:[UIColor blackColor]];
}
- (void)dealloc {
[super dealloc];
}
@end