8.21 Draw 绘图 走线与柱状图2

2016-09-05  本文已影响46人  jayck
import UIKit

class CustomerVIew: UIView {

    
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        EX()
        XY()
        XX()
        let context = UIGraphicsGetCurrentContext()        
//        走线图/折线图
        CGContextMoveToPoint(context, 50, 600)
        CGContextAddLineToPoint(context, 100, 520)
        CGContextAddLineToPoint(context, 150, 510)
        CGContextAddLineToPoint(context, 200, 590)
        CGContextAddLineToPoint(context, 250, 350)
        CGContextAddLineToPoint(context, 300, 450)
        CGContextSetStrokeColor(context, [1, 0, 0, 1])
        CGContextSetLineWidth(context, 2)
     
        CGContextStrokePath(context)
        
    }
    //画表格    
    func EX() {
        let context = UIGraphicsGetCurrentContext()
        
        var y: CGFloat = 50
        while y < self.bounds.size.height {
            CGContextMoveToPoint(context, 0, y)
            
            CGContextAddLineToPoint(context, self.bounds.size.width, y)
            
            y += 50
        }
        
        var x: CGFloat = 50
        while x < self.bounds.size.width {
            CGContextMoveToPoint(context, x, 0)
            
            CGContextAddLineToPoint(context, x, self.bounds.size.height)
            
            x += 50
        }
        //保存当前上下文状态
        CGContextSaveGState(context)
        
        CGContextSetLineWidth(context, 1)
        CGContextSetLineDash(context, 0, [4, 2], 2)
        CGContextSetStrokeColor(context, [0.2, 0, 1, 0.5])   // 线条颜色
        
        
        //绘制
        CGContextStrokePath(context)
        
        //重置到之前状态
        CGContextRestoreGState(context)
       
    }
    //画XY轴
    func XY() {
        
        let context = UIGraphicsGetCurrentContext()
        
        CGContextSaveGState(context)

        //画X,Y轴线
        
        CGContextMoveToPoint(context, 50, 600)
        CGContextAddLineToPoint(context, 50, 250)
        CGContextSetStrokeColor(context, [0, 0, 0, 1])
        CGContextSetLineWidth(context, 3)
        
        CGContextMoveToPoint(context, 50, 600)
        CGContextAddLineToPoint(context, 400, 600)
        CGContextSetStrokeColor(context, [0, 0, 0, 1])
        CGContextSetLineWidth(context, 3)
        
        CGContextStrokePath(context)
        
        CGContextRestoreGState(context)
    }
    
    //画柱状图    
    func XX() {
        let context = UIGraphicsGetCurrentContext()
        
        CGContextSaveGState(context)

    //        柱状图
    CGContextAddRect(context, CGRect(x: 95, y: 520, width: 10, height: 80))
    CGContextAddRect(context, CGRect(x: 145, y: 510, width: 10, height: 90))
    CGContextAddRect(context, CGRect(x: 195, y: 590, width: 10, height: 10))
    CGContextAddRect(context, CGRect(x: 245, y: 350, width: 10, height: 250))
    CGContextAddRect(context, CGRect(x: 295, y: 450, width: 10, height: 150))
    
    
    CGContextSetStrokeColor(context, [0, 0, 0.5, 1])
    CGContextSetFillColor(context, [0.8, 0.5, 0.8, 1])
    
    CGContextDrawPath(context, .FillStroke)
    CGContextRestoreGState(context)
    }
}

编译运行结果如下:

绘图用的比较多的应用类型

股票类 搜索 pod seach CorePlot

健康类,运动类,金融类

上一篇下一篇

猜你喜欢

热点阅读