亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

iOS繪制收益柱狀圖

2019-11-14 19:11:24
字體:
來源:轉載
供稿:網友

 項目需求,參考了其他繪圖demo,自己繪制出來了,不過代碼改得有點亂,添加了很多變量,時間關系沒用太合適的命名,邏輯處理也沒進行優化。

看看效果圖(虛線區域都是畫的,其他區域添加的都是控件),附上源碼


 

 

#import <UIKit/UIKit.h>

 

typedef enum : NSUInteger {

    CSYieldTypeWeek = 0,    //周收益

    CSYieldTypeMonth = 1,   //月收益

    CSYieldTypeYear = 2,    //年收益

} CSYieldType;  //收益類型

 

typedef enum : NSUInteger {

    CSTimePointJanuary  = 0,         //一月份

    CSTimePointFebruary,

    CSTimePointMarch,

    CSTimePointAPRil,

    CSTimePointMay,             //.

    CSTimePointJun,             //.

    CSTimePointJuly,            //.

    CSTimePointAugust,

    CSTimePointSeptember,

    CSTimePointOctober,

    CSTimePointNovember,

    CSTimePointDecember,        //十二月份

    CSTimePointThisWeek,    //本周

    CSTimePointLastWeek,        //上周

    

} CSTimePoint;

 

@interface CSYieldChartView : UIView

@property (nonatomic,assign)CSYieldType yieldType; //收益類型

@property (nonatomic,assign)CSTimePoint timePoint;  //時間點

@property (nonatomic,strong) NSArray *yields; //收益數組

@property (nonatomic,strong) NSArray *dayPoints;   //時間點數組

 

//刷新圖表

- (void)refreshChartWithYields:(NSArray *)yields dayPoints:(NSArray *)dayPoints yieldType:(CSYieldType)yieldType timePoint:(CSTimePoint)timePoint;

 

@end

 

 

#define yieldLineSpace 4

 

#define lineLeftMargin 40

#define lineRightMargin 10

#define lineTopMargin 10

#define lineBottomMargin 30

 

#define lineWidth 0.5f

 

#define itemLeftMargin 16

#define itemRightMargin 16

#define itemSpace 6

#define itemWidthRatio (5/12.f)

 

#define positiveItemHexColor 0xe83846   //正收益條顏色值

#define negativeItemHexColor 0x17c7ba   //負收益條顏色值

 

#import "CSYieldChartView.h"

#import "UIColor+Addition.h"

@implementation CSYieldChartView

 

-(void)refreshChartWithYields:(NSArray *)yields dayPoints:(NSArray *)dayPoints yieldType:(CSYieldType)yieldType timePoint:(CSTimePoint)timePoint

{

    self.yields = yields;

    self.dayPoints = dayPoints;

    self.yieldType = yieldType;

    self.timePoint = timePoint;

    

    [self setNeedsDisplay];

}

- (void)drawRect:(CGRect)rect

{

    

    //聲明最大的收益

    float maxYield = 0;

    

    //獲取最大的收益絕對值

    for (NSNumber *number in _yields) {

        if (maxYield < fabs([number floatValue])) {

            maxYield = fabs([number floatValue]);

        }

    }

    

    //若最大收益少于10,則將最大值設為10

    if (maxYield < 10.0) {

        maxYield = 10.0;

    }

    

    //分幾段

    NSInteger sectionNum = 2;

    

    //平均每段的收益

    CGFloat sectionYield = maxYield / 2;

    

    UIColor *currentColor = [UIColor lightGrayColor];

    

    CGContextRef contextRef = UIGraphicsGetCurrentContext();

    CGContextBeginPath(contextRef);

    CGContextSetLineWidth(contextRef, lineWidth); //設置線粗

    

    CGContextSetStrokeColorWithColor(contextRef, currentColor.CGColor);   //設置畫筆顏色

    

    //中線的y坐標

    CGFloat centerLineY = (self.frame.size.height - lineTopMargin - lineBottomMargin)/2.f + lineTopMargin;

    

    //設置段落風格

    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];

    paragraph.alignment = NSTextAlignmentRight;

    //設置字體顏色

    UIColor *textColor = [UIColor lightGrayColor];

    //設置字體大小

    UIFont *textFont = [UIFont systemFontOfSize:10];

    

    //0收益中線

    CGContextMoveToPoint(contextRef, lineLeftMargin, centerLineY);

    CGContextAddLineToPoint(contextRef, self.frame.size.width - lineRightMargin, centerLineY);

    CGContextStrokePath(contextRef);

    

    //0收益率

    [@"0%" drawInRect:CGRectMake(0, centerLineY - 7, lineLeftMargin - yieldLineSpace, lineLeftMargin) withAttributes:@{NSParagraphStyleAttributeName:paragraph,NSForegroundColorAttributeName:textColor,NSFontAttributeName:textFont}];

    

    //線與線的垂直距離

    CGFloat lineSpace = (self.frame.size.height - lineTopMargin - lineBottomMargin) / 4.f;

    

    for (int i = 0; i < sectionNum; i++) {

        CGFloat topLineY = centerLineY - lineSpace * (i + 1);

        CGFloat bottomLineY = centerLineY + lineSpace * (i + 1);

        

        //虛線 線寬與空格寬

        CGFloat dashes[] = {2,1};

        CGContextSetLineDash(contextRef, 0.0, dashes, 2);

        

        CGContextSetStrokeColorWithColor(contextRef, currentColor.CGColor);   //設置畫筆顏色

 

        //中線以上

        CGContextMoveToPoint(contextRef, lineLeftMargin, topLineY);

        CGContextAddLineToPoint(contextRef, self.frame.size.width - lineRightMargin, topLineY);

        CGContextStrokePath(contextRef);

        

        //收益率

        [[NSString stringWithFormat:@"%.f%%",sectionYield * (i + 1)] drawInRect:CGRectMake(0, topLineY - 7, lineLeftMargin - yieldLineSpace, lineLeftMargin) withAttributes:@{NSParagraphStyleAttributeName:paragraph,NSForegroundColorAttributeName:textColor,NSFontAttributeName:textFont}];

 

        CGContextSetStrokeColorWithColor(contextRef, currentColor.CGColor);   //設置畫筆顏色

        

        //中線以下

        CGContextMoveToPoint(contextRef, lineLeftMargin, bottomLineY);

        CGContextAddLineToPoint(contextRef, self.frame.size.width - lineRightMargin, bottomLineY);

        CGContextStrokePath(contextRef);

        

        //負收益率

        [[NSString stringWithFormat:@"-%.f%%",sectionYield * (i + 1)] drawInRect:CGRectMake(0, bottomLineY - 7, lineLeftMargin - yieldLineSpace, lineLeftMargin) withAttributes:@{NSParagraphStyleAttributeName:paragraph,NSForegroundColorAttributeName:textColor,NSFontAttributeName:textFont}];

    }

    

    if (0 == _yields.count) {

        paragraph.alignment = NSTextAlignmentCenter;

        

        NSDictionary *attributes = @{NSParagraphStyleAttributeName:paragraph,NSForegroundColorAttributeName:textColor,NSFontAttributeName:[UIFont systemFontOfSize:40]};

        

        CGSize textSize = [@"暫無數據" sizeWithAttributes:attributes];

        

        [@"暫無數據" drawInRect:CGRectMake(lineLeftMargin + itemLeftMargin,centerLineY - textSize.height / 2.f,self.width - lineLeftMargin - itemLeftMargin - lineRightMargin - itemRightMargin,textSize.height) withAttributes:attributes];

        

        return;

    }

    

    

    NSString *lastDay = [_dayPoints lastObject];

    

    //最多有幾個柱(如果是月收益,且收益數小于22,按22算,否則按收益數多少算)

    NSInteger partNum = (_yields.count < 22 && _yieldType == CSYieldTypeMonth) ? 22 : _yields.count;

    

    paragraph.alignment = NSTextAlignmentLeft;

    

    //繪制柱狀圖

    CGFloat averageSpace = (self.frame.size.width - lineLeftMargin - itemLeftMargin - lineRightMargin - itemRightMargin) / (partNum - 1);

    

    //柱寬

    CGFloat itemWidth = averageSpace * itemWidthRatio;

    

    for (int i=0; i<partNum; i++) {

        

        if (i > _yields.count - 1//后面暫時還沒收益就不畫柱狀條

        {

            break;

        }

        

        if (i % 5 == 0) {

            //繪制日期

            NSString *dayStr = nil;

            if (self.yieldType == CSYieldTypeMonth && 0 == i) {

                dayStr = [NSString stringWithFormat:@"%@()",_dayPoints[0]];

            }

            else

            {

                dayStr = [NSString stringWithFormat:@"%@",_dayPoints[i]];

            }

            [dayStr drawInRect:CGRectMake(lineLeftMargin + itemLeftMargin - 5 + averageSpace * i, self.frame.size.height - (lineBottomMargin - 6), 50, 15)withAttributes:@{NSParagraphStyleAttributeName:paragraph,NSForegroundColorAttributeName:textColor,NSFontAttributeName:textFont}];

        }

        else if (i == _yields.count - 1)    //最后一個柱狀圖日期

        {

            [[NSString stringWithFormat:@"%@",lastDay]  drawInRect:CGRectMake(lineLeftMargin + itemLeftMargin - 5 + averageSpace * i, self.frame.size.height - (lineBottomMargin - 6), 50, 15)withAttributes:@{NSParagraphStyleAttributeName:paragraph,NSForegroundColorAttributeName:textColor,NSFontAttributeName:textFont}];

        }

        

        //繪制柱狀圖

        float yield = [_yields[i] floatValue];

        

        

        float radius = itemWidth/2.f;   //柱狀末尾的半圓半徑

        

        //虛線 線寬與空格寬 (之前設了虛線,現在把虛線的間隙變為0

        CGFloat dashes[] = {1,0};

        CGContextSetLineDash(contextRef, 0.0, dashes, 2);

        

        

        CGFloat itemX = lineLeftMargin + itemLeftMargin + averageSpace * i - itemWidth/2.f;

        CGFloat itemY = centerLineY - (lineSpace * sectionNum) * yield / maxYield;

        

        

        //是否要畫半圓(如果收益高度低于寬度,就不畫半圓)

        BOOL shouldDrawCircle = (fabs(itemY - centerLineY) > itemWidth/2.f) ? YES : NO;

        

        if (yield >= 0) {

            

            if (shouldDrawCircle) {

                itemY += radius;

            }

            

            //正收益顏色

            CGContextSetStrokeColorWithColor(contextRef, [UIColor colorWithHex:positiveItemHexColor].CGColor);

            CGContextSetFillColorWithColor(contextRef, [UIColor colorWithHex:positiveItemHexColor].CGColor);

        }

        else

        {

            

            if (shouldDrawCircle) {

                itemY -= radius;

            }

            

            //負收益顏色

            CGContextSetStrokeColorWithColor(contextRef, [UIColor colorWithHex:negativeItemHexColor].CGColor);

            CGContextSetFillColorWithColor(contextRef, [UIColor colorWithHex:negativeItemHexColor].CGColor);

        }

        

        

//        CGContextSetShadow(contextRef,CGSizeMake(2, 0) , 2);  //陰影效果

        

        CGMutablePathRef pathRef = CGPathCreateMutable();

        CGPathMoveToPoint(pathRef, NULL, itemX, centerLineY);

        CGPathAddLineToPoint(pathRef, NULL, itemX, itemY);

        CGPathAddLineToPoint(pathRef, NULL, itemX + itemWidth, itemY);

        CGPathAddLineToPoint(pathRef, NULL, itemX + itemWidth, centerLineY);

        CGPathCloseSubpath(pathRef);

        CGContextAddPath(contextRef, pathRef);

        CGContextFillPath(contextRef);

        CGContextAddPath(contextRef, pathRef);

        CGContextStrokePath(contextRef);

        

        

        if (shouldDrawCircle) {

            //畫線末圓角

            CGPathMoveToPoint(pathRef, NULL, itemX + itemWidth/2.f, itemY);

            if (yield >= 0) {

                CGPathAddArc(pathRef, NULL, itemX + itemWidth/2.f, itemY, itemWidth/2.f, M_PI, 0, NO);

            }

            else

            {

                CGPathAddArc(pathRef, NULL, itemX + itemWidth/2.f, itemY, itemWidth/2.f, 0, M_PI, NO);

            }

            CGPathCloseSubpath(pathRef);

            CGContextAddPath(contextRef, pathRef);

            CGContextFillPath(contextRef);

            CGContextAddPath(contextRef, pathRef);

            CGContextStrokePath(contextRef);

        }

    }

}

 @end


上一篇:jsonj解析網絡數據

下一篇:預處理命令

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
亚洲嫩模很污视频| 中文字幕亚洲欧美日韩在线不卡| 亚洲影影院av| 欧美午夜影院在线视频| 久久久久久久香蕉网| 亚洲国产美女久久久久| 国产有码在线一区二区视频| 国产成人激情小视频| 国产91精品视频在线观看| 亚洲欧美另类在线观看| 欧美激情高清视频| 国产成人一区二区三区| 久久91精品国产91久久跳| 午夜精品一区二区三区在线播放| 亚洲精品99久久久久中文字幕| 国产一区欧美二区三区| 亚洲成人av资源网| 国产精品扒开腿爽爽爽视频| 欧美性猛交xxxx乱大交| 欧美亚洲另类在线| 一本大道香蕉久在线播放29| 最近2019年日本中文免费字幕| 欧美俄罗斯性视频| 日韩在线观看精品| 国产欧美在线视频| 亚洲欧美国产高清va在线播| 国产专区精品视频| 国产91色在线免费| 亚洲系列中文字幕| 久久久91精品国产一区不卡| 91亚洲精品久久久久久久久久久久| 国产精品国产亚洲伊人久久| 热久久这里只有| 精品国偷自产在线| 欧美极品在线播放| 亚洲自拍小视频| 色综合色综合网色综合| 欧美国产精品日韩| 欧美在线精品免播放器视频| 最近中文字幕mv在线一区二区三区四区| 久久久久国产精品免费| 欧美精品中文字幕一区| 日韩欧美成人区| 亚洲国产精品热久久| 欧美在线免费视频| 亚洲国产99精品国自产| 成人美女av在线直播| 久久久伊人欧美| 亚洲欧洲午夜一线一品| 国产精品视频久久久久| 欧美成人精品xxx| 不卡av电影院| 97高清免费视频| 久久夜色精品国产亚洲aⅴ| 国产日韩欧美在线视频观看| 性色av一区二区三区免费| 国产成人在线精品| 久久久久国色av免费观看性色| 国产99久久精品一区二区 夜夜躁日日躁| 亚洲日本成人女熟在线观看| 国产精品高清在线| 亚洲国产精品va在线看黑人| 国产一区私人高清影院| 久久久午夜视频| 免费99精品国产自在在线| 欧美电影在线观看高清| 亚洲网站在线看| 奇米成人av国产一区二区三区| 欧美性生交xxxxxdddd| 亚洲欧美日韩精品久久奇米色影视| 在线国产精品视频| 中文字幕一区二区三区电影| 久久久国产精彩视频美女艺术照福利| 全亚洲最色的网站在线观看| 欧美香蕉大胸在线视频观看| 亚洲一区二区免费在线| 91精品视频免费| 国产成人av在线播放| 国产成人精品免高潮费视频| 97色在线观看| 欧美理论片在线观看| 91精品视频网站| 国产精品激情av电影在线观看| 成人免费激情视频| 亚洲精品国产精品国产自| 日韩成人激情影院| 欧美在线影院在线视频| 2018日韩中文字幕| 国产精品久久久久久久久男| 亚洲天堂av在线免费观看| 欧美一区三区三区高中清蜜桃| 啪一啪鲁一鲁2019在线视频| 亚洲最大av网| 欧美一级淫片播放口| 国内精品视频久久| 亚洲欧美激情一区| 久久久久久久久爱| 日韩一区二区三区xxxx| 国产精品久久久久77777| 国产福利视频一区二区| 国产欧美一区二区三区在线| 精品久久中文字幕| 欧美日韩一区二区三区| 久久久精品久久久| 欧美激情精品久久久| 国产精品美女av| 91精品视频免费| 国产精品网站大全| 国产成人欧美在线观看| 日韩在线视频免费观看高清中文| 亚洲国产一区二区三区四区| 国产精品免费久久久久影院| 国产精品久久77777| 国产精品美女www爽爽爽视频| 91精品国产沙发| 精品视频久久久久久| 91精品在线观| 成人亚洲激情网| 欧美成人免费观看| 国产日韩欧美电影在线观看| 国产一区二区三区毛片| 日韩中文字幕久久| 欧美大片欧美激情性色a∨久久| 欧美在线日韩在线| 亚洲欧美日本另类| 亚洲成av人片在线观看香蕉| 日韩综合视频在线观看| 成人久久一区二区三区| 精品福利樱桃av导航| 中文字幕精品www乱入免费视频| 日韩av手机在线观看| 国产精品久久久久久久av电影| 亚洲男人天堂2023| 欧美在线视频在线播放完整版免费观看| 欧美日韩999| 日韩在线观看你懂的| 亚洲视屏在线播放| 欧美成年人视频| 国产精品欧美一区二区| 欧美色图在线视频| 国产精品午夜一区二区欲梦| 中国日韩欧美久久久久久久久| 国产亚洲精品久久久久久| 中文字幕在线看视频国产欧美在线看完整| 欧美亚洲激情视频| 岛国av一区二区| 久久精品国产亚洲7777| 欧美性受xxxx白人性爽| 国产精品第七十二页| 国产综合香蕉五月婷在线| 欧美在线视频网站| 国产精品99久久99久久久二8| 欧美激情乱人伦| 国产一区二区三区在线视频| 国产精品精品一区二区三区午夜版| 久久久极品av| 亚洲人成免费电影| 日本亚洲欧美三级| 91国产一区在线| 91国语精品自产拍在线观看性色| 欧美国产亚洲精品久久久8v| 国产一区二区三区日韩欧美| 国产精品免费一区| 欧美激情奇米色|