国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

iOS繪制收益柱狀圖

2019-11-14 19:11:24
字體:
供稿:網(wǎng)友

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

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


 

 

#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; //收益數(shù)組

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

 

//刷新圖表

- (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   //負(fù)收益條顏色值

 

#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,則將最大值設(shè)為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); //設(shè)置線粗

    

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

    

    //中線的y坐標(biāo)

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

    

    //設(shè)置段落風(fēng)格

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

    paragraph.alignment = NSTextAlignmentRight;

    //設(shè)置字體顏色

    UIColor *textColor = [UIColor lightGrayColor];

    //設(shè)置字體大小

    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);   //設(shè)置畫筆顏色

 

        //中線以上

        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);   //設(shè)置畫筆顏色

        

        //中線以下

        CGContextMoveToPoint(contextRef, lineLeftMargin, bottomLineY);

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

        CGContextStrokePath(contextRef);

        

        //負(fù)收益率

        [[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 = [@"暫無數(shù)據(jù)" sizeWithAttributes:attributes];

        

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

        

        return;

    }

    

    

    NSString *lastDay = [_dayPoints lastObject];

    

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

    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;   //柱狀末尾的半圓半徑

        

        //虛線 線寬與空格寬 (之前設(shè)了虛線,現(xiàn)在把虛線的間隙變?yōu)?span id="iwvjtn8m0" class="s11">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;

            }

            

            //負(fù)收益顏色

            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


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 定远县| 满洲里市| 云安县| 手游| 南召县| 蒲城县| 洞头县| 方山县| 吉隆县| 庄河市| 翁源县| 苗栗市| 临沭县| 黑河市| 龙海市| 临江市| 建宁县| 藁城市| 桃园市| 隆尧县| 久治县| 兰西县| 雷波县| 扎鲁特旗| 延川县| 两当县| 克什克腾旗| 晋州市| 灵丘县| 当涂县| 黄大仙区| 吉林市| 保亭| 江口县| 汶川县| 钟山县| 林西县| 招远市| 峨眉山市| 大邑县| 岳普湖县|