Computer Science/ios

food chart

728x90
반응형

//

//  BarChartView.h

//  BarChart

//

//  Created by Park Jong Pil on 11. 7. 25..

//  Copyright 2011 Reciper. All rights reserved.

//


#import <UIKit/UIKit.h>



@interface FoodBarChartComponent : NSObject

{

    NSString *xValue;

    float yValue;

}


@property (nonatomic, retain) NSString *xValue;

@property (nonatomic, assign) float yValue;


@end



#define X_LABEL_COUNT 10

#define Y_LABEL_COUNT 10


@interface FoodBarChartView : UIView

{

    CGRect xLabelFrame[X_LABEL_COUNT];

    UILabel *xLabelForFrame[X_LABEL_COUNT];

    

    CGRect yLabelFrame[Y_LABEL_COUNT];

    UILabel *yLabelForFrame[Y_LABEL_COUNT];

}


@property (nonatomic, retain) NSMutableArray *components;

@property (nonatomic, retain) UIView *plotBackground;

@property (nonatomic, retain) NSMutableArray *colors;


- (void)drawLine:(CGContextRef)context;

- (void)createXLabel;

- (void)createYLabel;

- (float)calcMaxValue;


@end


//

//  BarChartView.m

//  BarChart

//

//  Created by Park Jong Pil on 11. 7. 25..

//  Copyright 2011 Reciper. All rights reserved.

//


#import "FoodBarChartView.h"

#import "LPGlobal.h"

#import "BarPlot.h"

#import "AppDelegate.h"


@implementation FoodBarChartComponent


@synthesize xValue;

@synthesize yValue;


@end



#define LINE_HEIGHT_MARGIN 22.5 //배경축 시작높이

#define X_LABEL_WIDTH_MARGIN 40.0 //x 레이블 간격

#define Y_LABEL_HEIGHT_MARGIN 22.5 //y 레이블 간격

#define BAR_MAX_HEIGHT 200.0 // 최대높이

#define BAR_BASE_Y 225.0 // 시작위치

#define BAR_WIDTH 8.0 // 넓이

#define BAR_MARGIN 32.0 // 간격

//#define CURRENCY_UNIT 100


#define FIRST_COLOR UIColorFromRGB(0xe07e59)

#define SECOND_COLOR UIColorFromRGB(0x9ac05b)

#define THIRD_COLOR UIColorFromRGB(0xecbd3d)

#define FOURTH_COLOR UIColorFromRGB(0x67defc)

#define FIFTH_COLOR UIColorFromRGB(0x7981cd)

#define SIXTH_COLOR UIColorFromRGB(0xecbd3d)

#define SEVENTH_COLOR UIColorFromRGB(0xe07e59)

#define EIGHTH_COLOR UIColorFromRGB(0x67defc)

#define NINTH_COLOR UIColorFromRGB(0x7981cd)

#define TENTH_COLOR UIColorFromRGB(0x9ac05b)


@implementation FoodBarChartView


@synthesize components;

@synthesize plotBackground;

@synthesize colors;



- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self

    {

        // Initialization code

        self.colors = [NSMutableArray arrayWithObjects:

                       FIRST_COLOR

                       SECOND_COLOR

                       THIRD_COLOR

                       FOURTH_COLOR

                       FIFTH_COLOR,

                       SIXTH_COLOR,

                       SEVENTH_COLOR,

                       EIGHTH_COLOR,

                       NINTH_COLOR,

                       TENTH_COLOR,nil];

        

        [self createXLabel];

        [self createYLabel];

    }

    return self;

}


// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect

{

    // 컨텍스트.

    CGContextRef context = UIGraphicsGetCurrentContext();

    

    [self drawLine:context];

    

    self.plotBackground = [[UIView alloc] initWithFrame:CGRectMake(0.0, 300.0, 480.0, 0.0)];

    self.plotBackground.backgroundColor = [UIColor clearColor];

    self.plotBackground.clipsToBounds = YES;

    [self addSubview:self.plotBackground];

    

    

    // 차트 그리기.

    if ([self.components count] > 0)

    {

        // Y 최대값.

        float maxY = [self calcMaxValue];

        

        for (int i = 0; i < [self.components count]; i++)

        {

            

            FoodBarChartComponent *component = [self.components objectAtIndex:i];

            

            //db

            AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];

            

            if(i==0){

                component.yValue = [delegate.food1YValue floatValue];

            }else if (i==1){

                component.yValue = [delegate.food2YValue floatValue];

            }    else if (i==2){

                component.yValue = [delegate.food3YValue floatValue];

            }    else if (i==3){

                component.yValue = [delegate.food4YValue floatValue];

            }    else if (i==4){

                component.yValue = [delegate.food5YValue floatValue];

            }    else if (i==5){

                component.yValue = [delegate.food6YValue floatValue];

            }    else if (i==6){

                component.yValue = [delegate.food7YValue floatValue];

            }    else if (i==7){

                component.yValue = [delegate.food8YValue floatValue];

            }    else if (i==8){

                component.yValue = [delegate.food9YValue floatValue];

            }    else {

                component.yValue = [delegate.food10YValue floatValue];

            }


            

            //

            

            // 바의 높이 계산.

            

            float barHeight = (200 *component.yValue) / maxY;

            NSLog(@">>>>>>>>>%f", barHeight);

            

            // 바의 프레임.

            CGRect barRect = CGRectMake(60.0 + ((BAR_WIDTH + BAR_MARGIN) * i),

                                        (BAR_BASE_Y - barHeight), 

                                        BAR_WIDTH, barHeight);

            

            BarPlot *barPlot = [[BarPlot alloc] initWithFrame:barRect];

            barPlot.barColor = [self.colors objectAtIndex:i];

            [self.plotBackground addSubview:barPlot];


            NSString *XLabelText = component.xValue;

            xLabelForFrame[i].text = XLabelText;            

            CGSize XLabelSize = [XLabelText sizeWithFont:[UIFont systemFontOfSize:xLabelForFrame[i].font.pointSize]];

            xLabelForFrame[i].frame = CGRectMake(xLabelForFrame[i].frame.origin.x, xLabelForFrame[i].frame.origin.y, XLabelSize.width, XLabelSize.height);

            

            NSString *YLabelText = @"[ Food Recommend ]";

            yLabelForFrame[1].text = YLabelText;

            CGSize YLabelSize = [YLabelText sizeWithFont:[UIFont systemFontOfSize:yLabelForFrame[1].font.pointSize]];

            yLabelForFrame[1].frame = CGRectMake(yLabelForFrame[1].frame.origin.x, yLabelForFrame[1].frame.origin.y, YLabelSize.width, YLabelSize.height);


        }

    }

    

    // 애니메이션.

    [UIView beginAnimations:nil context:nil];

    [UIView setAnimationDuration:1.1];

    [UIView setAnimationDelegate:self];

    self.plotBackground.frame = CGRectMake(0.0, 0.0, 480.0, 250.0);

    [UIView commitAnimations];

}


// 백그라운드 라인 디자인.

- (void)drawLine:(CGContextRef)context

{

    // 라인 컬러 설정.

CGColorRef lineColor = RGB(222, 222, 222).CGColor;

    

// 수평선 그리기.

    for (int i = 0; i < Y_LABEL_COUNT; i++) 

    {

        // 위치.

        CGPoint startPoint = CGPointMake(35.0, 25.0 + (LINE_HEIGHT_MARGIN * i));

        CGPoint endPoint = CGPointMake(450.0, 25.0 + (LINE_HEIGHT_MARGIN * i));

        

        if (i != 9)

        {

            draw1PxStroke(context, startPoint, endPoint, lineColor);

        }

        else

        { 

            CGContextSetStrokeColorWithColor(context, lineColor);

            CGContextSetLineWidth(context, 4.0f);

            CGContextMoveToPoint(context, startPoint.x, startPoint.y);

            CGContextAddLineToPoint(context, endPoint.x, endPoint.y);

            CGContextStrokePath(context);

        }

    }

    

    // 수직선 그리기.

    CGPoint startPoint = CGPointMake(35.0, 17.0);

    CGPoint endPoint = CGPointMake(35.0, 230.0);

    draw1PxStroke(context, startPoint, endPoint, lineColor);

}


// X 라벨 디자인.

- (void)createXLabel

{

    for (int i = 0; i < X_LABEL_COUNT; i++)

    {

        int index = i;

        CGRect frame = CGRectMake(50.0 + (X_LABEL_WIDTH_MARGIN * i), 235.0, X_LABEL_WIDTH_MARGIN, 10.0);

        xLabelFrame[index] = frame;

        UILabel *xLabel = [[UILabel alloc] init];

        

        xLabelForFrame[index] = xLabel;

        xLabel.frame = frame;

        xLabel.tag = index;

        xLabel.backgroundColor = [UIColor clearColor];

        xLabel.font = [UIFont systemFontOfSize:10.5];

        xLabel.textAlignment = UITextAlignmentCenter;

        xLabel.textColor = [UIColor grayColor];

        

        [self addSubview:xLabel];


    }

}


// Y 라벨 디자인.

- (void)createYLabel

{

    for (int i = 0; i < Y_LABEL_COUNT; i++)

    {

        int index = i;

        

        CGRect frame = CGRectMake(50.0, 2.0 + (Y_LABEL_HEIGHT_MARGIN * i), 65.0, 0.0);

        

        yLabelFrame[index] = frame;

        UILabel *yLabel = [[UILabel alloc] init];

        yLabelForFrame[index] = yLabel;


        yLabel.frame = frame;

        yLabel.tag = index;

        yLabel.backgroundColor = [UIColor clearColor];

        yLabel.font = [UIFont systemFontOfSize:20];

        yLabel.textAlignment = UITextAlignmentRight;

        yLabel.textColor = [UIColor darkGrayColor];

        [self addSubview:yLabel];


    }

}


// Y 최대값 계산.

- (float)calcMaxValue

{

    NSMutableArray *numberArray = [NSMutableArray array];

    for (FoodBarChartComponent *component in self.components)

    {

        [numberArray addObject:[NSNumber numberWithFloat:component.yValue]];

    }

    

    // 배열에서 최대값.

    float highestNumber = 0.0;

    int numberIndex = 0;

    for (NSNumber *theNumber in numberArray)

    {

        if ([theNumber floatValue] > highestNumber) 

        {

            highestNumber = [theNumber integerValue];

            numberIndex = [numberArray indexOfObject:theNumber];

        }

    }

    

    return highestNumber;

}


@end


//

//  BarChartViewController.h

//  BarChart

//

//  Created by Park Jong Pil on 11. 7. 25..

//  Copyright 2011 Reciper. All rights reserved.

//


#import <UIKit/UIKit.h>

#import "LPGlobal.h"



@class FoodBarChartView;


@interface FoodChartViewController : UIViewController <UIScrollViewDelegate>


@property (nonatomic, retain) NSArray *data;

@property (nonatomic, retain) FoodBarChartView *barChart;


- (NSMutableArray *)loadData;


@end


//

//  BarChartViewController.m

//  BarChart

//

//  Created by Park Jong Pil on 11. 7. 25..

//  Copyright 2011 Reciper. All rights reserved.

//


#import "FoodChartViewController.h"

#import "FoodBarChartView.h"



@implementation FoodChartViewController


@synthesize data;

@synthesize barChart;


- (void)didReceiveMemoryWarning

{

    // Releases the view if it doesn't have a superview.

    [super didReceiveMemoryWarning];

    

    // Release any cached data, images, etc that aren't in use.

}


#pragma mark - View lifecycle


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

- (void)viewDidLoad

{

    [super viewDidLoad];

    

    // 데이터.

    self.data = [self loadData];

    

    // 차트 .

    self.barChart = [[FoodBarChartView alloc] initWithFrame:CGRectMake(0.0, 0.0, 480.0, 400.0)];

    self.barChart.backgroundColor = RGB(247, 247, 247);

    [self.view addSubview:barChart];

    

    NSMutableArray *components = [NSMutableArray array];

    for (int i = 0; i < [self.data count]; i++)

    {

        NSDictionary *dict = [self.data objectAtIndex:i];

        FoodBarChartComponent *component = [[FoodBarChartComponent alloc] init];

        component.xValue = [dict objectForKey:@"xValue"];

        component.yValue = [[dict objectForKey:@"yValue"] floatValue];

        

        [components addObject:component];

    }

    [barChart setComponents:components];

}


- (void)viewDidUnload

{

    [super viewDidUnload];

    // Release any retained subviews of the main view.

    // e.g. self.myOutlet = nil;

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    // Return YES for supported orientations

    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);

}


#pragma mark - 커스텀 메서드


// 샘플 데이터 로드.

- (NSMutableArray *)loadData

{

    NSString *path = [[NSBundle mainBundle] pathForResource:@"DataFood" ofType:@"plist"];

NSMutableArray *sampleData = [[NSMutableArray alloc] initWithContentsOfFile:path];

    

return sampleData;

}


@end

728x90
반응형