-1

iPhoneアプリでグラフを作成するためにEASYGRAPHを使用しています問題は、静的な値を取得し、このグラフのView Controllerから値を動的にする必要があるグラフを表示することです。

グラフの 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

まず、幅と高さの 2 つのプロパティを含むクラスを作成します。次に、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 に答える