渐变填写Swift for iOS-Charts

我正在尝试创建一个很好的渐变填充,如ios-charts页面上的演示中所示。 但是,我无法在swift vs obj-c中解决这个问题。 这是obj-c代码:

NSArray *gradientColors = @[ (id)[ChartColorTemplates colorFromString:@"#00ff0000"].CGColor, (id)[ChartColorTemplates colorFromString:@"#ffff0000"].CGColor ]; CGGradientRef gradient = CGGradientCreateWithColors(nil, (CFArrayRef)gradientColors, nil); set1.fillAlpha = 1.f; set1.fill = [ChartFill fillWithLinearGradient:gradient angle:90.f]; 

现在这是我在swift中的版本:

  let gradColors = [UIColor.cyanColor().CGColor, UIColor.init(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)] let colorLocations:[CGFloat] = [0.0, 1.0] let gradient = CGGradientCreateWithColors(CGColorSpaceCreateDeviceRGB(), gradColors, colorLocations) 

我缺少的是:图表填充fillWithLinearGradient

我无法在任何地方找到podWithLinearGradient,所以我有点困惑。

在此先感谢您的帮助! 抢

这非常有效(Swift 3.1和ios-charts 3.0.1)


 let gradientColors = [UIColor.cyan.cgColor, UIColor.clear.cgColor] as CFArray // Colors of the gradient let colorLocations:[CGFloat] = [1.0, 0.0] // Positioning of the gradient let gradient = CGGradient.init(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: gradientColors, locations: colorLocations) // Gradient Object yourDataSetName.fill = Fill.fillWithLinearGradient(gradient!, angle: 90.0) // Set the Gradient set.drawFilledEnabled = true // Draw the Gradient 

结果

在此处输入图像描述

我相信这是您正在寻找的代码。 您应该能够在Swift中使用相同的ChartFill类来设置set1.fill

 let gradColors = [UIColor.cyanColor().CGColor, UIColor.clearColor.CGColor] let colorLocations:[CGFloat] = [0.0, 1.0] if let gradient = CGGradientCreateWithColors(CGColorSpaceCreateDeviceRGB(), gradColors, colorLocations) { set1.fill = ChartFill(linearGradient: gradient, angle: 90.0) } 

请务必使用最新版本的ios-charts。 如果您使用CocoaPods来安装ios-charts,请更改:

pod 'Charts'

对此

 pod 'Charts', '~> 2.2'