4

I've tried so many different ways of add plots package from GitHub or using core plots or combining objective-c into swift, but it appeared so many problems during the process, and during this week I didn't make a successful chart. I'm really depressed.

Is there anyone who succeed in creating a pie chart in swift? The similar questions seem don't have successful answers.

I would really appreciate your help!

3 Answers 3

4

Don't be depressed. You just have to add a more specific question to get more help. For example, if you start from scratch and try to integrate a plots package from Github, you have to say what package, how did you try to integrate it, what errors are you getting etc.

However, drawing a simple pie chart is pretty easy with CoreGraphics functionality. Here is a little gift from my code, this draws progress value as a simple black and white pie chart. It only has 2 sections, but you can generalize from it

@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)
        }
   }
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, thanks a lot for your encouragement! I tried to run your code, but there was error message just after: let ctx = UIGraphicsGetCurrentContext(). It first showed "insert ','", then it showed "pattern variable binding cannot appear in an expression". So the rest of the code using 'ctx' all had error message. Is it something wrong with my settings that I cannot run it?
Sorry, there was a copy paste error, the rest of the CGRectMake call was missing. I updated the answer. I sincerely mean good when I say that you should learn to spot and understand that kind of easy errors yourself, otherwise you will have a hard journey ahead. Maybe start with an easier program first to learn the basics?
If you chose to implement this using Core Graphics and override drawRect(_:), the entire control would be re-rendered in every step of the animation. This is an expensive animation
0

We've made some changes to the Core Plot API, including replacing NSDecimal with NSDecimalNumber, for Swift compatibility. The changes are on the release-2.0 branch and not in a release package yet. See Core Plot issue #96 for more discussion of the issue.

1 Comment

Thanks! I just saw the new branch, I will follow the instructions and give it a try!
-1

Have you tried to look for any CorePlot tutorial with Swift ? Like this one maybe.

Otherwise, you might want to give a look to other framework.

1 Comment

Thanks a lot! I looked it once before and couldn't get the pods installed. But now it works, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.