core-plot swift version of barFillForPlotColor



祝大家美好的一天;

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

现在,与其对所有条形使用一种颜色,不如根据它们的值为它们着色。 例如,小于 1.0 的值为红色,值 1.0 为蓝色,高于 1.0 的值为绿色。

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

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

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
}

啊,我明白了;函数调用被转换为类似 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
}

谢谢埃里克。

相关内容

  • 没有找到相关文章

最新更新