| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 | ////  DVYBarView.m//  xxxxx////  Created by Fire on 15/11/11.//  Copyright © 2015年 DuoLaiDian. All rights reserved.//#import "DVYBarView.h"#import "UIView+Extension.h"#import "UIColor+Hex.h"#define random(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)/255.0]#define randomColor random(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))@interface DVYBarView ()@property (strong, nonatomic) UIView *separate;@end@implementation DVYBarView- (instancetype)initWithFrame:(CGRect)frame {        if (self = [super initWithFrame:frame]) {                // 垂直坐标轴        UIView *separate = [[UIView alloc] init];                self.separate = separate;                [self addSubview:separate];                self.textFont = [UIFont systemFontOfSize:12];            }    return self;}- (void)draw {        self.backgroundColor = self.backColor;            // 计算坐标轴的位置以及大小    //    NSDictionary *attr = @{NSFontAttributeName : self.textFont};        //    CGSize labelSize = [@"x" sizeWithAttributes:attr];    CGSize labelSize = CGSizeMake(self.barWidth, 29);        self.separate.backgroundColor = self.axisColor;    self.separate.x = self.width - 1;    self.separate.width = 1;    self.separate.y = 0;    if (self.landspace) {        self.separate.height = self.height - labelSize.height  - self.xAxisTextGap;    }else{        self.separate.height = self.height  - self.xAxisTextGap;    }            // 为顶部留出的空白    CGFloat topMargin = 50;            // Label做占据的高度    CGFloat allLabelHeight = self.height - topMargin - self.xAxisTextGap - labelSize.height;    if (self.landspace) {        allLabelHeight = self.height - topMargin - self.xAxisTextGap - labelSize.height;    } else {        allLabelHeight = self.height - topMargin - self.xAxisTextGap;    }        NSDictionary *attr = @{NSFontAttributeName : self.textFont};        CGSize yLabelSize = [@"x" sizeWithAttributes:attr];    // Label之间的间隙    CGFloat labelMargin = (allLabelHeight + yLabelSize.height - (self.numberOfYAxisElements + 1) * yLabelSize.height) / self.numberOfYAxisElements;         // 移除所有的Label    for (UILabel *label in self.subviews) {        if ([label isKindOfClass:[UILabel class]]) {                        [label removeFromSuperview];        }    }        // 添加Label    for (int i = 0; i < self.numberOfYAxisElements + 1; i++) {        UILabel *label = [[UILabel alloc] init];        CGFloat avgValue = self.yAxisMaxValue / (self.numberOfYAxisElements);        if (self.isPercent) {            label.text = [NSString stringWithFormat:@"%.0f%%", avgValue * i];        }else{            label.text = [NSString stringWithFormat:@"%.0f", avgValue * i];        }        label.text = @"";        //label.backgroundColor = [UIColor redColor];        label.textAlignment = NSTextAlignmentRight;// UITextAlignmentRight;        label.font = self.textFont;        label.textColor = self.textColor;                label.x = 0;        label.height = yLabelSize.height;        if (self.landspace) {            label.y = self.height - labelSize.height - self.xAxisTextGap - (label.height + labelMargin) * i - label.height/2;        } else {            label.y = self.height - self.xAxisTextGap - (label.height + labelMargin) * i - label.height/2;        }                label.width = self.width - 1 - self.yAxisTextGap;       // [self addSubview:label];    }            //添加图例    UIView *LegendView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.width - 1 , self.height)];    [  self addSubview:LegendView];        //图例1    UIView *legendView1 = [[UIView alloc]initWithFrame:CGRectMake(0, 50, 20, 10)];    legendView1.centerX = LegendView.centerX;    legendView1.backgroundColor =[UIColor colorWithHexString:@"#FEC591"]; ;    [LegendView addSubview:legendView1];    //FEC591    UILabel *legendLabel1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 50 + 10 + 5, self.width - 1, 60)];    legendLabel1.textColor = [UIColor colorWithHexString:@"888888"];    legendLabel1.textAlignment = NSTextAlignmentCenter;    legendLabel1.numberOfLines = 0;    legendLabel1.font = [UIFont boldSystemFontOfSize:9];    legendLabel1.text = self.legendTitle1;    [LegendView addSubview:legendLabel1];        //图例2    UIView *legendView2 = [[UIView alloc]initWithFrame:CGRectMake(0, 150, 20, 10)];    legendView2.centerX = LegendView.centerX;    legendView2.backgroundColor = [UIColor colorWithHexString:@"#FD8B23"];    [LegendView addSubview:legendView2];        UILabel *legendLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 150 + 10 + 5, self.width - 1, 60)];    legendLabel2.textColor = [UIColor colorWithHexString:@"888888"];    legendLabel2.textAlignment = NSTextAlignmentCenter;    legendLabel2.numberOfLines = 0;    legendLabel2.font = [UIFont boldSystemFontOfSize:9];    legendLabel2.text = self.legendTitle2;    [LegendView addSubview:legendLabel2];}@end
 |