UIGraphView is a view type designed for iOS using the Swift programming language.
|Bar Graph Mode||Line Graph Mode|
Designed to display a timeline of information:
- Past day by hour
- Past week by day
- Past month by day
- Past year by month
Can be modified to display different types of information sets.
- Easy installation with a single function to set up
- Customizable design through interface builder
- Automatically creates x-axis labels based on timeFrame passed and current date
SharedExtensions.swift to your Xcode project.
SharedExtensions.swift contains an extension I wrote in order to easily return a clean string from a Double. This is used in several places in
UIGraphView.swift so it is essential. Alternatively, you can copy the
cleanValue extension and paste it at the end of
UIGraphView.swift to your Xcode project.
Add a view to your view controller and set its class to UIGraphView.
Create an @IBOutlet of type UIGraphView and point it at the view:
@IBOutlet var graphView: UIGraphView!
To setup the graph information use
graphView.configure(graphData: [Double], graphTimeFrame: String, graphTitle: String, graphSubtitle: String, latestEntry: String, latestEntryTime: String)
graphData expects an array of Doubles and should automatically adjust placement of data points based on the amount of entries in the array.
graphTimeFrame expects a string of
Year. If one of these strings is passed to
graphTimeFrame, x-axis labels will be automatically created for you based on that time frame and the current date.
latestEntryTime all expect String values.
While the graph should accept any length of
graphData array, for the data points to properly line up with the x-axis labels you should use an array with a length appropriate for the
graphTimeFrame you use:
Dayexpects 24 entries
Weekexpects 7 entries
Monthexpects 31 entries
Yearexpects 12 entries
Interface Builder Designable
UIGraphView can be set up in Interface Builder. You can specify Graph Type (
barGraph), Graph Corners (
cornerRadius), Graph Background Colors (
gradientBottom), and Graph Information Colors (
graphFontColor). All of these have default settings.
UIGraphView was built and tested using:
- iOS 11
- Xcode 9
- Swift 4
Programmed and designed by Peter Mostoff. You can contact me via Twitter if you'd like! I'm definitely open to suggestions on how to improve this code. I made it work for what I needed and thought I'd share, but I'm sure there are ways to improve it that I haven't even thought of. Please let me know if you have any suggestions!
If you do use this code in an application you're working, I'd really appreciate you letting me know. I'd really enjoy getting to know this was useful for someone else and maybe I'll even drop in a link to your app here!
Apps Using UIGraphView
UIGraphView is available under the MIT license. See the LICENSE file for more info.