2

I need to implement a horizontal bar graph similar to the one shown below. Here I don't need to show axises along with the graph, still I need to imagine there is one:

enter image description here

How can I achieve this?

2

1 Answer 1

3

You can easily do this by using UIBezierPath with CAShapeLayer

You can use the following function to draw an horizontal bar

    func drawLine(startpoint start:CGPoint, endpint end:CGPoint, linecolor color: CGColor ,  linewidth widthline:CGFloat){        
    var path = UIBezierPath()
    path.moveToPoint(start)
    path.addLineToPoint(end)

    var shapeLayer = CAShapeLayer()
    shapeLayer.path = path.CGPath
    shapeLayer.strokeColor = color
    shapeLayer.lineWidth = widthline

    view.layer.addSublayer(shapeLayer)
    }

How to call this function in swift is

    let start = CGPoint(x:20,y:100)
    let end = CGPoint(x:200,y:100)
    //red part of line

   drawLine(startpoint: start, endpint: end,linecolor:       UIColor.redColor().CGColor,linewidth:11.0)

You can find out complete code in here

http://bestarticlesall.blogspot.com/2014/12/draw-horizontal-bar-chart-using-swift.html

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.