麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

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

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成人免费 | 一级电影免费 | 一级毛片真人免费播放视频 | 成人在线免费视频观看 | 麻豆视频在线播放 | 黄色网欧美| 一级大黄毛片 | 国产1区2区在线观看 | qyl在线视频精品免费观看 | 国产91在线免费 | 神马久久蜜桃 | 精品黑人一区二区三区国语馆 | 国产免费专区 | 日韩av影片在线观看 | 怦然心动50免费完整版 | 欧美一级黄色影院 | 国产精品爱久久久久久久 | 久久久久九九九女人毛片 | 国产精品18久久久久久久久 | 把娇妻调教成暴露狂 | 欧美黄色免费视频 | 久久久久久久久久久av | 曰批全过程120分钟免费69 | 日本成人一区二区三区 | 免费视频a | 日韩电影一区二区三区 | 亚洲aⅴ在线观看 | 91精品国产一区二区在线观看 | 亚洲片在线观看 | 狠狠操天天射 | 久草视频福利在线观看 | 久久国产精品二区 | 亚洲特黄| 午夜视频在线观看免费视频 | 一本免费视频 | 91成人在线免费视频 | 欧美日韩亚洲国产 | 美女福利视频国产 |