-1

我正在使用 EASYGRAPH 在 iphone 应用程序中创建图形问题是它获取静态值并显示图形我希望值应该是来自该图形的视图控制器的动态值。

图形的 UIView

 #import <UIKit/UIKit.h>
 #import "ECGraph.h"
 #import "ECGraphItem.h"
@class GraphsViewController;
@interface Display : UIView {

NSArray *percentages;



}

 @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();
ECGraph *graph = [[ECGraph alloc] initWithFrame:CGRectMake(300,500,320,200) withContext:_context isPortrait:NO];



//300,500,320, 200

ECGraphItem *item1 = [[ECGraphItem alloc] init];

ECGraphItem *item2 = [[ECGraphItem alloc] init];    
item1.isPercentage = YES;
item1.yValue=80;
item1.width = 35;
item1.name = @"item1";
item2.isPercentage = YES;
item2.yValue =17;
item2.width = 35; 
item2.name = @"item2";




NSArray *items = [[NSArray alloc] initWithObjects:item1,item2,nil];

[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]];



[graph drawHistogramWithItems:items lineWidth:2 color:[UIColor blackColor]];




 }
4

1 回答 1

0

首先创建一个包含宽度和高度两个属性的类。然后使用 addObject 将其添加到数组中。现在每个对象都有单独的高度和宽度值。

然后用这个

for(Item* theObj in arrayItems)
{//arrayItems is the array in which you have added the objects height and width
  ECGraphItem *tempItem = [[ECGraphItem alloc] init];    
  tempItem.isPercentage = YES;
  tempItem.yValue=theObj.height;
  tempItem.width = theObj.widht;
  tempItem.name = [NSString stringWithFormat@"Item: %d",/*set some number here*/];

  [items addObject:tempItem];
}

然后将其传递给图形

[graph drawHistogramWithItems:items lineWidth:2 color:[UIColor blackColor]];
于 2012-05-15T08:06:23.327 回答