Draw a line graph with CoreGraphics in Swift. This code is intended to be an example of how you could use CoreGraphics to draw graphs and other similar UI elements.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Graph.xcodeproj Update to Swift 4.2 Aug 23, 2018
Graph Replace line.move with line.addLine Feb 27, 2018
README.md Updated for Swift 4 Dec 3, 2017

README.md

CoreGraphicsGraph

Example Draw a line graph with CGPath and Swift. This is really just the result of playing with Swift, and so I can't garuntee the code is production ready, or even very good.

// GraphView.swift example usage

let myData = [
    ["Mon" : 15],
    ["Tues" : 30],
    ["Weds" : 7],
    ["Thurs" : 60],
    ["Fri" : 30],
    ["Sat" : 15],
    ["Sun" : 45]
]

let graph = GraphView(frame: CGRect(x: 50, y: 50, width: 420, height: 200), data: myData)
self.view.addSubview(graph)

Graph customisation options

There isn't really that many…

  • showLines - whether or not to display lines from Y axis values
  • showPoints - whether or not to display points on the line graph
  • linesColor - The colour of Y axis lines if visible
  • xAxisColor - The colour of the X axis
  • yAxisColor - The colour of the X axis
  • graphColor - The colour of the actual line graph and points
  • labelFont - Axis label font
  • labelColor - The colour of the axis labels
  • originLabelText - Text placed origin point
  • originLabelText - The colour of originLabelText
  • xMargin - padding left of initial point and right of last point