ScrollableGraphView
An adaptive scrollable graph view for iOS to visualise simple discrete datasets. Written in Swift.
Install / Use
/learn @philackm/ScrollableGraphViewREADME
ScrollableGraphView
Announcements
9-7-2017 - Version 4:
Version 4 was released which adds multiple plots, dynamic reloading of values, more reference line customisation options and various bug fixes.
You can see the major changes in the API here.
The public interface is incompatible with previous versions. If you prefer to keep using the older version, make sure to specify version 3 in your podfile or downloaded the classes from a pre-v4 release.
About

An adaptive scrollable graph view for iOS to visualise simple discrete datasets. Written in Swift. Originally written for a small personal project.
The main goal of the this graph component is to visualise simple discrete datasets and allow the the user to scroll through the graph.

Contribution
All pull requests are welcome. There is a list of features people would like on the issues page, ranging from simple changes to quite complex. Feel free to jump on in.
Sponsors
Development of this component has been sponsored by Anomaly. Check them out here.
Contents
Features
| Feature List |
|--------|
| Initialisation animations and range adaption animations. <br><br>
|
| Multiple plots and dynamic reloading of the values. <br><br>
|
| Range adaption when scrolling through the graph. The range of the y-axis will automatically adapt to to the min and max of the visible points. <br><br>
|
| Smooth scrolling around the graph. <br><br>
|
| Handles as many points as you can throw at it. <br><br>
|
| Many customisation options. (Check the customisation section) <br><br>
|
Usage
Adding the ScrollableGraphView to your project:
Add the ScrollableGraphView class to your project. There are two ways to add the ScrollableGraphView to your project.
Manually
Add all of the files in the Classes directory to your project in Xcode to your project in Xcode.
CocoaPods
Add pod 'ScrollableGraphView' to your Podfile and then make sure to import ScrollableGraphView in your code.
Carthage
Add github "philackm/ScrollableGraphView" ~> 4.0.2 to your Cartfile and then make sure to link the frameworks and import ScrollableGraphView in your code.
Creating a graph and providing it with data.
-
Create a ScrollableGraphView instance. The graph requires a data source, which is an object that conforms to the
ScrollableGraphViewDataSourceprotocol.// Compose the graph view by creating a graph, then adding any plots // and reference lines before adding the graph to the view hierarchy. let graphView = ScrollableGraphView(frame: frame, dataSource: self) let linePlot = LinePlot(identifier: "line") // Identifier should be unique for each plot. let referenceLines = ReferenceLines() graphView.addPlot(plot: linePlot) graphView.addReferenceLines(referenceLines: referenceLines) -
Ensure the dataSource object conforms to the
ScrollableGraphViewDataSourceprotocol and implements the following three methods like so:func value(forPlot plot: Plot, atIndex pointIndex: Int) -> Double { // Return the data for each plot. switch(plot.identifier) { case "line": return linePlotData[pointIndex] default: return 0 } } func label(atIndex pointIndex: Int) -> String { return "FEB \(pointIndex)" } func numberOfPoints() -> Int { return numberOfDataPointsInGraph } -
Finally, add the ScrollableGraphView to the view hierarchy.
someViewController.view.addSubview(graphView)
This will create a graph that looks something like:

Interface Builder support
There is now support for Interface Builder (from CocoaPod version 2.0.0). See the example project in the folder: graphview_example_ib
Things you could use it for:
- ✔ Study applications to show time studied/etc
- ✔ Weather applications
- ✔ Prototyping
- ✔ Simple data visualisation
Things you shouldn't/cannot use it for:
- ✘ Rigorous statistical software
- ✘ Important & complex data visualisation
- ✘ Graphing continuous mathematical functions
Customisation
The entire graph is composed by initially creating an empty ScrollableGraphView object and progressively adding whatever plots and reference lines you require.
Create a plot using the any of the LinePlot, DotPlot, BarPlot constructors. Create reference lines using the ReferenceLines() constructor. Before adding the ScrollableGraphView object to the view hierarchy, add the plots and reference lines to the graph using the addPlot and addReferenceLines methods. You can add multiple plots (examples are shown below). Each plot must have the same number of data points.
In the case of interface builder, graph customisation is performed via the properties pane, whilst plots and reference lines customisation is done in the corresponding view controller. See the example project in the folder: graphview_example_ib
Graph Customisation
These settings can be set directly on the ScrollableGraphView object before adding it to the view hierarchy.
Adapting & Animations
| Property | Description |
|---------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| shouldAdaptRange: Bool | Whether or not the y-axis' range should adapt to the points that are visible on screen. This means if there are only 5 points visible on screen at any given time, the maximum on the y-axis will be the maximum of those 5 points. This is updated automatically as the user scrolls along the graph.
|
| shouldAnimateOnAdapt: Bool | If shouldAdaptRange is set to true then this specifies whether or not the points on the graph should animate to their new positions. Default is set to true. Looks very janky if set to false. |
| shouldAnimateOnStartup: Bool | Whether or not the graph should animate to their positions when the graph is first displayed. |
Spacing

| Property | Description | |-----------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | topMargin: CGFloat | How far the "maximum" reference line is from the top of the view's frame. In points. | | bottomMargin: CGFloat | How far the "minimum" reference line is from the bottom of the view's frame. In points. | | leftmostPointPadding: CGFloat | How far the first point on the graph should be placed from the left hand side of the view. | | rightmostPointPadding: CGFloat | How far the final point on the graph should be placed from the right hand side of the view. | | dataPointSpacing: CGFloat | How much space should be between each data point.
