Created
February 17, 2016 10:20
-
-
Save vtourraine/82a55d9e6588ca54170b to your computer and use it in GitHub Desktop.
ResearchKit dashboard controller with activity completion pie chart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// DashboardViewController.swift | |
// MyStudyApp | |
// | |
// Created by Vincent Tourraine on 2/16/16. | |
// Copyright © 2016 Shazino. All rights reserved. | |
// | |
import UIKit | |
import ResearchKit | |
class DashboardViewController: UITableViewController, ORKPieChartViewDataSource { | |
@IBOutlet weak var pieChartView: ORKPieChartView! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
activityCompletionPercentage = 80 | |
pieChartView.dataSource = self | |
pieChartView.title = NSLocalizedString("Daily Activity Completion", comment: "") | |
pieChartView.showsTitleAboveChart = true | |
let dateFormatter = NSDateFormatter() | |
dateFormatter.dateStyle = .LongStyle | |
pieChartView.text = dateFormatter.stringFromDate(NSDate()) | |
} | |
// MARK: Pie chart data source | |
func numberOfSegmentsInPieChartView(pieChartView: ORKPieChartView) -> Int { | |
return 2 | |
} | |
enum PieChartSegment: Int { | |
case Completed, Remaining | |
} | |
var activityCompletionPercentage: CGFloat = 0 | |
func pieChartView(pieChartView: ORKPieChartView, valueForSegmentAtIndex index: Int) -> CGFloat { | |
switch PieChartSegment(rawValue: index)! { | |
case .Completed: | |
return activityCompletionPercentage | |
case .Remaining: | |
return 100 - activityCompletionPercentage | |
} | |
} | |
func pieChartView(pieChartView: ORKPieChartView, colorForSegmentAtIndex index: Int) -> UIColor { | |
switch PieChartSegment(rawValue: index)! { | |
case .Completed: | |
return UIColor.orangeColor() | |
case .Remaining: | |
return UIColor.lightGrayColor() | |
} | |
} | |
func pieChartView(pieChartView: ORKPieChartView, titleForSegmentAtIndex index: Int) -> String { | |
switch PieChartSegment(rawValue: index)! { | |
case .Completed: | |
return NSLocalizedString("Completed", comment: "") | |
case .Remaining: | |
return NSLocalizedString("Remaining", comment: "") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here’s what the dashboard looks like: