In the last tutorial here I showed you how to create a line graph with PaintCode. In this tutorial we’ll use that line graph to plot some data. To do this we’ll get body mass (weight) data from HealthKit and plot it on the graph. By the end of this tutorial, you’ll see that although it was great to fetch the data from HealthKit and plot it on a graph, the app isn’t really that useful. In part 3, we will add more features that allow you to write data to HealthKit.
You can begin by downloading the basic tutorial where we left off last time found here.
Allowing the Points and Colours to Be Changed
As the current tutorial stands you can only display a static graph which is really of no use. Lets begin adding in the modifications that allow us to adjust the points and colours as needed.
To allow changes to be made we need to make a few modifications to the LineView class. We need to create some public variables. Open up LineView.swift and add the following just below the class definition:
// Set the UIColors here. Defaults are .red and .gray
public var lineColour: UIColor = .red
public var pointColour: UIColor = .red
public var horizontalLineColour: UIColor = .gray
// The value of the top line on the graph (defaulted to 10)
public var height: CGFloat = 10.0
// Points on the graph
public var point0: CGFloat = 8.0
public var point1: CGFloat = 1.0
public var point2: CGFloat = 2.0
public var point3: CGFloat = 3.0
public var point4: CGFloat = 4.0
public var point5: CGFloat = 5.0
public var point6: CGFloat = 6.0
// Line width is defaulted to 2.0
public var lineWidth: CGFloat = 2.0