[英]pie chart/plot in swift
我嘗試了很多不同的方法來添加GitHub中的繪圖包或者使用核心繪圖或將objective-c結合到swift中,但是在這個過程中出現了很多問題,並且在本周我沒有成功繪制圖表。 我真的很沮喪。
有人在swift中成功創建餅圖嗎? 類似的問題似乎沒有成功的答案。
我將衷心感謝您的幫助!
不要郁悶。 您只需添加更具體的問題即可獲得更多幫助。 例如,如果你從頭開始並嘗試集成來自Github的繪圖包,你必須說明什么包,你是如何嘗試集成它的,你得到了什么錯誤等等。
但是,使用CoreGraphics功能繪制簡單的餅圖非常簡單。 這是我的代碼中的一個小禮物,它將進度值繪制為簡單的黑白餅圖。 它只有2個部分,但你可以從中推廣
@IBDesignable class ProgressPieIcon: UIView {
@IBInspectable var progress : Double = 0.0 {
didSet {
self.setNeedsDisplay()
}
}
required init(coder aDecoder: NSCoder) {
super.init(coder:aDecoder)
self.contentMode = .Redraw
}
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.clearColor()
self.contentMode = .Redraw
}
override func drawRect(rect: CGRect) {
let color = UIColor.blackColor().CGColor
let lineWidth : CGFloat = 2.0
// Calculate box with insets
let margin: CGFloat = lineWidth
let box0 = CGRectInset(self.bounds, margin, margin)
let side : CGFloat = min(box0.width, box0.height)
let box = CGRectMake((self.bounds.width-side)/2, (self.bounds.height-side)/2,side,side)
let ctx = UIGraphicsGetCurrentContext()
// Draw outline
CGContextBeginPath(ctx)
CGContextSetStrokeColorWithColor(ctx, color)
CGContextSetLineWidth(ctx, lineWidth)
CGContextAddEllipseInRect(ctx, box)
CGContextClosePath(ctx)
CGContextStrokePath(ctx)
// Draw arc
let delta : CGFloat = -CGFloat(M_PI_2)
let radius : CGFloat = min(box.width, box.height)/2.0
func prog_to_rad(p: Double) -> CGFloat {
let rad = CGFloat(p * 2 * M_PI)
return rad + delta
}
func draw_arc(s: CGFloat, e: CGFloat, color: CGColor) {
CGContextBeginPath(ctx)
CGContextMoveToPoint(ctx, box.midX, box.midY)
CGContextSetFillColorWithColor(ctx, color)
CGContextAddArc(
ctx,
box.midX,
box.midY,
radius-lineWidth/2,
s,
e,
0)
CGContextClosePath(ctx)
CGContextFillPath(ctx)
}
if progress > 0 {
let s = prog_to_rad(0)
let e = prog_to_rad(min(1.0, progress))
draw_arc(s, e, color)
}
}
我們對Core Plot API進行了一些更改,包括用NSDecimal
替換NSDecimalNumber
,以實現Swift兼容性。 這些更改位於release-2.0
分支上,而不是發布包中。 有關該問題的更多討論,請參閱Core Plot問題#96 。
您是否曾嘗試使用Swift查找任何CorePlot教程? 也許就像這樣 。
否則,您可能想看看其他框架 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.