【代碼筆記】柱狀圖,筆記柱狀圖

來源:互聯網
上載者:User

【代碼筆記】柱狀圖,筆記柱狀圖

一,。

二,工程圖。

三,代碼。

RootViewController.h

#import <UIKit/UIKit.h>@interface RootViewController : UIViewController{    UIView* zhuView;}@end

 

RootViewController.m

#import "RootViewController.h"@interface RootViewController ()@end@implementation RootViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view.        self.title=@"柱狀圖";    self.view.backgroundColor=[UIColor greenColor];            [self initZhuView:[NSArray arrayWithObjects:@"10",@"20",@"30",@"40",@"50",@"60",@"70",@"80",@"90",nil]];    }#pragma -mark -柱狀圖初始化- (void)initZhuView:(NSArray*)days{        if ([[UIScreen mainScreen] bounds].size.height > 480) {        zhuView = [[UIView alloc] initWithFrame:CGRectMake(0, 310 - 45 + 50, 320, 150)];    }    else    {        zhuView = [[UIView alloc] initWithFrame:CGRectMake(0, 310 - 45, 320, 150)];    }    //柱狀圖所在的背景圖    [self.view addSubview:zhuView];        //0天,50天,100天所在的豎線    UIView* shuView = [[UIView alloc] initWithFrame:CGRectMake(55, 0, 1, 120)];    [shuView setBackgroundColor:[UIColor orangeColor]];    [zhuView addSubview:shuView];        //0-9所在的橫線    UIView* hengView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMinX(shuView.frame), CGRectGetMaxY(shuView.frame), 320 - CGRectGetMinX(shuView.frame) - 20, 1)];    [hengView setBackgroundColor:[UIColor orangeColor]];    [zhuView addSubview:hengView];            NSArray* titles = [NSArray arrayWithObjects:@"100天",@"50天",@"0天", nil];    for (int i = 0 ; i < 3; i++) {        //天數所在Label        UILabel* yibaiLb = [[UILabel alloc] initWithFrame:CGRectMake(0, 5 + i* 50, 53, 15)];        [yibaiLb setText:[titles objectAtIndex:i]];        [yibaiLb setTextAlignment:NSTextAlignmentRight];        [yibaiLb setFont:[UIFont systemFontOfSize:14]];        [yibaiLb setBackgroundColor:[UIColor clearColor]];        [zhuView addSubview:yibaiLb];    }        for (int i = 0; i < [days count]; i++) {                int height = [[days objectAtIndex:i] intValue];        if(height>=100)            height=100;                //紅色的豎線的柱狀圖        UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(shuView.frame) + 10 + i*25, CGRectGetMinY(hengView.frame) - height, 15, height)];        [imageView setBackgroundColor:[UIColor redColor]];        [zhuView addSubview:imageView];                //柱狀圖上的紅色的數字Label        UILabel* dayLB = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(imageView.frame) - 5, CGRectGetMinY(imageView.frame) - 10, 25, 10)];        [dayLB setTextAlignment:NSTextAlignmentCenter];        [dayLB setBackgroundColor:[UIColor clearColor]];        [dayLB setFont:[UIFont systemFontOfSize:10]];        [dayLB setText:[NSString stringWithFormat:@"%d",[[days objectAtIndex:i] intValue]]];        [zhuView addSubview:dayLB];                //1-9的數字所在的Label        UILabel* diLb = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(imageView.frame), CGRectGetMaxY(hengView.frame) , 15, 20)];        [diLb setText:[NSString stringWithFormat:@"%d",i+1]];        [diLb setBackgroundColor:[UIColor clearColor]];        [diLb setTextAlignment:NSTextAlignmentCenter];        [diLb setFont:[UIFont systemFontOfSize:14]];        [zhuView addSubview:diLb];    }}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.