| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608 |
- //
- // SalesAnalysisListNewChartViewController.m
- // IBOSS
- //
- // Created by Simon on 2019/6/10.
- // Copyright © 2019 elongtian. All rights reserved.
- //
- #import "SalesAnalysisListNewChartViewController.h"
- @interface SalesAnalysisListNewChartViewController (){
-
- UIScrollView * _scroll;
- NSMutableArray *chartArray;
- }
- @end
- @implementation SalesAnalysisListNewChartViewController
- #pragma mark - 公共函数
- /**
- 视图加载完成函数
- */
- - (void)viewDidLoad {
- [super viewDidLoad];
- [self initUI];
- }
- #pragma mark - 私有函数
- /**
- 初始化UI
- */
- -(void)initUI{
- UIView *separatorView = [UIView new];
- separatorView.tag = 11;
- separatorView.frame = CGRectMake(0, 0, Screen_Width, 10);
- separatorView.backgroundColor = LineBackgroundColor;
- [self.view addSubview:separatorView];
- //柱状图左间距
- _scroll = [UIScrollView new];
- _scroll.frame = CGRectMake(0, CGRectGetMaxY(separatorView.frame), SCREENWIDTH, self.view.frame.size.height-CGRectGetMaxY(separatorView.frame));
- [self.view addSubview:_scroll];
- self.view.backgroundColor = [UIColor whiteColor];
- UILabel *lblTitle = [[UILabel alloc]init];
- lblTitle.tag = 10;
- lblTitle.frame = CGRectMake((Screen_Width-160)/2, CGRectGetMaxY(separatorView.frame)+5, 160, 25);
- lblTitle.text = @"畅滞销排行";
- lblTitle.textAlignment = 1;
- lblTitle.textColor = LabelGrayTextColor;
- lblTitle.font = [UIFont systemFontOfSize:14];
- [_scroll addSubview:lblTitle];
- _bottomSeparatorView = [UIView new];
- _bottomSeparatorView.tag = 10;
- _bottomSeparatorView.hidden = YES;
- _bottomSeparatorView.frame = CGRectMake(0,CGRectGetMaxY(lblTitle.frame)+5, Screen_Width,2);
- _bottomSeparatorView.backgroundColor = LineBackgroundColor;
- [_scroll addSubview:_bottomSeparatorView];
- chartArray = [[NSMutableArray alloc] init];
- }
- /**
- 刷新数据
- */
- -(void)refreshData{
- if(_rankList.count > 10){
- NSRange range = NSMakeRange(0, 10);
- chartArray = [NSMutableArray arrayWithArray:[_rankList subarrayWithRange:range]];
- }
-
- // Boolean flag = [self isHaveNegativeValue];
- // if(flag){
- //
- // [self loadNegativeHistogram];
- // }
- //
- // else{
- [self loadPositiveHistogram];
- // }
- }
- /**
- 刷新正数柱状图
- */
- - (void) loadPositiveHistogram{
- for (UIView *subview in _scroll.subviews) {
- if(subview.tag != 10) {
- [subview removeFromSuperview];
- }
- }
- if (chartArray.count<=0) {
- return;
- }
- self.view.backgroundColor = [UIColor whiteColor];
- CGFloat xSpacing = 30;
- //柱状图高度
- CGFloat height = 40;
- //第一个柱状图上间距
- CGFloat fySpacing = 15;
- //柱状图间距
- CGFloat ySpacing = 10;
- //柱状图内文字左间距
- CGFloat xTextSpacing = 5;
- //柱状图最大长度
- CGFloat maxWidth = SCREENWIDTH - xSpacing - 10;
- _histogramView = [[UIView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(_bottomSeparatorView.frame),Screen_Width,[self getHistogramHeight:chartArray])];
- [_scroll addSubview:_histogramView];
- _histogramView.backgroundColor = [UIColor whiteColor];
- // 判断是按照销售数量、销售金额、销售次数中的哪种方式排序
- int sumNumber = 0;
- double sumAmount = 0.0;
- int sumTimes = 0;
- SalesAnalysisModel *model = [chartArray objectAtIndex:0];
- // for (SalesAnalysisModel *model in _rankList) {
- sumNumber+=[model.number intValue];
- sumAmount+=[model.salesAmount doubleValue];
- sumTimes+=[model.salesTimes intValue];
- // }
- double maxUnit = 0.0;
- if ([_sortType isEqualToString:@"1"]) {
- maxUnit = sumNumber;
- }else if ([_sortType isEqualToString:@"2"]){
- maxUnit = sumAmount;
- }else if ([_sortType isEqualToString:@"3"]){
- maxUnit = sumTimes;
- }
- //>=3不画轴线
- if(chartArray.count>=3){
- [self createAxis:maxWidth maxAmount:maxUnit];
- }
-
- //画水平柱状图
- for(int i = 0;i < chartArray.count;i++){
- SalesAnalysisModel *analysisModel = [chartArray objectAtIndex:i];
- //销售数量
- int number = [analysisModel.number intValue];
- //销售金额
- double salesAmount = [analysisModel.salesAmount doubleValue];
- //销售次数
- int salesTimes = [analysisModel.salesTimes intValue];
- //商品品牌
- NSString *commodityBrand = analysisModel.commodityBrand==nil?@"":analysisModel.commodityBrand;
- //商品名称
- NSString *commodityName = analysisModel.commodityName==nil?@"":analysisModel.commodityName;
-
- //排序方式单位
- NSString *goodsAmountStr = @"";
- double loopUnit = 0.0;
- if ([_sortType isEqualToString:@"1"]) {
- loopUnit = number;
- goodsAmountStr = [NSString stringWithFormat:@"%d", number];
- }else if ([_sortType isEqualToString:@"2"]){
- loopUnit = salesAmount;
- goodsAmountStr = [NSString stringWithFormat:@"¥%.2f", loopUnit];
- }else if ([_sortType isEqualToString:@"3"]){
- loopUnit = salesTimes;
- goodsAmountStr = [NSString stringWithFormat:@"%d", salesTimes];
- }
-
- NSString *objectName = [NSString stringWithFormat:@"%@%@",commodityBrand,commodityName];
-
- CGFloat width;
- //计算长度
- if(loopUnit < maxUnit){
- width = maxWidth * (fabs(loopUnit)/maxUnit);
- if(width < 1){
- width = 1;
- }
- }
- else{
- width = maxWidth;
- }
- [self layerToRight:_histogramView withFrame:CGRectMake(xSpacing, fySpacing*(i+1)+height*i,width, height)withColor:[UIColor colorWithRed:96.0/255.0 green:159.0/255.0 blue:233.0/255.0 alpha:1]];
- [self.view addSubview:_histogramView];
- UILabel *lblOrderSort = [[UILabel alloc] init];
- NSString *orderSortStr = analysisModel.ranking;
- CGRect orderSortFrame = [orderSortStr textRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) attributes:@{NSFontAttributeName:kTextFont}];
- orderSortFrame.origin.x = 10;
- orderSortFrame.origin.y = (height - orderSortFrame.size.height)/2 + fySpacing * (i+1) + height * i;
- lblOrderSort.frame = orderSortFrame;
- lblOrderSort.font = kTextFont;;
- lblOrderSort.text = orderSortStr;
- [_histogramView addSubview:lblOrderSort];
- UILabel *lblDepartmentName = [[UILabel alloc] init];
- NSString *departmentNameStr = objectName;
- CGRect departmentNameFrame = [departmentNameStr textRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) attributes:@{NSFontAttributeName:kTextFont}];
- departmentNameFrame.origin.x = xTextSpacing + xSpacing;
- departmentNameFrame.origin.y = 5 + fySpacing*(i+1)+height*i;
- lblDepartmentName.textAlignment = NSTextAlignmentRight;
- lblDepartmentName.frame = departmentNameFrame;
- lblDepartmentName.font = kTextFont;
- lblDepartmentName.text = departmentNameStr;
- [_histogramView addSubview:lblDepartmentName];
- UILabel *lblGoodsAmount = [[UILabel alloc] init];
- CGRect goodsAmountFrame = [goodsAmountStr textRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) attributes:@{NSFontAttributeName:kTextFont}];
- goodsAmountFrame.origin.x= xTextSpacing + xSpacing;
- goodsAmountFrame.origin.y=CGRectGetMaxY( lblDepartmentName.frame);
- lblGoodsAmount.textAlignment = NSTextAlignmentRight;
- lblGoodsAmount.frame = goodsAmountFrame;
- lblGoodsAmount.font = kTextFont;
- lblGoodsAmount.text = goodsAmountStr;
- [_histogramView addSubview:lblGoodsAmount];
- }
- _scroll.contentSize = CGSizeMake(self.view.frame.size.width, CGRectGetMaxY(_histogramView.frame)+10 + rectStatusHeight +rectNavHeight + 150);
- [_scroll addSubview:_histogramView];
- }
- /**
- 创建负数的轴线
- @param width 最大长度
- @param maxAmount 最大金额
- */
- - (void) createNegativeAxis:(CGFloat) width maxAmount:(CGFloat)maxAmount{
- // 线的路径
- UIBezierPath *leveLinePath = [UIBezierPath bezierPath];
- // 起点
- [leveLinePath moveToPoint:CGPointMake((_histogramView.frame.size.width - (2 * width))/2, 0)];
- // 其他点
- [leveLinePath addLineToPoint:CGPointMake(2 * width + 10, 0)];
- CAShapeLayer *leveLineLayer = [CAShapeLayer layer];
- leveLineLayer.lineWidth = 1;
- leveLineLayer.strokeColor = LineBackgroundColor.CGColor;
- leveLineLayer.path = leveLinePath.CGPath;
- leveLineLayer.fillColor = nil; // 默认为blackColor
- [_histogramView.layer addSublayer:leveLineLayer];
- NSString *str = [NSString stringWithFormat:@"%d",(int)maxAmount];
- int m = ceil([str integerValue]/pow(10, str.length-1));
- m = m * pow(10, str.length-1);
- //右侧垂直轴线
- for (int i = 0; i <= 3; i++) {
- if(i != 0){
- CGFloat c = (width/3)*i+width+10;
- // if(i != 0){
- // UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(c-20, _histogramView.frame.origin.y-17, 200, 17)];
- // lb.textAlignment = NSTextAlignmentCenter;
- // lb.center = CGPointMake(c-10, _histogramView.frame.origin.y-10);
- // lb.text = [NSString stringWithFormat:@"%d",(m/5)*i];
- // lb.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1];
- // lb.font = [UIFont systemFontOfSize:9];
- // [_scroll addSubview:lb];
- // }
- UIBezierPath *linePath = [UIBezierPath bezierPath];
- [linePath moveToPoint:CGPointMake(c, 0)];
- [linePath addLineToPoint:CGPointMake(c, _histogramView.frame.size.height)];
- CAShapeLayer *lineLayer = [CAShapeLayer layer];
- lineLayer.lineWidth = 1;
- lineLayer.strokeColor = LineBackgroundColor.CGColor;
- lineLayer.path = linePath.CGPath;
- lineLayer.fillColor = nil; // 默认为blackColor
- [_histogramView.layer addSublayer:lineLayer];
- }
- }
- //左侧垂直轴线
- for (int i = 0; i <= 3; i++) {
- //计算等分间距
- CGFloat c = width + 10 -(width/3)*i;
- if(i != 0){
- UIBezierPath *linePath = [UIBezierPath bezierPath];
- [linePath moveToPoint:CGPointMake(c, 0)];
- [linePath addLineToPoint:CGPointMake(c, _histogramView.frame.size.height)];
- CAShapeLayer *lineLayer = [CAShapeLayer layer];
- lineLayer.lineWidth = 1;
- lineLayer.strokeColor = LineBackgroundColor.CGColor;
- lineLayer.path = linePath.CGPath;
- lineLayer.fillColor = nil; // 默认为blackColor
- [_histogramView.layer addSublayer:lineLayer];
- }
- }
- }
- /**
- 创建正数的轴线
- @param width <#width description#>
- @param maxAmount <#maxAmount description#>
- */
- - (void) createAxis:(CGFloat) width maxAmount:(CGFloat)maxAmount{
- // 线的路径
- UIBezierPath *linePath = [UIBezierPath bezierPath];
- // 起点
- [linePath moveToPoint:CGPointMake(width+30, 0)];
- // 其他点
- [linePath addLineToPoint:CGPointMake(30, 0)];
- [linePath addLineToPoint:CGPointMake(30,_histogramView.frame.size.height)];
- CAShapeLayer *lineLayer = [CAShapeLayer layer];
- lineLayer.lineWidth = 1;
- lineLayer.strokeColor = LineBackgroundColor.CGColor;
- lineLayer.path = linePath.CGPath;
- lineLayer.fillColor = nil; // 默认为blackColor
- [_histogramView.layer addSublayer:lineLayer];
- NSString *str = [NSString stringWithFormat:@"%d",(int)maxAmount];
- int m = ceil([str integerValue]/pow(10, str.length-1));
- m = m * pow(10, str.length-1);
- //垂直五条轴线
- for (int i = 0; i <= 5; i++) {
- CGFloat c = (width/5)*i+30;
- // if(i != 0){
- // UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(c-20, _histogramView.frame.origin.y-17, 200, 17)];
- // lb.textAlignment = NSTextAlignmentCenter;
- // lb.center = CGPointMake(c-10, _histogramView.frame.origin.y-10);
- // lb.text = [NSString stringWithFormat:@"%d",(m/5)*i];
- // lb.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1];
- // lb.font = [UIFont systemFontOfSize:9];
- // [_scroll addSubview:lb];
- // }
- UIBezierPath *linePath = [UIBezierPath bezierPath];
- [linePath moveToPoint:CGPointMake(c, 0)];
- [linePath addLineToPoint:CGPointMake(c, _histogramView.frame.size.height)];
- CAShapeLayer *lineLayer = [CAShapeLayer layer];
- lineLayer.lineWidth = 1;
- lineLayer.strokeColor = LineBackgroundColor.CGColor;
- lineLayer.path = linePath.CGPath;
- lineLayer.fillColor = nil; // 默认为blackColor
- [_histogramView.layer addSublayer:lineLayer];
- }
- }
- /**
- 刷新负数柱状图
- */
- - (void) loadNegativeHistogram{
- for (UIView *subview in _scroll.subviews) {
- if(subview.tag != 10) {
- [subview removeFromSuperview];
- }
- }
- //柱状图左间距
- CGFloat xSpacing = 15;
- //柱状图高度
- CGFloat height = 40;
- //第一个柱状图上间距
- CGFloat fySpacing = 15;
- //柱状图间距
- CGFloat ySpacing = 10;
- //柱状图内文字左间距
- CGFloat xTextSpacing = 5;
- //柱状图最大长度
- CGFloat maxWidth = SCREENWIDTH/2 - 10;
- _histogramView = [[UIView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(_bottomSeparatorView.frame),Screen_Width,[self getHistogramHeight:_rankList])];
- [_scroll addSubview:_histogramView];
- _histogramView.backgroundColor = [UIColor whiteColor];
- SalesAnalysisModel *maxBackModel = [chartArray objectAtIndex:0];
- SalesAnalysisModel *minBackModel = [chartArray objectAtIndex:chartArray.count - 1];
- //获得最大值绝对值
- double maxUnit = 0.0;
- double firstAmount = 0.0;
- // maxBackFrame.achievementRankModel.goodsAmount;
- double lastAmount = 0.0;
- // minBackFrame.achievementRankModel.goodsAmount;
- // 判断是按照销售数量、销售金额、销售次数中的哪种方式排序
- // int sumNumber = 0;
- // double sumAmount = 0.0;
- // int sumTimes = 0;
- // for (SalesAnalysisModel *model in _rankList) {
- // sumNumber+=[model.number intValue];
- // sumAmount+=[model.salesAmount doubleValue];
- // sumTimes+=[model.salesTimes intValue];
- // }
- if ([_sortType isEqualToString:@"1"]) {
- maxUnit = [maxBackModel.number floatValue];
- firstAmount = [maxBackModel.number floatValue];
- lastAmount = [minBackModel.number floatValue];
- }else if ([_sortType isEqualToString:@"2"]){
- maxUnit = [maxBackModel.salesAmount floatValue];
- firstAmount = [maxBackModel.salesAmount floatValue];
- lastAmount = [minBackModel.salesAmount floatValue];
- }else if ([_sortType isEqualToString:@"3"]){
- maxUnit = [maxBackModel.salesTimes floatValue];
- firstAmount = [maxBackModel.salesTimes floatValue];
- lastAmount = [minBackModel.salesTimes floatValue];
- }
- if(fabs(firstAmount)>fabs(lastAmount)){
- maxUnit = firstAmount;
- }else{
- maxUnit = fabs(lastAmount);
- }
- if(_rankList.count>=3){
- [self createNegativeAxis:maxWidth maxAmount:maxUnit];
- }
- for(int i = 0;i < chartArray.count;i++){
- SalesAnalysisModel *maxAnalysisModel = [chartArray objectAtIndex:i];
- //销售数量
- int number = [maxAnalysisModel.number intValue];
- //销售金额
- double salesAmount = [maxAnalysisModel.salesAmount doubleValue];
- //销售次数
- int salesTimes = [maxAnalysisModel.salesTimes intValue];
- //商品品牌
- NSString *commodityBrand = maxAnalysisModel.commodityBrand==nil?@"":maxAnalysisModel.commodityBrand;
- //商品名称
- NSString *commodityName = maxAnalysisModel.commodityName==nil?@"":maxAnalysisModel.commodityName;
-
- //排序方式单位
- NSString *goodsAmountStr = @"";
- double goodsAmount = 0.0;
-
- if ([_sortType isEqualToString:@"1"]) {
- goodsAmount = number;
- goodsAmountStr = [NSString stringWithFormat:@"%d", number];
- }else if ([_sortType isEqualToString:@"2"]){
- goodsAmount = salesAmount;
- goodsAmountStr = [NSString stringWithFormat:@"%.2f", salesAmount];
- }else if ([_sortType isEqualToString:@"3"]){
- goodsAmount = salesTimes;
- goodsAmountStr = [NSString stringWithFormat:@"%d", salesTimes];
- }
- NSString *objectName = [NSString stringWithFormat:@"%@%@",commodityBrand,commodityName];
- CGFloat width;
- if(goodsAmount < maxUnit){
- width = maxWidth * (fabs(goodsAmount)/maxUnit);
- if(width < 1){
- width = 1;
- }
- }
- else{
- width = maxWidth;
- }
- UILabel *lblGoodsAmount = [[UILabel alloc] init];
- CGRect goodsAmountFrame = [goodsAmountStr textRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) attributes:@{NSFontAttributeName:kTextFont}];
- UILabel *lblOrderSort = [[UILabel alloc] init];
- NSString *orderSortStr = maxAnalysisModel.ranking;
- CGRect orderSortFrame = [orderSortStr textRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) attributes:@{NSFontAttributeName:kTextFont}];
- UILabel *lblDepartmentName = [[UILabel alloc] init];
- NSString *objectNameStr = objectName;
- CGRect objectNameFrame = [objectNameStr textRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) attributes:@{NSFontAttributeName:kTextFont}];
- //画右侧柱状图
- if(goodsAmount >=0){
- [self layerToRight:_histogramView withFrame:CGRectMake(Screen_Width/2, fySpacing*(i+1)+height*i,width, height)withColor:[UIColor colorWithRed:96.0/255.0 green:159.0/255.0 blue:233.0/255.0 alpha:1]];
- objectNameFrame.origin.x = xTextSpacing+Screen_Width/2;
- objectNameFrame.origin.y = 5 + fySpacing*(i+1)+height*i;
- goodsAmountFrame.origin.x=xTextSpacing+Screen_Width/2;
- goodsAmountFrame.origin.y=CGRectGetMaxY(objectNameFrame);
- orderSortFrame.origin.x = Screen_Width/2-xSpacing;
- orderSortFrame.origin.y = (height - orderSortFrame.size.height)/2 + fySpacing*(i+1)+height*i;
- //跳转按钮frame
- // detailBtnFrame = CGRectMake(SCREENWIDTH/2, fySpacing*(i+1)+height*i, SCREENWIDTH/2, height);
- }
- //画左侧柱状图
- else if(goodsAmount < 0){
- [self layerToLeft:_histogramView withFrame:CGRectMake(Screen_Width/2-width, fySpacing*(i+1)+height*i,width, height)withColor:[UIColor colorWithRed:96.0/255.0 green:159.0/255.0 blue:233.0/255.0 alpha:1]];
- objectNameFrame.origin.x=Screen_Width/2- xTextSpacing-CGRectGetWidth(objectNameFrame);
- objectNameFrame.origin.y = 5 + fySpacing*(i+1)+height*i;
- goodsAmountFrame.origin.x=Screen_Width/2-xTextSpacing-CGRectGetWidth(goodsAmountFrame);
- goodsAmountFrame.origin.y=CGRectGetMaxY(objectNameFrame);
- orderSortFrame.origin.x =Screen_Width/2+xTextSpacing;
- orderSortFrame.origin.y = (height - orderSortFrame.size.height)/2 + fySpacing*(i+1)+height*i;
- //跳转按钮frame
- // detailBtnFrame = CGRectMake(0, fySpacing*(i+1)+height*i, SCREENWIDTH/2, height);
- }
- [self.view addSubview:_histogramView];
- UIView *yAxis=[[UIView alloc]init];
- yAxis.frame=CGRectMake(Screen_Width/2, 0, 2.5, [self getHistogramHeight:_rankList]);
- yAxis.backgroundColor=LineBackgroundColor;
- [_histogramView addSubview:yAxis];
- lblDepartmentName.frame = objectNameFrame;
- lblDepartmentName.font = kTextFont;
- lblDepartmentName.text = objectNameStr;
- [_histogramView addSubview:lblDepartmentName];
- lblOrderSort.frame = orderSortFrame;
- lblOrderSort.font = kTextFont;
- lblOrderSort.text = orderSortStr;
- [_histogramView addSubview:lblOrderSort];
- lblGoodsAmount.textAlignment = NSTextAlignmentRight;
- lblGoodsAmount.frame = goodsAmountFrame;
- lblGoodsAmount.font = kTextFont;
- lblGoodsAmount.text = goodsAmountStr;
- [_histogramView addSubview:lblGoodsAmount];
- }
- _scroll.contentSize = CGSizeMake(self.view.frame.size.width, CGRectGetMaxY(_histogramView.frame)+10+rectStatusHeight+rectNavHeight + 150);
- [_scroll addSubview:_histogramView];
- }
- /**
- 判断是否有负数
- @return <#return value description#>
- */
- -(Boolean) isHaveNegativeValue{
- Boolean flag=NO;
- for(int i = 0;i < _rankList.count;i++){
- SalesAnalysisModel *model = [chartArray objectAtIndex:i];
- double loopUnit = 0.0;
- if ([_sortType isEqualToString:@"1"]) {
- loopUnit = [model.number doubleValue];
- }else if ([_sortType isEqualToString:@"2"]){
- loopUnit = [model.salesAmount doubleValue];
- }else if ([_sortType isEqualToString:@"3"]){
- loopUnit = [model.salesTimes doubleValue];
- }
- if(loopUnit < 0){
- flag = true;
- break;
- }
- }
- return flag;
- }
- /**
- 计算柱状图高度
- @param dataList <#dataList description#>
- @return <#return value description#>
- */
- -(CGFloat)getHistogramHeight:(NSArray*)dataList{
- NSInteger row = dataList.count;
- return (row * 40 + (row * 15));
- }
- /**
- 绘制向右的柱状图
- @param subview
- @param frame
- */
- - (void)layerToRight:(UIView *) subview withFrame:(CGRect) frame withColor:(UIColor *) color
- {
- CGFloat x = frame.origin.x;
- CGFloat y = frame.origin.y;
- CGFloat height = frame.size.height;
- CGFloat width = frame.size.width;
- CAGradientLayer *itemLayer = [CAGradientLayer layer];
- itemLayer.frame = frame;
- itemLayer.colors = [self getColorArr];;
- itemLayer.locations = @[@0,@0.5];
- itemLayer.startPoint = CGPointMake(0, 0);
- itemLayer.endPoint = CGPointMake(0, 1.0);
- [subview.layer addSublayer:itemLayer];
- CABasicAnimation *aniBounds = [CABasicAnimation animationWithKeyPath:@"bounds"];
- aniBounds.fromValue = [NSValue valueWithCGRect:CGRectMake(x, y, 0, height)];
- aniBounds.toValue = [NSValue valueWithCGRect:CGRectMake(x, y, CGRectGetWidth(frame), CGRectGetHeight(frame))];
- CABasicAnimation *aniPosition = [CABasicAnimation animationWithKeyPath:@"position"];
- aniPosition.fromValue = [NSValue valueWithCGPoint:CGPointMake(x, frame.size.height/2 + y)];
- aniPosition.toValue = [NSValue valueWithCGPoint:CGPointMake(x + (CGRectGetMaxX(frame)-x)/2,y + (CGRectGetMaxY(frame)-y)/2)];
- CAAnimationGroup *anis = [CAAnimationGroup animation];
- anis.animations = @[aniBounds,aniPosition];
- anis.duration = 1;
- anis.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
- anis.removedOnCompletion = NO;
- anis.fillMode=kCAFillModeForwards;
- [itemLayer addAnimation:anis forKey:nil];
- }
- /**
- 绘制向左的柱状图
- @param subview
- @param frame
- */
- - (void)layerToLeft:(UIView *) subview withFrame:(CGRect) frame withColor:(UIColor *) color
- {
- CGFloat x = frame.origin.x;
- CGFloat y = frame.origin.y;
- CGFloat height = frame.size.height;
- CGFloat width = frame.size.width;
- CAGradientLayer *itemLayer = [CAGradientLayer layer];
- itemLayer.frame = frame;
- itemLayer.colors = [self getColorArr];
- itemLayer.locations = @[@0,@0.5];
- itemLayer.startPoint = CGPointMake(0, 0);
- itemLayer.endPoint = CGPointMake(0, 1.0);
- [subview.layer addSublayer:itemLayer];
- CABasicAnimation *aniBounds = [CABasicAnimation animationWithKeyPath:@"bounds"];
- aniBounds.fromValue = [NSValue valueWithCGRect:CGRectMake(x, y, 0, height)];
- aniBounds.toValue = [NSValue valueWithCGRect:CGRectMake(x, y, CGRectGetWidth(frame), CGRectGetHeight(frame))];
- CABasicAnimation *aniPosition = [CABasicAnimation animationWithKeyPath:@"position"];
- aniPosition.toValue = [NSValue valueWithCGPoint:CGPointMake(x+ (CGRectGetMaxX(frame)-x)/2, frame.size.height/2 + y)];
- aniPosition.fromValue = [NSValue valueWithCGPoint:CGPointMake(x+width,y + (CGRectGetMaxY(frame)-y)/2)];
- CAAnimationGroup *anis = [CAAnimationGroup animation];
- anis.animations = @[aniBounds,aniPosition];
- anis.duration = 1;
- anis.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
- anis.removedOnCompletion = NO;
- anis.fillMode=kCAFillModeForwards;
- [itemLayer addAnimation:anis forKey:nil];
- }
- /**
- 渐变色
- @return <#return value description#>
- */
- -(NSMutableArray *) getColorArr{
- NSMutableArray *arr = [NSMutableArray array];
- [arr addObject:(__bridge id)[UIColor colorWithRed:236/255.0 green:134/255.0 blue:40/255.0 alpha:1].CGColor];
- [arr addObject:(__bridge id)[UIColor colorWithRed:236/255.0 green:134/255.0 blue:40/255.0 alpha:0.4].CGColor];
- [arr addObject:(__bridge id)[UIColor colorWithRed:236/255.0 green:134/255.0 blue:40/255.0 alpha:1].CGColor];
- return arr;
- }
- @end
|