• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 如何使用研究工具包构建饼图

[复制链接]
菜鸟教程小白 发表于 2022-12-11 18:15:30 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

我引用这个 link 写了一些代码

运行我的代码时,它显示错误“类型'ViewController'不符合协议(protocol)'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)"
                }
            }
        }
    }



Best Answer-推荐答案


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
                }

关于ios - 如何使用研究工具包构建饼图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45053763/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap