// // DVXBarView.m // xxxxx // // Created by Fire on 15/11/11. // Copyright © 2015年 DuoLaiDian. All rights reserved. // #import "DVXBarView.h" #import "UIView+Extension.h" #import "DVPlot.h" //#import "UIButton+Extension.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 DVXBarView () /** * 图表顶部留白区域 */ @property (assign, nonatomic) CGFloat topMargin; /** * 记录图表区域的高度 */ @property (assign, nonatomic) CGFloat chartHeight; /** * 记录坐标轴Label的高度 */ @property (assign, nonatomic) CGFloat textHeight; /** * 存放坐标轴的label(底部的) */ @property (strong, nonatomic) NSMutableArray *titleButtonArray; /** * 存放柱子顶部的label */ @property (strong, nonatomic) NSMutableArray *barTopButtonArray; /** * 记录点按钮的集合 */ @property (strong, nonatomic) NSMutableArray *barButtonArray; /** * 记录点按钮的集合2 */ @property (strong, nonatomic) NSMutableArray *barButtonArray2; /** * 记录点按钮的集合3 */ @property (strong, nonatomic) NSMutableArray *barButtonArray3; /** * 选中的点 */ @property (strong, nonatomic) UIButton *selectedBarButton; /** * 选中的点 */ @property (strong, nonatomic) UIButton *selectedBarButton2; /** * 选中的点 */ @property (strong, nonatomic) UIButton *selectedBarButton3; /** * 选中的柱子顶部的label */ @property (strong, nonatomic) UIButton *selectedBarTopButton; /** * 选中的坐标轴的label */ @property (strong, nonatomic) UIButton *selectedTitleButton; @end @implementation DVXBarView - (NSMutableArray *)titleButtonArray { if (_titleButtonArray == nil) { _titleButtonArray = [NSMutableArray array]; } return _titleButtonArray; } - (NSMutableArray *)barTopButtonArray { if (_barTopButtonArray == nil) { _barTopButtonArray = [NSMutableArray array]; } return _barTopButtonArray; } - (NSMutableArray *)barButtonArray { if (_barButtonArray == nil) { _barButtonArray = [NSMutableArray array]; } return _barButtonArray; } - (NSMutableArray *)barButtonArray2 { if (_barButtonArray2 == nil) { _barButtonArray2 = [NSMutableArray array]; } return _barButtonArray2; } - (NSMutableArray *)barButtonArray3 { if (_barButtonArray3 == nil) { _barButtonArray3 = [NSMutableArray array]; } return _barButtonArray3; } - (void)draw { self.backgroundColor = self.backColor; // 移除先前存在的所有视图 for (UIView *view in self.subviews) { [view removeFromSuperview]; } // 移除数组内所有的Label元素 [self.titleButtonArray removeAllObjects]; [self.barTopButtonArray removeAllObjects]; [self.barButtonArray removeAllObjects]; [self.barButtonArray2 removeAllObjects]; [self.barButtonArray3 removeAllObjects]; CGSize labelSize = CGSizeMake(self.barWidth, 29); // 添加坐标轴Label for (int i = 0; i < self.xAxisTitleArray.count; i++) { NSString *title = self.xAxisTitleArray[i]; UIButton *button = [[UIButton alloc] init]; [button setTitle:title forState:UIControlStateNormal]; // button.backgroundColor = [UIColor redColor ]; button.titleLabel.font = self.textFont; [button setTitleColor:self.textColor forState:UIControlStateNormal]; [button setTitleColor:self.barSelectedColor forState:UIControlStateSelected]; button.tag = 100 + i; button.titleLabel.font = [UIFont boldSystemFontOfSize:11]; [button addTarget:self action:@selector(barDidClicked:) forControlEvents:UIControlEventTouchUpInside]; button.titleLabel.numberOfLines = 0; button.x = (i + 1) * self.barGap + i * labelSize.width - 12.5 - self.barGap / 2; // button.y = self.height - labelSize.height; if (self.landspace) { button.y = self.height - labelSize.height; } else { button.y = 0; } button.width = labelSize.width + 25; button.height = labelSize.height; [self.titleButtonArray addObject:button]; [self addSubview:button]; } // 添加坐标轴 self.textHeight = labelSize.height; UIView *view = [[UIView alloc] init]; view.backgroundColor = self.axisColor; view.height = 1; view.width = self.width + 200; view.x = -200; // view.y = self.height - labelSize.height - self.xAxisTextGap; if (self.landspace) { view.y = self.height - labelSize.height - self.xAxisTextGap; } else { view.y = self.height - 0 - self.xAxisTextGap; } [self addSubview:view]; self.topMargin = 50; if (self.landspace) { self.chartHeight = self.height - labelSize.height - self.xAxisTextGap - self.topMargin; } else { self.chartHeight = self.height - self.xAxisTextGap - self.topMargin; } for (int i = 0; i < self.xValues.count; i++) { NSNumber *value = self.xValues[i]; NSNumber *value2 = self.xValues2[i]; NSString *title = [self decimalwithFormat:@"0" floatV:value.floatValue]; // NSString *title2 = [self decimalwithFormat:@"0" floatV:value2.floatValue]; if (value.floatValue < 0) { value = @(0); } if (value2.floatValue < 0) { value2 = @(0); } // if (i == 0 ) { // // }else { // // } CGPoint center = CGPointMake((i+1)*self.barGap + i*self.barWidth - self.barGap / 2, self.chartHeight - value.floatValue/self.yAxisMaxValue * self.chartHeight + self.topMargin); CGFloat a1 = (i+1)*self.barGap + i*self.barWidth - self.barGap / 2; // CGFloat b1 = (i+1)*self.barGap + i*self.barWidth; CGPoint center2 = CGPointMake(a1, self.chartHeight - value.floatValue/self.yAxisMaxValue * self.chartHeight- value2.floatValue/self.yAxisMaxValue * self.chartHeight + self.topMargin); if (self.yAxisMaxValue * self.chartHeight == 0) { center = CGPointMake((i+1)*self.barGap + i*self.barWidth, self.chartHeight + self.topMargin); } // 添加point处的Label if (self.isShowPointLabel) { [self addLabelWithTitle:title atLocation:center andTag:i]; } [self addVerticalBGViewAtLocationIndex:i]; CGPoint linePoint = CGPointMake((i+1)*(self.barGap + self.barWidth ) , 0); [self addVerticalLineAtLocation:linePoint]; //第一组 UIButton *button = [[UIButton alloc] init]; button.tag = i + 300; // [button setBackgroundColor:self.barColor forState:UIControlStateNormal]; // [button setBackgroundColor:self.barSelectedColor forState:UIControlStateSelected]; [button setBackgroundImage:[self imageWithColor:self.barColor] forState:UIControlStateNormal]; [button setBackgroundImage:[self imageWithColor:self.barColor] forState:UIControlStateSelected]; button.titleLabel.numberOfLines = 0; button.titleLabel.textAlignment = NSTextAlignmentCenter; [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected]; button.titleLabel.font = self.textFont; // button.layer.backgroundColor = self.backColor.CGColor; CGSize buttonSize = CGSizeMake(self.barWidth, value.floatValue/self.yAxisMaxValue * self.chartHeight); if (self.yAxisMaxValue == 0) { button.size = CGSizeMake(self.barWidth, 0); }else{ button.size = buttonSize; } button.x = center.x; button.y = center.y; if (self.shotImage) { } button.userInteractionEnabled = self.isBarUserInteractionEnabled; [button addTarget:self action:@selector(barDidClicked:) forControlEvents:UIControlEventTouchUpInside]; [self.barButtonArray addObject:button]; if (button.userInteractionEnabled) { if (self.index < 0) { if (i == 0) { // [self barDidClicked:button]; } }else{ if (i == self.index) { //[self barDidClicked:button]; } } } [self addSubview:button]; if (self.shotImage == true) { button.width = 45; button.height = (value.floatValue/self.yAxisMaxValue + 0.1) * self.chartHeight; button.x = (i)*self.barGap + i*self.barWidth + 2.5; button.y = self.chartHeight - (value.floatValue/self.yAxisMaxValue + 0.1) * self.chartHeight + self.topMargin; [button setTitle:[NSString stringWithFormat:@"%@",self.xTitles[i]] forState:UIControlStateNormal]; } //第二组 if (self.xValues2.count != 0) { UIButton *button2 = [[UIButton alloc] init]; button2.tag = i + 400; [button2 setBackgroundImage:[self imageWithColor:self.barColor2] forState:UIControlStateNormal]; [button2 setBackgroundImage:[self imageWithColor:self.barColor2] forState:UIControlStateSelected]; [button2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [button2 setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected]; button2.titleLabel.font = self.textFont; button2.titleLabel.numberOfLines = 0; button2.titleLabel.textAlignment = NSTextAlignmentCenter; CGSize buttonSize2 = CGSizeMake(self.barWidth , value2.floatValue/self.yAxisMaxValue * self.chartHeight); if (self.yAxisMaxValue == 0) { button2.size = CGSizeMake(self.barWidth, 0); }else{ button2.size = buttonSize2; } button2.x = center2.x; button2.y = center2.y; button2.userInteractionEnabled = self.isBarUserInteractionEnabled; [button2 addTarget:self action:@selector(barDidClicked:) forControlEvents:UIControlEventTouchUpInside]; [self.barButtonArray2 addObject:button2]; if (button.userInteractionEnabled) { if (self.index < 0) { if (i == 0) { // [self barDidClicked:button2]; } }else{ if (i == self.index) { // [self barDidClicked:button2]; } } } [self addSubview:button2]; if (self.shotImage == YES) { button2.width = 45; button2.height = (value2.floatValue/self.yAxisMaxValue + 0.1) * self.chartHeight; button2.x = (i)*self.barGap + i*self.barWidth + 2.5; button2.y = self.chartHeight - (value.floatValue/self.yAxisMaxValue + 0.1) * self.chartHeight- (value2.floatValue/self.yAxisMaxValue + 0.1) * self.chartHeight + self.topMargin; [button2 setTitle:[NSString stringWithFormat:@"%@",self.xTitles2[i]] forState:UIControlStateNormal]; } } //第三组 if ( self.xValues3.count != 0) { NSNumber *value3 = self.xValues3[i]; // NSString *title3 = [self decimalwithFormat:@"0" floatV:value3.floatValue]; if (value3.floatValue < 0) { value3 = @(0); } CGPoint center3 = CGPointMake(a1, self.chartHeight - value.floatValue/self.yAxisMaxValue * self.chartHeight- value2.floatValue/self.yAxisMaxValue * self.chartHeight - value3.floatValue/self.yAxisMaxValue * self.chartHeight + self.topMargin); UIButton *button3 = [[UIButton alloc] init]; button3.tag = i + 500; [button3 setBackgroundImage:[self imageWithColor:self.barColor3] forState:UIControlStateNormal]; [button3 setBackgroundImage:[self imageWithColor:self.barColor3] forState:UIControlStateSelected]; [button3 setTitleColor:self.textColor forState:UIControlStateNormal]; button3.titleLabel.numberOfLines = 0; button3.titleLabel.textAlignment = NSTextAlignmentCenter; [button3 setTitleColor:self.barSelectedColor forState:UIControlStateSelected]; button3.titleLabel.font = self.textFont; //button.layer.backgroundColor = self.backColor.CGColor; CGSize buttonSize3 = CGSizeMake(self.barWidth, value3.floatValue/self.yAxisMaxValue * self.chartHeight); if (self.yAxisMaxValue == 0) { button3.size = CGSizeMake(self.barWidth, 0); }else{ button3.size = buttonSize3; } button3.x = center3.x; button3.y = center3.y; button3.userInteractionEnabled = self.isBarUserInteractionEnabled; [button3 addTarget:self action:@selector(barDidClicked:) forControlEvents:UIControlEventTouchUpInside]; [self.barButtonArray3 addObject:button3]; if (button3.userInteractionEnabled) { if (self.index < 0) { if (i == 0) { // [self barDidClicked:button2]; } }else{ if (i == self.index) { // [self barDidClicked:button2]; } } } [self addSubview:button3]; } } [self setNeedsDisplay]; } - (NSString *) decimalwithFormat:(NSString *)format floatV:(float)floatV { NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; [numberFormatter setPositiveFormat:format]; return [numberFormatter stringFromNumber:[NSNumber numberWithFloat:floatV]]; } //添加竖线 -(void)addVerticalLineAtLocation:(CGPoint)location{ UIView *line = [[UIView alloc]init]; line.backgroundColor = self.axisColor; line.x = location.x; line.y = location.y; line.width = 1; if (self.landspace) { line.height = self.height - self.xAxisTextGap - 29; } else { line.height = self.height - self.xAxisTextGap; } [self addSubview:line]; } //添加bgview -(void)addVerticalBGViewAtLocationIndex:(int)index{ CGPoint location = CGPointMake((index)*(self.barGap + self.barWidth ) , 0); UIView *bgView = [[UIView alloc]init]; //bgView.backgroundColor = randomColor; bgView.x = location.x; bgView.y = location.y; bgView.width = self.barGap + self.barWidth ; if (self.landspace) { bgView.height = self.height - self.xAxisTextGap - 29; } else { bgView.height = self.height - self.xAxisTextGap; } bgView.tag = 200000 + index; [self addSubview:bgView]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)]; [bgView addGestureRecognizer:tap]; } -(void)tapAction:(UITapGestureRecognizer *)tap{ NSInteger index = tap.view.tag - 200000 ; NSLog(@"点击了 第%ld块区域 ",index); [self selectedIndex:index]; if ([self.delegate respondsToSelector:@selector(xBarView:didClickButtonAtIndex:)]) { [self.delegate xBarView:self didClickButtonAtIndex:index]; } } // 添加pointLabel的方法 - (void)addLabelWithTitle:(NSString *)title atLocation:(CGPoint)location andTag:(NSInteger)tag { UIButton *button = [[UIButton alloc] init]; if (self.isPercent) { [button setTitle:[NSString stringWithFormat:@"%@%%", title] forState:UIControlStateNormal]; }else{ [button setTitle:title forState:UIControlStateNormal]; } [button setTitleColor:self.textColor forState:UIControlStateNormal]; [button setTitleColor:self.barSelectedColor forState:UIControlStateSelected]; button.titleLabel.font = self.textFont; button.layer.backgroundColor = self.backColor.CGColor; // NSDictionary *attr = @{NSFontAttributeName : self.textFont}; // CGSize labelSize = [label.text sizeWithAttributes:attr]; CGSize labelSize = CGSizeMake(self.barWidth, 12); button.titleLabel.textAlignment = NSTextAlignmentCenter; button.width = labelSize.width; button.height = labelSize.height; button.x = location.x; // - label.width / 2; button.y = location.y - button.height - 3; button.tag = 200+tag; [button addTarget:self action:@selector(barDidClicked:) forControlEvents:UIControlEventTouchUpInside]; button.hidden = YES; [self.barTopButtonArray addObject:button]; [self addSubview:button]; } - (void)barDidClicked:(UIButton *)button { NSInteger index = 0; if (button.tag >= 100 && button.tag < 200) { index = button.tag - 100; } else if (button.tag >= 200 && button.tag < 300) { index = button.tag - 200 ; } else if ( button.tag >= 300 && button.tag < 400){ index = button.tag - 300 ; }else if ( button.tag >= 400 && button.tag < 500){ index = button.tag - 400 ; }else if ( button.tag >= 500 ){ index = button.tag - 500 ; } self.selectedBarButton.selected = NO; self.selectedBarTopButton.selected = NO; self.selectedTitleButton.selected = NO; UIButton *barButton = self.barButtonArray[index]; if (self.xValues2.count != 0) { self.selectedBarButton2.selected = NO; UIButton *barButton2 = self.barButtonArray2[index]; barButton2.selected = YES; self.selectedBarButton2 = barButton2; } if (self.xValues3.count != 0) { self.selectedBarButton3.selected = NO; UIButton *barButton3 = self.barButtonArray3[index]; barButton3.selected = YES; self.selectedBarButton3 = barButton3; } UIButton *barTopButton = self.barTopButtonArray[index]; UIButton *titleButton = self.titleButtonArray[index]; barButton.selected = YES; barTopButton.selected = YES; titleButton.selected = YES; self.selectedBarButton = barButton; self.selectedBarTopButton = barTopButton; self.selectedTitleButton = titleButton; [self selectedIndex:index]; if ([self.delegate respondsToSelector:@selector(xBarView:didClickButtonAtIndex:)]) { [self.delegate xBarView:self didClickButtonAtIndex:index]; } } -(void)reversalSelected:(BOOL)selected{ if (selected == YES) { } } -(void)selectedIndex:(NSInteger)index{ if (self.barButtonArray.count != 0 && self.barButtonArray2.count != 0 && self.barButtonArray3.count == 0) { for (int i = 0; i < self.barButtonArray.count; i ++ ) { NSNumber *value = self.xValues[i]; NSNumber *value2 = self.xValues2[i]; if (value.floatValue < 0) { value = @(0); } if (value2.floatValue < 0) { value2 = @(0); } CGPoint center = CGPointMake((i+1)*self.barGap + i*self.barWidth - self.barGap / 2, self.chartHeight - value.floatValue/self.yAxisMaxValue * self.chartHeight + self.topMargin); CGFloat a1 = (i+1)*self.barGap + i*self.barWidth - self.barGap / 2; // CGFloat b1 = (i+1)*self.barGap + i*self.barWidth; CGPoint center2 = CGPointMake(a1, self.chartHeight - value.floatValue/self.yAxisMaxValue * self.chartHeight- value2.floatValue/self.yAxisMaxValue * self.chartHeight + self.topMargin); CGSize buttonSize1 = CGSizeMake(self.barWidth, value.floatValue/self.yAxisMaxValue * self.chartHeight); CGSize buttonSize2 = CGSizeMake(self.barWidth, value2.floatValue/self.yAxisMaxValue * self.chartHeight); UIButton *barButton = self.barButtonArray[i]; UIButton *barButton2 = self.barButtonArray2[i]; if (i == index) { if (i == 0 ) {//如果是第一个 往右边 barButton.width = 78; barButton.height = (value.floatValue/self.yAxisMaxValue + 0.1) * self.chartHeight; barButton.x = i*(self.barWidth + self.barGap) ; barButton.y = self.chartHeight - (value.floatValue/self.yAxisMaxValue + 0.1) * self.chartHeight + self.topMargin; [barButton setTitle:[NSString stringWithFormat:@"%@",self.xTitles[i]] forState:UIControlStateNormal]; barButton2.width = 78; barButton2.height = (value2.floatValue/self.yAxisMaxValue + 0.1) * self.chartHeight; barButton2.x = i*(self.barWidth + self.barGap) ; barButton2.y = self.chartHeight - (value.floatValue/self.yAxisMaxValue + 0.1) * self.chartHeight- (value2.floatValue/self.yAxisMaxValue + 0.1) * self.chartHeight + self.topMargin; [barButton2 setTitle:[NSString stringWithFormat:@"%@",self.xTitles2[i]] forState:UIControlStateNormal]; }else{ barButton.width = 78; barButton.height = (value.floatValue/self.yAxisMaxValue + 0.1) * self.chartHeight; barButton.x = i*(self.barWidth + self.barGap) - 14; barButton.y = self.chartHeight - (value.floatValue/self.yAxisMaxValue + 0.1) * self.chartHeight + self.topMargin; [barButton setTitle:[NSString stringWithFormat:@"%@",self.xTitles[i]] forState:UIControlStateNormal]; barButton2.width = 78; barButton2.height = (value2.floatValue/self.yAxisMaxValue + 0.1) * self.chartHeight; barButton2.x = i*(self.barWidth + self.barGap) - 14; barButton2.y = self.chartHeight - (value.floatValue/self.yAxisMaxValue + 0.1) * self.chartHeight- (value2.floatValue/self.yAxisMaxValue + 0.1) * self.chartHeight + self.topMargin; [barButton2 setTitle:[NSString stringWithFormat:@"%@",self.xTitles2[i]] forState:UIControlStateNormal]; } }else{ barButton.size = buttonSize1; barButton.y = center.y; barButton.x = (i+1)*self.barGap + i*self.barWidth - self.barGap / 2; [barButton setTitle:@"" forState:UIControlStateNormal]; barButton2.size = buttonSize2; barButton2.y = center2.y; barButton2.x = (i+1)*self.barGap + i*self.barWidth - self.barGap / 2; [barButton2 setTitle:@"" forState:UIControlStateNormal]; } } } } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"%s",__func__); if (self.barButtonArray.count != 0 && self.barButtonArray2.count != 0 && self.barButtonArray3.count == 0) { for (int i = 0; i < self.barButtonArray.count; i ++ ) { UIButton *barButton = self.barButtonArray[i]; UIButton *barButton2 = self.barButtonArray2[i]; barButton.width = 30; barButton.x = (i+1)*self.barGap + i*self.barWidth - self.barGap / 2 ; [barButton setTitle:@"" forState:UIControlStateNormal]; barButton2.width = 30; barButton2.x = (i+1)*self.barGap + i*self.barWidth - self.barGap / 2; [barButton2 setTitle:@"" forState:UIControlStateNormal]; } } } - (UIImage *)imageWithColor:(UIColor *)color { CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [color CGColor]); CGContextFillRect(context, rect); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } @end