【问题标题】:core-plot swift version of barFillForPlotColorbarFillForPlotColor 的核心绘图快速版本
【发布时间】:2018-10-03 17:35:30
【问题描述】:

祝大家今天好;

我最近开始在我的 iOS 项目中使用 CorePlot,并学习了如何根据我的数据创建条形图(感谢 RayWenderlich 教程)。

现在,我不想为所有条形使用一种颜色,而是希望根据它们的值对它们进行着色。例如,低于 1.0 的值为红色,低于 1.0 的值为蓝色,高于 1.0 的值为绿色。

我知道这是可以做到的,并且我已经阅读了很多关于使用 -(nullable CPTFill *)barFillForBarPlot:(nonnull CPTBarPlot *)barPlot recordIndex:(NSUInteger)index 函数的文章。事实上,我已经查看了 CorePlot 示例 - 绘图库 中的 colouredBarChart 示例,并了解他们如何使用此功能。我遇到的问题是我正在使用 Swift 编程,并且需要有一个我可以使用的这个函数的 Swift 版本,或者解释我如何在我的 Swift 代码中使用这个版本。

非常感谢您提供的任何帮助。

【问题讨论】:

    标签: core-plot


    【解决方案1】:

    Swift 编译器会自动重命名 Objective-C 方法以使它们更“Swifty”。在条形图数据源中试试这个:

    func barFill(for plot: CPTBarPlot, record: UInt) -> CPTFill?
    {
        return CPTFill(color: .red()) // or whatever color you want based on the data value
    }
    

    【讨论】:

      【解决方案2】:

      啊,我明白了;函数调用被转换为像 Swift 一样。根据这些信息,我可以查看一些我已经在 CorePlot 中使用的 Swift 调用以及相关的 Objective C 代码,以了解如何转换它。

      这是我想出的:

         // original function
      -(nullable CPTFill *)barFillForBarPlot:(nonnull CPTBarPlot *)barPlot recordIndex:(NSUInteger)index
      {
         return CPTFill(color: .red()) // or whatever color you want based on the data value
      }
         // Swift version of the function (updated 2018/Oct(10)/07)
      func barFill(for barPlot: CPTBarPlot, record index: UInt ) -> CPTFill?
      {
         return CPTFill(color: .red()) // or whatever color you want based on the data value
      }
      

      谢谢埃里克。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-05-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-30
        相关资源
        最近更新 更多