Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
swift中的饼图/绘图_Swift_Pie Chart - Fatal编程技术网

swift中的饼图/绘图

swift中的饼图/绘图,swift,pie-chart,Swift,Pie Chart,我已经尝试了很多不同的方法从GitHub添加plots包,或者使用core plots,或者将objective-c合并到swift中,但是在这个过程中出现了很多问题,在这周我没有制作出成功的图表。我真的很沮丧 是否有人成功地在swift中创建了饼图?类似的问题似乎没有成功的答案 我非常感谢你的帮助 您是否尝试过使用Swift查找CorePlot教程?也许吧 否则,您可能想看看其他框架。不要沮丧。你只需要添加一个更具体的问题就可以得到更多的帮助。例如,如果您从头开始尝试集成Github的plot

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

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


我非常感谢你的帮助

您是否尝试过使用Swift查找CorePlot教程?也许吧


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

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

然而,使用CoreGraphics功能绘制简单的饼图非常容易。这是我的代码中的一个小礼物,它将进度值绘制为一个简单的黑白饼图。它只有两个部分,但您可以从中进行概括

@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)
        }
   }

我们对核心绘图API进行了一些更改,包括将
NSDecimalNumber
替换为
NSDecimalNumber
,以实现快速兼容性。更改在
release-2.0
分支中,尚未在发布包中。有关此问题的更多讨论,请参阅。

您好,非常感谢您的鼓励!我试图运行您的代码,但紧接着出现错误消息:let ctx=UIGraphicsGetCurrentContext()。它首先显示“insert',”,然后显示“模式变量绑定不能出现在表达式中”。因此,使用“ctx”的其余代码都有错误消息。我的设置有问题吗?我无法运行它?很抱歉,出现了复制粘贴错误,CGRectMake调用的其余部分丢失。我更新了答案。当我说你应该学会自己发现和理解那种容易犯的错误时,我是真诚的好意,否则你将有一段艰难的旅程。也许可以先从一个更简单的程序开始学习基础知识?如果您选择使用核心图形并覆盖drawRect(:),则整个控件将在动画的每个步骤中重新渲染。这是一个昂贵的动画非常感谢!我以前看过一次,没法安装吊舱。但是现在它工作了,谢谢!不客气!如果链接对您有所帮助,您可能希望接受此答案:)谢谢!我刚刚看到了新的分支,我将按照说明进行尝试!