Swift 绘制环形图

无图无真相!


真像

LzRingChart.swift

将其中的颜色值改为你喜欢的色即可。。。



import UIKit

class LzRingChart: UIView {

    
    /// 中间半径
    var innerRadius: CGFloat = 50
    /// 环形个数
    var ringCount:Int = 3
    
    /// 默认环形颜色
    var ringBgColor: UIColor = AppConst.color_d8
    
    var ringWidth: CGFloat = 5
    var ringSpace: CGFloat = 5
    
    var values: [Double]?
    var colors: [UIColor]?
    var title: NSMutableAttributedString?

    override func draw(_ rect: CGRect) {

        let ctf = UIGraphicsGetCurrentContext()
        ctf?.setAllowsAntialiasing(true)
        ctf?.setLineWidth(CGFloat(ringWidth))
        drawBgRing(ctf: ctf!)
        drawPercenRing(ctf: ctf!)
        drawCenterText(ctf: ctf!)
        
    }
    
    
    /// 设置数据源
    func setData(title: NSMutableAttributedString?, values: [Double], colors: [UIColor])  {
        self.values = values
        self.colors = colors
        self.title = title
        self.setNeedsDisplay()
    }
    
    
    /// 重新计算尺寸,防止显示不全
    override func layoutSubviews() {
        super.layoutSubviews()

        let wR = (innerRadius +  CGFloat(ringCount) * ringWidth + CGFloat(ringCount-1) * ringSpace )*2
        var w = self.frame.size.width
        var h = self.frame.size.height
        if wR > w {
            w = wR
        }
        if wR > h {
            h = wR
        }
        debugPrint("宽度=\(w), 高度=\(h)")
        let newSize = CGSize(width: w, height: h)
        let newRect = CGRect(origin: self.bounds.origin, size: newSize)
        self.frame = newRect
    }
    
    
    /// 绘制默认的 n个环形 先绘制最外面的环形
    private func drawBgRing(ctf: CGContext) {
        ringBgColor = AppConst.color_d8
        
        let centerP = CGPoint(x: self.bounds.size.width/2, y: self.bounds.size.height/2)

        for i in 0 ..< ringCount {
            let r = innerRadius + CGFloat(ringCount - i - 1)*ringSpace + CGFloat(ringCount - i)*ringWidth - ringWidth/2
            ctf.addArc(center: centerP, radius: r, startAngle: 0, endAngle: CGFloat(Double.pi*2), clockwise: false)
            ctf.setStrokeColor(ringBgColor.cgColor)
            ctf.strokePath()
        }
    }

    
    /// 绘制百分比
    private func drawPercenRing(ctf: CGContext) {
        if self.values == nil || self.values!.count == 0 {
            return
        }
        let centerP = CGPoint(x: self.bounds.size.width/2, y: self.bounds.size.height/2)

        for i in 0..

在ViewController中使用

    override func viewDidLoad() {
        super.viewDidLoad()

        addRingChart()
    }

    func addRingChart() {
         
         let str1:String = "12:00"
         let attr1 = NSAttributedString(string: str1, attributes: [
             NSAttributedString.Key.font: UIFont.systemFont(ofSize: 32),
             NSAttributedString.Key.foregroundColor : AppConst.color_main
         ])
         
         let str2:String = "\n睡眠总时长"
         let attr2 = NSAttributedString(string: str2, attributes: [
             NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12),
             NSAttributedString.Key.foregroundColor : AppConst.color_9
         ])
         
         let attr = NSMutableAttributedString(attributedString: attr1)
         attr.append(attr2)
         
         
        let ringChart = LzRingChart(frame: CGRect(x: 50, y: 100, width: 200  , height: 200))
         ringChart.backgroundColor = .yellow
         ringChart.setData(title: attr, values: [0.2, 0.5, 0.3], colors: [AppConst.color_chart_deep, AppConst.color_chart_light, AppConst.color_chart_dream])
         self.view.addSubview(ringChart)
    }
    

Android 版本请参考
Android-Java 版本

你可能感兴趣的:(Swift 绘制环形图)