user5631
user5631

Reputation: 35

Gradient colors based on values for a bar graph

I need to define colors for graph based on the values(example : green for values between 0-50 and red for values between 50-100).I tried with gradient colors but it is not as expected. Could anyone help me out in this please? I'm using Swift 3.

I need the bottom of the graph to be green and upper part as red

Upvotes: 0

Views: 656

Answers (1)

Ravi Padsala
Ravi Padsala

Reputation: 126

Declare out of class for this code

class Colors {
    var gl:CAGradientLayer!

    init() {
        let colorTop =  UIColor(red: 135.0/255.0, green: 155.0/255.0, blue: 36.0/255.0, alpha: 1.0).cgColor
        let colorcenter = UIColor(red: 186.0/255.0, green: 213.0/255.0, blue: 49.0/255.0, alpha: 1.0).cgColor
        let colorBottom = UIColor(red: 224.0/255.0, green: 241.0/255.0, blue: 142.0/255.0, alpha: 1.0).cgColor

        self.gl = CAGradientLayer()
        self.gl.colors = [colorTop, colorcenter, colorBottom]
        self.gl.locations = [0.5,0.8, 1.0]
    }
}

declate in viewDidLoad Method.

let colors = Colors()
view.backgroundColor = UIColor.clear
let backgroundLayer = colors.gl
backgroundLayer?.frame = Yourview.frame
Yourview.layer.insertSublayer(backgroundLayer!, at: 0)

Upvotes: 1

Related Questions