123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- //
- // DVBarChartView.m
- // xxxxx
- //
- // Created by Fire on 15/11/11.
- // Copyright © 2015年 DuoLaiDian. All rights reserved.
- //
- #import "DVBarChartView.h"
- #import "DVXBarView.h"
- #import "DVYBarView.h"
- #import "UIColor+Hex.h"
- #import "UIView+Extension.h"
- @interface DVBarChartView () <DVXBarViewDelegate>
- @property (strong, nonatomic) UIScrollView *scrollView;
- @property (strong, nonatomic) DVXBarView *xBarView;
- @property (strong, nonatomic) DVYBarView *yBarView;
- @property (assign, nonatomic) CGFloat gap;
- @end
- @implementation DVBarChartView
- -(NSMutableArray *)xValues{
- if (!_xValues) {
- _xValues = [NSMutableArray array];
- }
- return _xValues;
- }
- -(NSMutableArray *)xValues2{
- if (!_xValues2) {
- _xValues2 = [NSMutableArray array];
- }
- return _xValues2;
- }
- -(NSMutableArray *)xValues3{
- if (!_xValues3) {
- _xValues3 = [NSMutableArray array];
- }
- return _xValues3;
- }
- - (void)draw {
-
-
- if (self.xValues.count == 0) return;
-
- // 设置y轴视图的尺寸
- self.yBarView.width = self.yAxisViewWidth;
- self.yBarView.x = 0;
- self.yBarView.height = self.height;
- self.yBarView.y = 0;
-
- // 设置scrollView的尺寸
- self.scrollView.x = self.yBarView.width;
- self.scrollView.y = 0;
- self.scrollView.width = self.width - self.scrollView.x;
- self.scrollView.height = self.height;
-
- // 设置x轴视图的尺寸
- self.xBarView.x = 0;
- self.xBarView.y = 0;
- self.xBarView.height = self.scrollView.height;
- self.xBarView.width = self.xAxisTitleArray.count * (self.barGap + self.barWidth) + 50;
-
- self.scrollView.contentSize = self.xBarView.frame.size;
-
- // 给y轴视图传递参数
- self.yBarView.landspace = self.landspace;
- self.yBarView.xAxisTextGap = self.xAxisTextGap;
- self.yBarView.yAxisTextGap = self.yAxisTextGap;
- self.yBarView.textColor = self.textColor;
- self.yBarView.textFont = self.textFont;
- self.yBarView.percent = self.isPercent;
- self.yBarView.axisColor = self.axisColor;
- self.yBarView.numberOfYAxisElements = self.numberOfYAxisElements;
- self.yBarView.yAxisMaxValue = self.yAxisMaxValue;
- self.yBarView.backColor = self.backColor;
- self.yBarView.barWidth = self.barWidth;
- self.yBarView.legendTitle1 = self.legendTitle1;
- self.yBarView.legendTitle2 = self.legendTitle2;
- [self.yBarView draw];
-
- self.xBarView.landspace = self.landspace;
- self.xBarView.shotImage = self.shotImage;
- self.xBarView.xAxisTitleArray = self.xAxisTitleArray;
- self.xBarView.barGap = self.barGap;
- self.xBarView.xAxisTextGap = self.xAxisTextGap;
- self.xBarView.axisColor = self.axisColor;
- self.xBarView.numberOfYAxisElements = self.numberOfYAxisElements;
- self.xBarView.xValues = self.xValues;
- self.xBarView.xValues2 = self.xValues2;
- self.xBarView.xValues3 = self.xValues3;
- self.xBarView.xTitles = self.xTitles;
- self.xBarView.xTitles2 = self.xTitles2;
- self.xBarView.xTitles3 = self.xTitles3;
- self.xBarView.yAxisMaxValue = self.yAxisMaxValue;
- self.xBarView.showPointLabel = self.isShowPointLabel;
- self.xBarView.backColor = self.backColor;
- self.xBarView.textFont = self.textFont;
- self.xBarView.textColor = self.textColor;
- self.xBarView.percent = self.isPercent;
- self.xBarView.barUserInteractionEnabled = self.isBarUserInteractionEnabled;
- self.xBarView.barWidth = self.barWidth;
- self.xBarView.barColor = self.barColor;
- self.xBarView.barColor2 = self.barColor2;
- self.xBarView.barColor3 = self.barColor3;
- self.xBarView.barSelectedColor = self.barSelectedColor;
- self.xBarView.index = self.index;
- [self.xBarView draw];
-
- if (self.index < 0) {
-
- if (self.index * (self.barGap + self.barWidth) > self.scrollView.width * 0.5) {
- [self.scrollView setContentOffset:CGPointMake(self.index * (self.barGap + self.barWidth) - self.scrollView.width * 0.5, 0) animated:YES];
- }else{
-
- [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
- }
- }
-
-
-
- //初始值
- [self.xBarView selectedIndex:0];
-
-
- //else if (self.index * (self.barGap + self.barWidth) < self.scrollView.width &&
- // self.index * (self.barGap + self.barWidth) > self.scrollView.width * 0.5) {
- // [self.scrollView setContentOffset:CGPointMake(self.index * (self.barGap + self.barWidth) - self.scrollView.width * 0.5, 0) animated:NO];
- //}else
- }
- -(void)selectedIndex:(NSInteger)index{
-
- [self.xBarView selectedIndex:index];
-
- }
- - (void)commonInit {
-
- // 初始化某些属性值
- self.xAxisTextGap = 10;
- self.yAxisTextGap = 10;
- self.barGap = 1;
- self.axisColor = [UIColor colorWithHexString:@"67707c"];
- self.textColor = [UIColor colorWithHexString:@"9aafc1"];
- self.textFont = [UIFont systemFontOfSize:9];
- self.numberOfYAxisElements = 5;
- self.percent = NO;
- self.showPointLabel = YES;
- self.backColor = [UIColor colorWithHexString:@"3e4a59"];
- self.barUserInteractionEnabled = YES;
- self.barColor = [UIColor colorWithRed:0.99 green: 0.55 blue:0.14 alpha:1];
- self.barColor2 = [UIColor colorWithRed:0.99 green: 0.55 blue:0.14 alpha:0.5];
- self.barColor3 = [UIColor colorWithRed:0.99 green: 0.55 blue:0.14 alpha:0.1];
- self.barSelectedColor = [UIColor colorWithHexString:@"fdb302"];
- self.barWidth = 57;
- self.index = -1;
- self.landspace = NO;
- self.shotImage = NO;
- self.legendTitle1 = @"图例1";
- self.legendTitle2 = @"图例2";
- // 添加x轴与y轴视图
- DVYBarView *yBarView = [[DVYBarView alloc] init];
-
- [self addSubview:yBarView];
-
- UIScrollView *scrollView = [[UIScrollView alloc] init];
-
- DVXBarView *xBarView = [[DVXBarView alloc] init];
-
- [scrollView addSubview:xBarView];
-
- [self addSubview:scrollView];
-
- self.scrollView = scrollView;
- self.xBarView = xBarView;
- self.xBarView.delegate = self;
- self.yBarView = yBarView;
-
- self.scrollView.showsHorizontalScrollIndicator = NO;
- self.scrollView.bounces = NO;
-
- }
- -(instancetype)init{
- if (self == [super init]) {
- [self commonInit];
- }
- return self;
- }
- - (instancetype)initWithFrame:(CGRect)frame {
-
- if (self == [super initWithFrame:frame]) {
-
- [self commonInit];
-
- }
- return self;
- }
- - (void)awakeFromNib {
-
- [self commonInit];
- }
- + (instancetype)barChartView {
- return [[self alloc] init];
- }
- - (void)xBarView:(DVXBarView *)xBarView didClickButtonAtIndex:(NSInteger)index {
- if ([self.delegate respondsToSelector:@selector(barChartView:didSelectedBarAtIndex:)]) {
-
- [self.delegate barChartView:self didSelectedBarAtIndex:index];
- }
- }
- - (void)addPlot:(NSArray *)plot {
- //先清空数组里边的数据
- [self.xValues removeAllObjects];
- if (plot == nil) return;
- if (plot.count == 0) return;
-
-
- [self.xValues addObjectsFromArray:plot];
- // NSLog(@"%s,%@",__func__,self.xValues);
-
- }
- - (void)addPlot2:(NSArray *)plot {
- [self.xValues2 removeAllObjects];
- if (plot == nil) return;
- if (plot.count == 0) return;
-
- [self.xValues2 addObjectsFromArray:plot];
- // NSLog(@"%s,%@",__func__,self.xValues2);
-
- }
- - (void)addPlot3:(NSArray *)plot {
- [self.xValues3 removeAllObjects];
- if (plot == nil) return;
- if (plot.count == 0) return;
- [self.xValues3 addObjectsFromArray:plot];
- // NSLog(@"%s,%@",__func__,self.xValues2);
- }
- - (void)removePlot{
- [self.xValues removeAllObjects];
- }
- - (void)removeAllPlot {
- [self.xValues removeAllObjects];
- [self.xValues2 removeAllObjects];
- [self.xValues3 removeAllObjects];
-
- }
- @end
|