2

Hi i am new to swift and i tried to build a piechart using research kit app.

i write some code with reference of this link

When run my code it showing error 'Type 'ViewController' does not conform to protocol 'ORK PieChartView DataSource''

please suggest how to solve this issue.

here is my code:

import UIKit
    import ResearchKit

    class ViewController: UIViewController,ORKPieChartViewDataSource {


        @IBOutlet weak var pieChartView: ORKPieChartView!
        var colors : NSArray!

        override func viewDidLoad() {
            super.viewDidLoad()

                colors = [
                UIColor(red: 217/225, green: 217/255, blue: 217/225, alpha: 1),
                UIColor(red: 142/255, green: 142/255, blue: 147/255, alpha: 1),
                UIColor(red: 244/255, green: 200/255, blue: 74/255, alpha: 1)
            ]

            // Connect the pie chart object to a data source
            pieChartView.dataSource = pieChartDataSource

            // Optional custom configuration
            pieChartView.showsTitleAboveChart = false
            pieChartView.showsPercentageLabels = true
            pieChartView.drawsClockwise = true
            pieChartView.titleColor = UIColor.purple
            pieChartView.textColor = UIColor.purple
            pieChartView.title = "Weekly"
            pieChartView.text = "Report"
            pieChartView.lineWidth = 10
            pieChartView.showsPercentageLabels = true

        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }

        func numberOfSegmentsInPieChartView(pieChartView: ORKPieChartView ) -> Int {
            return 3
        }

        func pieChartView(_ pieChartView: ORKPieChartView, valueForSegmentAt index: Int) -> CGFloat {
            switch index {
            case 0:
                return 60.0
            case 1:
                return 25.0
            case 2:
                return 15.0
            }

            // Optional methods
            // Give a color to each segment in the pie chart.
            func pieChartView(pieChartView: ORKPieChartView, colorForSegmentAtIndex index: Int) -> UIColor {
                return colors[index]
            }

            // Give a title to each segment in the pie chart.
            func pieChartView(pieChartView: ORKPieChartView, titleForSegmentAtIndex index: Int) -> String {
                switch index {
                case 0:
                    return "Steps taken"
                case 1:
                    return "Tasks completed"
                case 2:
                    return "Surveys completed"
                default:
                    return "task \(index + 1)"
                }
            }
        }
    }
basha
  • 587
  • 2
  • 6
  • 25

1 Answers1

0

The error 'Type 'ViewController' does not conform to protocol 'ORK PieChartView DataSource' means that you are not implemented all the required data source methods..... Here in your code you wrote a wrong datasource method as func pieChartView(_ pieChartView: ORKPieChartView, valueForSegmentAt index: Int) -> CGFloat, change it like below.....

func pieChartView(pieChartView: ORKPieChartView, valueForSegmentAt index: Int) -> CGFloat {
                switch index {
                case 0:
                    return 60.0
                case 1:
                    return 25.0
                case 2:
                    return 15.0
                }
Vignesh Davins
  • 285
  • 1
  • 13
  • Thanks for the reply.i searched for missing methods, but i did n't find. If you know what is missing methods. please list out them. – basha Jul 12 '17 at 10:28
  • Actually u just remove the `func pieChartView(_ pieChartView: ORKPieChartView, valueForSegmentAt index: Int) -> CGFloat` and add the datasource method which i mentioned above..... – Vignesh Davins Jul 12 '17 at 10:34