Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradient Fill in Swift for iOS-Charts

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

like image 569
throb Avatar asked Feb 01 '16 14:02

throb


2 Answers

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

like image 192
Michael Avatar answered Oct 20 '22 01:10

Michael


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)
like image 43
Hardik Thakkar Avatar answered Oct 19 '22 23:10

Hardik Thakkar