0

我有 iphone 应用程序,我在其中使用 EASYGRAPH api 进行图表我创建了显示视图,我在其中绘制百分比和图表我希望其中的值可能在创建应用程序时动态出现。就像我有 78% 的百分比那么应该如何分配这个值,这是我的代码中对显示视图的另一个视图

显示视图

 #import <UIKit/UIKit.h>
 @interface Display : UIView {

 }

 @end



  #import "Display.h"
  #import "ECGraph.h"
 @implementation Display


 - (id)initWithFrame:(CGRect)frame {

 self = [super initWithFrame:frame];
 if (self) {
    // Initialization code.
 }
 return self;
  }


- (void)drawRect:(CGRect)rect {
// Drawing code


CGContextRef _context = UIGraphicsGetCurrentContext();

ECGraph *graph = [[ECGraph alloc] initWithFrame:CGRectMake(500,-320,320, 200) withContext:_context isPortrait:NO];


ECGraphItem *item1 = [[ECGraphItem alloc] init];
item1.isPercentage = YES;
item1.yValue = 80;

我希望以以下方式设置百分比 totoalpercentage ,它位于我的 GraohsViewCONtroller 中,其 nin 是 DisplayView

像这样

    item1.yValue=totalpercentage;
item1.width = 35;
item1.name = @"item1";

ECGraphItem *item2 = [[ECGraphItem alloc] init];
item2.isPercentage = YES;
item2.yValue =17;
item2.width = 35; 
item2.name = @"item2";
/*
 ECGraphItem *item3 = [[ECGraphItem alloc] init];
 item3.isPercentage = YES;
 item3.yValue = 45;
 item3.width = 35;
 item3.name = @"item3";

 ECGraphItem *item4 = [[ECGraphItem alloc] init];
 item4.isPercentage = YES;
 item4.yValue = 78.6;
 item4.width = 35;
 item4.name = @"item4";

 ECGraphItem *item5 = [[ECGraphItem alloc] init];
 item5.isPercentage = YES;
 item5.yValue = 94.45;
 item5.width = 35;
 item5.name = @"item5"; */


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

     }

图形视图控制器

  - (void)viewDidLoad {
   [super viewDidLoad];

[self createGraph];

NSArray *percentages = [NSArray arrayWithObjects:@"80",@"17", nil];

[display setPercentageArray:percentages];
float costtoclient=[costToClient floatValue];
float markup=[clinicalMarkup floatValue];
float ProfitForCerenia=costtoclient/markup;
    float totalProfit=costtoclient-ProfitForCerenia;
int result = (int)ceilf(totalProfit );
    NSString*cerniaProfitTitle=[NSString stringWithFormat:@"%d",result];
[profitForEachCereniaButton setTitle:cerniaProfitTitle forState:UIControlStateNormal];

int one=[valueOne intValue];
int two=[valueTwo intValue];
int three=[valueThree intValue];
int four=[valueFour intValue];
    int dogsPerMonthCarSickness=one+two+three+four;
    int dogsPerYear=dogsPerMonthCarSickness*12;
int annualprofit=dogsPerYear*result;
NSString*annualProfitButtonTitle=[NSString stringWithFormat:@"%d",annualprofit];

[currentAnnualProfitFromCereniaButton setTitle:annualProfitButtonTitle forState:UIControlStateNormal];

int otherthancerenia=two+three+four;
    int visitclinicpermonth=[perMonth intValue];
     int otherthanCereniaAnnaul=otherthancerenia*12;

int potentialprofit=otherthanCereniaAnnaul*annualprofit;

NSString*potentialProfitTitle=[NSString stringWithFormat:@"%d",potentialprofit];

[potentialProfit setTitle:potentialProfitTitle forState:UIControlStateNormal];

     int cereniaUsedForCarSickness=[cerenia intValue];
 int cereniapercentageCal=cereniaUsedForCarSickness*100;
      int cereniaPercentageCalculate=cereniapercentageCal/dogsPerMonthCarSickness;
  NSString*cereniaUsedForCarSicknessTitle=[NSString stringWithFormat:@"%d",cereniaPercentageCalculate];

 [cereniaUsedForMotionSicknessButton setTitle:cereniaUsedForCarSicknessTitle forState:UIControlStateNormal];


int otherthancereniaperc=100-cereniaPercentageCalculate;

int otherthancereniapercentage=otherthancereniaperc;


//int remaining=dogsPerMonthCarSickness-cereniaUsedForCarSickness;
NSString*remainingTitle=[NSString stringWithFormat:@"%d",otherthancereniapercentage];

// [otherDrugsButton setTitle:remainingTitle forState:UIControlStateNormal]; [otherDrugsButton setTitle:remainingTitle forState:UIControlStateNormal];

dogsPerYearForCarSickness=[NSString stringWithFormat:@"%d",dogsPerYear];


     [dogsPerYearForCarSicknessButton setTitle:dogsPerYearForCarSickness forState:UIControlStateNormal];


 NSString *titledogsPerMonthForCarSicknessButton =[NSString stringWithFormat:@"%d",dogsPerMonthCarSickness];



[dogsPerMonthForCarSicknessButton setTitle:titledogsPerMonthForCarSicknessButton forState:UIControlStateNormal]
     int visitclinicperyear=visitclinicpermonth*12;

     float maxprofit=visitclinicperyear*0.17*result;
 int maximumprofit = (int)ceilf(maxprofit);
 NSString*maximumProfitTitle=[NSString stringWithFormat:@"%d",maximumprofit];
[maximumProfit setTitle:maximumProfitTitle forState:UIControlStateNormal];

}

  -(void)createGraph{


PieClass *myPieClass=[[PieClass alloc]initWithFrame:CGRectMake(50,440,320,230)];

myPieClass.backgroundColor=[UIColor clearColor];

myPieClass.itemArray=[[NSArray alloc]initWithObjects:valueOne,valueTwo,valueThree,valueFour, nil];

myPieClass.myColorArray=[[NSArray alloc]initWithObjects:[UIColor blueColor],[UIColor redColor],[UIColor greenColor],[UIColor brownColor], nil];

myPieClass.radius=100;

[self.view addSubview:myPieClass];

   }
4

1 回答 1

0

如果您需要将每个项目的百分比传递给 DisplayView,请在 DisplayView 中创建一个方法,

-(void) setPercentageArray:(NSArray*) 数组

然后从 GraphsViewController 调用此方法并传递数组,例如,

NSArray *percentages = [NSArray arrayWithObjects:@"80", @"75", @"90", nil];

在 DisplayView 中创建一个类成员变量,“setPercentageArray”这个方法将设置该变量。

将该数组用于各个项目。

编辑:

在 DisplayView 的 .h 中

@interface Display : UIView {

 NSArray *percentages;

}
-(void) setPercentageArray:(NSArray*) array;
@end

在 DisplayView.m 中,添加此方法,

-(void) setPercentageArray:(NSArray*) array
{
    percentages = array;
}

在 drawRect 方法中,你可以这样做,

 ECGraphItem *item1 = [[ECGraphItem alloc] init];
 item1.isPercentage = YES;
 item1.yValue = 80;     
 item1.yValue= [percentages objectAtIndex:0];
 item1.width = 35;
 item1.name = @"item1";

 ECGraphItem *item2 = [[ECGraphItem alloc] init];
 item2.isPercentage = [percentages objectAtIndex:1]
 item2.yValue =17;
 item2.width = 35; 
 item2.name = @"item2";
于 2012-05-14T11:34:02.167 回答