菜鸟教程小白 发表于 2022-12-11 18:15:30

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


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

<p>我引用这个 <a href="https://github.com/ResearchKit/ResearchKit/blob/master/docs/Charts/ChartsAndGraph-template.markdown" rel="noreferrer noopener nofollow">link</a> 写了一些代码</p>

<p>运行我的代码时,它显示错误“类型'ViewController'不符合协议(protocol)'ORK PieChartView DataSource'”</p>

<p>请建议如何解决这个问题。</p>

<p>这是我的代码:</p>

<pre><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 = &#34;Weekly&#34;
            pieChartView.text = &#34;Report&#34;
            pieChartView.lineWidth = 10
            pieChartView.showsPercentageLabels = true

      }

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

      func numberOfSegmentsInPieChartView(pieChartView: ORKPieChartView ) -&gt; Int {
            return 3
      }

      func pieChartView(_ pieChartView: ORKPieChartView, valueForSegmentAt index: Int) -&gt; 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) -&gt; UIColor {
                return colors
            }

            // Give a title to each segment in the pie chart.
            func pieChartView(pieChartView: ORKPieChartView, titleForSegmentAtIndex index: Int) -&gt; String {
                switch index {
                case 0:
                  return &#34;Steps taken&#34;
                case 1:
                  return &#34;Tasks completed&#34;
                case 2:
                  return &#34;Surveys completed&#34;
                default:
                  return &#34;task \(index + 1)&#34;
                }
            }
      }
    }
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><blockquote>
<p>The error &#39;Type &#39;ViewController&#39; does not conform to protocol
&#39;ORK PieChartView DataSource&#39; <code>means that you are not implemented all
the required data source methods</code>..... Here in your code you wrote a
<code>wrong datasource method</code> as<code>func pieChartView(_ pieChartView:
ORKPieChartView, valueForSegmentAt index: Int) -&gt; CGFloat</code>, change it
like below.....</p>
</blockquote>

<pre><code>func pieChartView(pieChartView: ORKPieChartView, valueForSegmentAt index: Int) -&gt; CGFloat {
                switch index {
                case 0:
                  return 60.0
                case 1:
                  return 25.0
                case 2:
                  return 15.0
                }
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何使用研究工具包构建饼图,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/45053763/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/45053763/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何使用研究工具包构建饼图