My need is to show 3-colored distribution in a view. So I'm using following code.
func drawWithGradientLayer() {
// total contains sum of all values contained in segmentValues variable.
// I'm using green -> Orange -> Red colors as colors
if total == 0 {
return
}
if gradientLayer.superlayer != nil {
gradientLayer.removeFromSuperlayer()
}
var previous: CGFloat = (CGFloat(segmentValues[0])/CGFloat(total))
var locations: [NSNumber] = [previous]
for var i=1; i<segmentValues.count; i++ {
previous = previous + (CGFloat(segmentValues[i])/CGFloat(total))
locations.append(previous)
}
locations.append(1.0) // Line may need to be commented
gradientLayer = CAGradientLayer()
gradientLayer.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)
gradientLayer.startPoint = CGPointMake(0.0, 0.5);
gradientLayer.endPoint = CGPointMake(1.0, 0.5);
gradientLayer.colors = colors
gradientLayer.locations = locations
self.layer.insertSublayer(gradientLayer, atIndex: 0)
}
But problem is that gradient colors are not distributed according to my requirements. E.g if ihave values of [1,0,1] so bar should display 50% red & 0% orange and 50% red. If values are [0,0,1] then only redColor should be drawn. How can I achieve this without grid lines?
any other better solution will always be appreciated :)