美文网首页
iOS走势图

iOS走势图

作者: 枫叶风 | 来源:发表于2018-03-12 13:44 被阅读0次

绘制如图的走势图,原理其实很简单。需要用到CGContext,确定选中的数字,获取选中数字的坐标,然后根据坐标在两个选中的数字中绘制连线。

彩票走势图
首先定义几个宏定义:
#define maxNum 10    // 假设每行都10个数字
#define FormWidth [[UIScreen mainScreen] bounds].size.width/(maxNum+1)    // 每个放个都长宽
#define FormHeight FormWidth
#define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width

定义几个数组来保存数据:

@interface ChartView()

@property (nonatomic , copy) NSArray *xDatas;      // 每行的数字为0~9
@property (nonatomic , copy) NSArray *yDatas;      // 每列的数据
@property (nonatomic , strong) NSMutableArray *fillDatas;  // 来存放选中号码的数组
@property (nonatomic , strong) NSMutableArray *frameArray;  // 用来存放坐标的数组

@end

初始化页面,并在数组中填入一些数据:

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _yDatas = @[@"01期",@"02期",@"03期",@"04期",@"05期",@"06期",@"07期",@"08期",@"09期",@"10期"];
        _xDatas = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];
        // 选中号码数组
        _fillDatas = [NSMutableArray new];
        _frameArray = [NSMutableArray new];
        self.backgroundColor = [UIColor whiteColor];
        // 随机取选中号码
        for (NSInteger i = 0; i < _xDatas.count; i++) {
            NSString *number = [self randomFormArray:_xDatas];
            [_fillDatas addObject:number];
        }
    }
    return self;
}

- (NSString *)randomFormArray:(NSArray *)array {
    NSInteger count = array.count;
    NSInteger index = arc4random() % count;
    
    return [NSString stringWithFormat:@"%ld" , index];
}

下面是主要的绘制部分,原理很简单基本上都有注释就不多说了:

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    // FormHeight 为格子高度 7是离上边界的距离 不设置 就会出现显示边界的线不好控制
    // 根据yData数据绘制横向线
    for (int i = 0; i <= _yDatas.count; i++) {
        if (i < _yDatas.count) {
            NSString *date = _yDatas[i];
            // 调用计算字符串宽高方法
            CGSize size = [self calculationTextIsze:date andSize:CGSizeMake(FormWidth, FormHeight)];
            [date drawInRect:CGRectMake((FormWidth-size.width)/2.0, (FormHeight-size.height)/2.0+FormHeight*i+7, FormWidth, FormHeight) withAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12] , NSForegroundColorAttributeName : [UIColor blackColor]}];
        }
        
        // 设置画笔颜色
        // 描边颜色
        CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
        CGContextSetLineWidth(context, 1);
        // 画笔的起始坐标
        // 画笔移动到该点开始画线
        CGContextMoveToPoint(context, 0, i * FormHeight + 7);
        // 画直线到该点
        CGContextAddLineToPoint(context, SCREEN_WIDTH, i * FormHeight + 7);
        // 根据坐标绘制路径  kCGPathStroke:只有边框
        CGContextDrawPath(context, kCGPathStroke);
    }
    
    // 设置彩票号码
    for (int j = 0; j <= _yDatas.count; j++) {
        if (j < maxNum) {
            for (int i = 0; i < maxNum; i++) {
                NSString *dateStr = _xDatas[j];
                CGSize size = [self calculationTextIsze:dateStr andSize:CGSizeMake(FormWidth, FormHeight)];
                [dateStr drawInRect:CGRectMake((FormWidth-size.width)/2.0+j*FormWidth+FormWidth,(FormHeight-size.height)/2.0+i*FormHeight+7, FormWidth, FormHeight) withAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12] , NSForegroundColorAttributeName : [UIColor blackColor]}];
            }
        }
        // 绘制纵向边界线
        CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor);
        CGContextSetLineWidth(context, 1);
        CGContextMoveToPoint(context, j * FormWidth , 7);
        CGContextAddLineToPoint(context, j * FormWidth, maxNum * FormWidth + 7);
        CGContextDrawPath(context, kCGPathStroke);
    }
    
    // 画填充圆
    for (int i = 0; i < _fillDatas.count; i++) {
        CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor);
        CGContextSetLineWidth(context, 1);
        NSString *number = _fillDatas[i];
        for (int x = 0 ; x < _xDatas.count; x++) {
            if ([number intValue] == x) {
                // 画圆
                CGContextAddArc(context, FormWidth*x+FormWidth/2+FormWidth, FormHeight*i+FormHeight/2+7, FormHeight/2, 0, M_PI*2, 1);
                // 计算出圆的中心点
                CGPoint point = CGPointMake(FormWidth*x+FormWidth/2+FormWidth, FormHeight*i+FormHeight/2+7);
                NSString *str = NSStringFromCGPoint(point);
                // 保存圆中心的位置,给下面连线
                [_frameArray addObject:str];
                CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor);
                CGContextDrawPath(context, kCGPathStroke);
                
                // 填满整个圆
                CGContextAddArc(context, FormWidth*x+FormWidth/2+FormWidth, FormHeight*i+FormHeight/2+7, FormWidth/2, 0, M_PI*2, 1);
                // 设置填充颜色
                CGContextSetFillColorWithColor(context, [UIColor grayColor].CGColor);
                // 根据坐标绘制路径  kCGPathFill:只有填充,不绘制边框
                CGContextDrawPath(context, kCGPathFill);
                NSString *numberStr = [NSString stringWithFormat:@"%@" , number];
                CGSize size = [self calculationTextIsze:numberStr andSize:CGSizeMake(FormWidth, FormWidth)];
                // 画内容 被选中的号码
                [numberStr drawInRect:CGRectMake((FormWidth-size.width)/2.0+x*FormWidth+FormWidth,(FormHeight-size.height)/2.0+i*FormHeight+7, FormWidth, FormHeight) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:[UIColor redColor]}];
            }
        }
    }
    
    // 绘制连线
    for (int i = 0; i < _frameArray.count; i++) {
        NSString *pointStr = [_frameArray objectAtIndex:i];
        CGPoint point = CGPointFromString(pointStr);
        // 设置画笔颜色
        CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
        CGContextSetLineWidth(context, 2);
        if (i == 0) {
            // 画笔起始坐标
            CGContextMoveToPoint(context, point.x, point.y);
        } else {
            NSString *str1 = [_frameArray objectAtIndex:i-1];
            CGPoint point1 = CGPointFromString(str1);
            CGContextMoveToPoint(context, point1.x, point1.y);
            CGContextAddLineToPoint(context, point.x, point.y);
        }
        CGContextDrawPath(context, kCGPathStroke);
    }
}
// 根据文字内容计算宽高方法
- (CGSize)calculationTextIsze:(NSString *)text andSize:(CGSize)size {
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    paragraphStyle.alignment = NSTextAlignmentLeft;
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:12] , NSParagraphStyleAttributeName : paragraphStyle};
    
    CGSize contentSize = [text boundingRectWithSize:CGSizeMake(size.width, MAXFLOAT) options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) attributes:attributes context:nil].size;
    return contentSize;
}

绘制简单的走势图就是这些代码,这里也有demo可以下载:(https://github.com/sunbin117/chartView)

相关文章

  • iOS股票K线图

    mark:iOS股票K线图 iOS 股票K线图绘制 iOS 股票K线图绘制 从零开始实现k线图走势图绘制(iOS实战篇)

  • iOS走势图

    绘制如图的走势图,原理其实很简单。需要用到CGContext,确定选中的数字,获取选中数字的坐标,然后根据坐标在两...

  • ios 绘制曲线走势图

    - (void)drawLine{ //view是曲线的背景 viewUIView *view = [[UIVie...

  • 西瓜经第六十一章:参考

    现在的彩票技术,百家争艳,莫衷一是。当你在网络中浏览大乐透的走势图的时候,你会发现各种版本的走势图。有的走势图设计...

  • 博观而约取,厚积而薄发

    这个是大神卫斯理的走势图, 这个是我的走势图, 从走势图来看,刚开始进入股市炒股的阶段,都是一个比较平缓或者是赔钱...

  • 《缠论形动学》第4节:中枢的运用【图文+案例】

    无论是打开是大盘走势图,还是板块走势图,还是个股走势图,先找到并确认最后一个中枢,然后根据当下价格所处的相对于中枢...

  • 完美三段

    5分钟走势图上3段完美走势。 30分钟走势图的笔中枢,扩展或者下跌!

  • 走势图

    上涨、下跌、盘整三种基本走势,有六种组合可能代表着三类不同的走势: 陷阱式:上涨+下跌;下跌+上涨。反转式:上涨+...

  • 2018-03-01

    Android开发之实现比特币走势图(仿股票走势图) 1、介绍 最近有关区块链的项目层出不穷,在项目中碰到了绘...

  • 2018-01-22 BTC及ETH行情分析

    首先先看一下ETH的走势图 这是OKEX上的ETH期货走势图,很明显目前在进行DE线段回调当中,纵观整个走势,DE...

网友评论

      本文标题:iOS走势图

      本文链接:https://www.haomeiwen.com/subject/vtenfftx.html