Skip to content

Instantly share code, notes, and snippets.

@vtourraine
Created February 17, 2016 10:20
Show Gist options
  • Save vtourraine/82a55d9e6588ca54170b to your computer and use it in GitHub Desktop.
Save vtourraine/82a55d9e6588ca54170b to your computer and use it in GitHub Desktop.
ResearchKit dashboard controller with activity completion pie chart
//
// 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: "")
}
}
}
@vtourraine
Copy link
Author

Here’s what the dashboard looks like:

completion-pie-chart

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment