无图无真相!
真像
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..<ringCount {
let r = innerRadius + ringSpace*CGFloat(ringCount - i - 1) + ringWidth*CGFloat(ringCount - i) - ringWidth/2
ctf.addArc(center: centerP, radius: r, startAngle: -CGFloat(Double.pi/2), endAngle: CGFloat(Double.pi*2*values![i]) - CGFloat(Double.pi/2), clockwise: false)
ctf.setStrokeColor(colors![i].cgColor)
ctf.strokePath()
}
}
/// 绘制中间文本
private func drawCenterText(ctf: CGContext) {
if title == nil {
return
}
//转换坐标
ctf.textMatrix = CGAffineTransform.identity
ctf.translateBy(x: 0, y: self.bounds.height)
ctf.scaleBy(x: 1, y: -1)
//清除文本四边颜色
ctf.setStrokeColor(UIColor.clear.cgColor)
let h = (title?.boundingRect(with: CGSize(width: innerRadius*2, height: innerRadius*2), options: .usesLineFragmentOrigin, context: nil).height)!
let style = NSMutableParagraphStyle()
style.alignment = .center
title?.addAttribute(.paragraphStyle, value: style, range: NSRange(location: 0, length: title!.length))
let x: CGFloat = self.frame.size.width/2 - CGFloat(innerRadius)
let y: CGFloat = self.frame.size.height/2 - h/2
let textPath = CGMutablePath()
textPath.addRect(CGRect(x: x, y: y, width: CGFloat(innerRadius*2), height: h ))
ctf.addPath(textPath)
ctf.drawPath(using: .stroke)
let frameSetter = CTFramesetterCreateWithAttributedString(title!)
let fram = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, title!.length), textPath, nil)
CTFrameDraw(fram, ctf)
}
}
在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 版本
网友评论