Draw Horizontal Bar chart Using Swift (IOS)
We will use UIBezierPath with CAShapeLayer
First of all you need to make a UIVIEWCONTROLL in your app
Next go to viewdidload function and simple
simple call the method to make a bar
How to call
This will simple make a single ..
Comple code to draw Graph(horizontal bar chart)
We will use UIBezierPath with CAShapeLayer
First of all you need to make a UIVIEWCONTROLL in your app
Next go to viewdidload function and simple
simple call the method to make a 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
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)
This will simple make a single ..
Comple code to draw Graph(horizontal bar chart)
//
// HorizontalBarChart.swift
// Graphs
//
// Created by tahir fazal hussain on 01/12/2014.
// Copyright (c) 2014 tahir fazal hussain. All rights reserved.
//
import UIKit
class HorizontalBarChart: UIViewController {
var frameWidth = 0
var onePart = 0.0
override func viewDidLoad() {
super.viewDidLoad()
frameWidth = (Int(self.view.frame.width) - 40)
onePart = Double(frameWidth) / Double(100)
NSLog("frame Width %i One Part %d", frameWidth,onePart)
self.view.backgroundColor = UIColor.whiteColor()
drawlines(lineNumber:1, percent:30.0, linename:"Bar1")
drawlines(lineNumber:2, percent:85, linename:"Bar2")
drawlines(lineNumber:3, percent:49.0, linename:"Bar3")
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func drawlines (lineNumber num:Int , percent val:Double,linename name:String){
let startpoint=50
let distance=80
let start = CGPoint(x:20,y:Int(num*distance)+startpoint)
let end = CGPoint(x:Int(val*onePart)+20,y:Int(num*distance)+startpoint)
//first label settings
var lbl = UILabel()
lbl.frame = CGRectMake(start.x, start.y - 25, 55, 15)
lbl.font = lbl.font.fontWithSize(15)
lbl.text = name
view.addSubview(lbl)
//red part of line
drawLine(startpoint: start, endpint: end,linecolor: UIColor.redColor().CGColor,linewidth:11.0)
//gray part of line
let nextpt = Int(Double(100 - val)*onePart) + Int(val*onePart)
let nstart = CGPoint(x:Int(val*onePart)+20,y:Int(num*distance)+startpoint)
let nend = CGPoint(x:nextpt,y:Int(num*distance)+startpoint)
drawLine(startpoint: nstart, endpint: nend, linecolor: UIColor.grayColor().CGColor,linewidth:11.0)
//black vertical line
let vline = CGPoint(x:end.x,y:end.y+5)
let height = CGPoint(x:end.x,y:end.y-30)
drawLine(startpoint: vline, endpint: height, linecolor: UIColor.blackColor().CGColor,linewidth:2.0)
//value label
var lbl2 = UILabel()
//check distance
if ((vline.x + 100 ) > self.view.frame.width ){
lbl2.frame = CGRectMake(vline.x-50, start.y - 25, 50, 15)
}
else{
lbl2.frame = CGRectMake(vline.x+5, start.y - 25, 50, 15)
}
lbl2.font = lbl2.font.fontWithSize(15)
lbl2.text = "value"
view.addSubview(lbl2)
//description value
var lbl3 = UILabel()
lbl3.frame = CGRectMake(start.x+40, start.y + 15, 150, 15)
lbl3.font = lbl3.font.fontWithSize(15)
lbl3.text = "Some Description"
view.addSubview(lbl3)
}
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)
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}