Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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: "")
}
}
}
Owner

vtourraine commented Feb 19, 2016

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