【问题标题】:pie chart/plot in swift饼图/快速绘图
【发布时间】:2015-04-30 08:53:33
【问题描述】:

我尝试了很多不同的方法,从 GitHub 添加 plots 包或使用 core plots 或将 Objective-c 结合到 swift 中,但过程中出现了很多问题,这周我没有成功图表。我真的很沮丧。

有没有人成功地在 swift 中创建饼图?类似的问题似乎没有成功的答案。

非常感谢您的帮助!

【问题讨论】:

    标签: swift pie-chart


    【解决方案1】:

    不要沮丧。您只需添加更具体的问题即可获得更多帮助。例如,如果你从头开始尝试从 Github 集成一个 plots 包,你必须说明是什么包,你是如何尝试集成它的,你遇到了什么错误等等。

    但是,使用 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)
            }
       }
    

    【讨论】:

    • 您好,非常感谢您的鼓励!我尝试运行您的代码,但之后出现错误消息:let ctx = UIGraphicsGetCurrentContext()。它首先显示“插入','”,然后显示“模式变量绑定不能出现在表达式中”。所以使用'ctx'的其余代码都有错误消息。是我的设置有问题无法运行吗?
    • 抱歉,出现复制粘贴错误,缺少 CGRectMake 调用的其余部分。我更新了答案。当我说你应该学会自己发现和理解那种容易的错误时,我真诚的意思是好的,否则你将有一个艰难的旅程。也许先从一个更简单的程序开始学习基础知识?
    • 如果您选择使用 Core Graphics 实现这一点并覆盖 drawRect(_:),则整个控件将在动画的每一步中重新渲染。这是一个昂贵的动画
    【解决方案2】:

    我们对 Core Plot API 进行了一些更改,包括将 NSDecimal 替换为 NSDecimalNumber,以实现 Swift 兼容性。更改在 release-2.0 分支上,尚未在发布包中。有关该问题的更多讨论,请参阅Core Plot issue #96

    【讨论】:

    • 谢谢!刚看到新的分支,我会按照说明试一试!
    【解决方案3】:

    您是否尝试过使用 Swift 寻找任何 CorePlot 教程?可能喜欢this one

    否则,您可能想看看其他框架

    【讨论】:

    • 非常感谢!我以前看过一次,无法安装吊舱。但现在可以了,谢谢!
    • 不客气!如果链接对您有帮助,您可能希望接受此答案:)
    猜你喜欢
    • 1970-01-01
    • 2016-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多