【问题标题】:How to build a Pie chart using Research Kit如何使用研究工具包构建饼图
【发布时间】:2017-07-12 09:28:37
【问题描述】:

您好,我是 swift 新手,我尝试使用研究工具包应用构建饼图。

我参考这个link写了一些代码

当运行我的代码时,它显示错误'类型'ViewController'不符合协议'ORK PieChartView DataSource''

请建议如何解决这个问题。

这是我的代码:

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)"
                }
            }
        }
    }

【问题讨论】:

    标签: ios swift3 researchkit


    【解决方案1】:

    错误 'Type 'ViewController' 不符合协议 'ORK PieChartView DataSource' means that you are not implemented all the required data source methods..... 在你的代码中你写了一个 wrong datasource method 改成func pieChartView(_ pieChartView: ORKPieChartView, valueForSegmentAt index: Int) -> CGFloat,改一下 像下面.....

    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
                    }
    

    【讨论】:

    • 感谢回复。我搜索了缺少的方法,但没有找到。如果您知道缺少什么方法。请列出它们。
    • 其实你只需删除func pieChartView(_ pieChartView: ORKPieChartView, valueForSegmentAt index: Int) -> CGFloat 并添加我上面提到的数据源方法.....
    猜你喜欢
    • 2015-06-19
    • 2015-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多