Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. import UIKit
  2.  
  3. class GonDetail2ViewController: UIViewController {
  4. var dataMoneyTracker1 = [Int]()
  5. @IBOutlet var View1: UIView!
  6.  
  7. }
  8.  
  9. import UIKit
  10.  
  11. class GonGraphUIView: UIView {
  12.  
  13.  
  14. var graphPoints = [Int]()
  15.  
  16. override func drawRect(rect: CGRect) {
  17. let width = rect.width
  18. let height = rect.height
  19.  
  20. let margin:CGFloat = 20.0
  21. let columnXPoint = { (column:Int) -> CGFloat in
  22. //Calculate gap between points
  23. let spacer = (width - margin*2 - 4) /
  24. CGFloat((self.graphPoints.count - 1))
  25. var x:CGFloat = CGFloat(column) * spacer
  26. x += margin + 2
  27. return x
  28.  
  29.  
  30.  
  31. }
  32.  
  33. let topBorder:CGFloat = 60
  34. let bottomBorder:CGFloat = 50
  35. let graphHeight = height - topBorder - bottomBorder
  36. let maxValue = graphPoints.maxElement()
  37. let columnYPoint = { (graphPoint:Int) -> CGFloat in
  38. var y:CGFloat = CGFloat(graphPoint) /
  39. CGFloat(maxValue!) * graphHeight
  40. y = graphHeight + topBorder - y // Flip the graph
  41. return y
  42. }
  43.  
  44. UIColor.whiteColor().setFill()
  45. UIColor.whiteColor().setStroke()
  46.  
  47. //set up the points line
  48. let graphPath = UIBezierPath()
  49. //go to start of line
  50. graphPath.moveToPoint(CGPoint(x:columnXPoint(0),
  51. y:columnYPoint(graphPoints[0])))
  52.  
  53. //add points for each item in the graphPoints array
  54. //at the correct (x, y) for the point
  55. for i in 1..<graphPoints.count {
  56. let nextPoint = CGPoint(x:columnXPoint(i),
  57. y:columnYPoint(graphPoints[i]))
  58. graphPath.addLineToPoint(nextPoint)
  59. }
  60.  
  61. graphPath.stroke()
  62.  
  63. graphPath.lineWidth = 2.0
  64. graphPath.stroke()
  65.  
  66. //Draw the circles on top of graph stroke
  67. for i in 0..<graphPoints.count {
  68. var point = CGPoint(x:columnXPoint(i), y:columnYPoint(graphPoints[i]))
  69. point.x -= 5.0/2
  70. point.y -= 5.0/2
  71.  
  72. let circle = UIBezierPath(ovalInRect:
  73. CGRect(origin: point,
  74. size: CGSize(width: 5.0, height: 5.0)))
  75. circle.fill()
  76. }
  77.  
  78. /*
  79. // Only override drawRect: if you perform custom drawing.
  80. // An empty implementation adversely affects performance during animation.
  81. override func drawRect(rect: CGRect) {
  82. // Drawing code
  83. }
  84. */
  85.  
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement