14

I am trying to create a nice gradient fill as seen in the demos on the ios-charts page. However, I cannot figure this out in swift vs obj-c. Here is the obj-c code:

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];

Now here is my version of that in 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)

What I am missing is the : Chart Fill fillWithLinearGradient

I can't find the fillWithLinearGradient in the pod anywhere so I am a bit confused.

Thanks in advance for any help! Rob

throb
  • 185
  • 1
  • 1
  • 6
  • Check out the implementation of ChartFill: https://github.com/danielgindi/ios-charts/blob/607c4515d92b9fa5b15192981c3cc4f8bb38f01b/Charts/Classes/Utils/ChartFill.swift There's an init that accepts a linearGradient and angle. I believe @matt's answer to be correct. – Stephen Feb 01 '16 at 14:57
  • @Stephen, in the LineChartDataSet class there is no .fill. However there is a fillFormatter but clearly in my noobish state, I am not sure how to make that work. – throb Feb 01 '16 at 16:23
  • `LineChartDataSet` is a subclass of `LineRadarChartDataSet`. `LineRadarChartDataSet` does have a public fill variable, so it should be available to you. – Stephen Feb 01 '16 at 20:54
  • Hi, sorry I have the same question and in your code what does set1 stand for? – VAAA May 10 '16 at 01:05

6 Answers6

48

This works perfectly (Swift 3.1 and 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

Result

enter image description here

Michael
  • 9,639
  • 3
  • 64
  • 69
8

For Swift 5 use the following:

yourDataSetName.fill = LinearGradientFill(....
or
yourDataSetName.fill = ColorFill(...

More details you can read here: Charts

Ihor Chernysh
  • 446
  • 4
  • 5
6

I believe this is the code that you are looking for. You should be able to use the same ChartFill class in Swift to set 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)
}
Stephen
  • 1,154
  • 11
  • 27
3

For swift4

let colorTop =  UIColor(red: 255.0/255.0, green: 149.0/255.0, blue: 0.0/255.0, alpha: 1.0).cgColor
let colorBottom = UIColor(red: 255.0/255.0, green: 94.0/255.0, blue: 58.0/255.0, alpha: 1.0).cgColor

let gradientColors = [colorTop, colorBottom] as CFArray
let colorLocations:[CGFloat] = [0.0, 1.0]
let gradient = CGGradient.init(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: gradientColors, locations: colorLocations) // Gradient Object
yourDataSetName.fill = Fill.fillWithLinearGradient(gradient!, angle: 90.0)
Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81
2

Fill.fillWithLinearGradiant has been updated to LinearGradientFill

Muhammad Aakif
  • 193
  • 2
  • 5
0

Make sure to use the latest version of ios-charts. If you use CocoaPods to install ios-charts change this:

pod 'Charts'

to this

pod 'Charts', '~> 2.2'
cronenberg
  • 97
  • 1
  • 9
  • I just had to wait for that rev I guess :) I still don't get the gradient fill but at least the error doesn't rear its ugly head. Thank you. – throb Feb 25 '16 at 16:21
  • 1
    @throb you probably have solved it already, but just in case: I copy pasted your code and didn't get gradient either, and turns out the issue was that the second color was not sent in as .CGColor. Adding this solved it for me. let gradColors = [UIColor.cyanColor().CGColor, UIColor.init(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0).CGColor] – ClockWise Mar 11 '16 at 06:08
  • Sweet! Thanks! I ended up not using the gradient but I will now :) Cheers!! – throb Mar 11 '16 at 15:22