Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- class GonDetail2ViewController: UIViewController {
- var dataMoneyTracker1 = [Int]()
- @IBOutlet var View1: UIView!
- }
- import UIKit
- class GonGraphUIView: UIView {
- var graphPoints = [Int]()
- override func drawRect(rect: CGRect) {
- let width = rect.width
- let height = rect.height
- let margin:CGFloat = 20.0
- let columnXPoint = { (column:Int) -> CGFloat in
- //Calculate gap between points
- let spacer = (width - margin*2 - 4) /
- CGFloat((self.graphPoints.count - 1))
- var x:CGFloat = CGFloat(column) * spacer
- x += margin + 2
- return x
- }
- let topBorder:CGFloat = 60
- let bottomBorder:CGFloat = 50
- let graphHeight = height - topBorder - bottomBorder
- let maxValue = graphPoints.maxElement()
- let columnYPoint = { (graphPoint:Int) -> CGFloat in
- var y:CGFloat = CGFloat(graphPoint) /
- CGFloat(maxValue!) * graphHeight
- y = graphHeight + topBorder - y // Flip the graph
- return y
- }
- UIColor.whiteColor().setFill()
- UIColor.whiteColor().setStroke()
- //set up the points line
- let graphPath = UIBezierPath()
- //go to start of line
- graphPath.moveToPoint(CGPoint(x:columnXPoint(0),
- y:columnYPoint(graphPoints[0])))
- //add points for each item in the graphPoints array
- //at the correct (x, y) for the point
- for i in 1..<graphPoints.count {
- let nextPoint = CGPoint(x:columnXPoint(i),
- y:columnYPoint(graphPoints[i]))
- graphPath.addLineToPoint(nextPoint)
- }
- graphPath.stroke()
- graphPath.lineWidth = 2.0
- graphPath.stroke()
- //Draw the circles on top of graph stroke
- for i in 0..<graphPoints.count {
- var point = CGPoint(x:columnXPoint(i), y:columnYPoint(graphPoints[i]))
- point.x -= 5.0/2
- point.y -= 5.0/2
- let circle = UIBezierPath(ovalInRect:
- CGRect(origin: point,
- size: CGSize(width: 5.0, height: 5.0)))
- circle.fill()
- }
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- override func drawRect(rect: CGRect) {
- // Drawing code
- }
- */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement