我有应用程序来创建图表我已经创建了一个基于视图的应用程序,然后在其中添加了用于创建图表的代码,但它没有显示图表。如果使用相同的代码创建一个单独的 UIView 那么它的工作原理不是
#import <UIKit/UIKit.h>
#import "ECGraph.h"
#import "ECGraphItem.h"
@class GraphsViewController;
@interface Display : UIView {
NSArray *percentages;
int myY;
ECGraph *graph;
ECGraphItem *item1;
ECGraphItem *item2;
}
@property(nonatomic,retain)NSArray*percentages;
-(void) setPercentageArray:(NSArray*) array;
@end
#import "Display.h"
#import "ECGraph.h"
@implementation Display
@synthesize percentages;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code.
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextRef _context = UIGraphicsGetCurrentContext();
graph = [[ECGraph alloc] initWithFrame:CGRectMake(500,-320,320, 200) withContext:_context isPortrait:NO];
item1 = [[ECGraphItem alloc] init];
item2 = [[ECGraphItem alloc] init];
/*
ECGraphItem *item1 = [[ECGraphItem alloc] init];
ECGraphItem *item2 = [[ECGraphItem alloc] init];*/
item1.isPercentage = YES;
item1.yValue=myY;
item1.width = 35;
item1.name = @"item1";
item2.isPercentage = YES;
item2.yValue =17;
item2.width = 35;
item2.name = @"item2";
[graph setXaxisTitle:@"name"];
[graph setYaxisTitle:@"Percentage"];
[graph setGraphicTitle:@"Histogram"];
[graph setDelegate:self];
[graph setBackgroundColor:[UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1]];
NSArray *items = [[NSArray alloc] initWithObjects:item1,item2,nil];
[graph drawHistogramWithItems:items lineWidth:2 color:[UIColor blackColor]];
}
我在 GraphsViewController 中添加了这个视图,但它没有显示任何内容
// 实现 viewDidLoad 以在加载视图后进行额外的设置,通常来自 nib。
- (void)viewDidLoad {
[super viewDidLoad];
[self createGraph];
percentages = [NSArray arrayWithObjects:@"80",@"17", nil];
display = [[Display alloc] init];
[self.view addSubview:display];
[display setPercentageArray:percentages];
}