Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply gradient to bar in bar chart in ios-Charts

Is there any way to fill the bar in a bar chart with a gradient. I can make the gradient like this as explained in the link below:

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

but how to apply it on the bars?

Previous question Gradient Fill in Swift for iOS-Charts does not answer it because .fill is not available on BarChartDataSet

like image 573
Kashif Avatar asked Nov 05 '18 04:11

Kashif


1 Answers

I assume that you are using Daniel Cohen Gindi's Charts library (formerly known as ios-charts but now supporting iOS, macOS and tvOS).

The library does not support bar charts with gradients yet even though it's a popular request. You can up vote the request and hope it will be implemented soon.

Having said that there's also a pull request that implements the feature. Sadly, it has not yet been merged as changes have been requested and there seem to be other open issues.

You can still try to clone the repository and use the pull request. The open issues might not affect you.

If you're successful, bar charts with gradients are as easy as:

dataSet.barGradientColors = [[color1, color2], [color3, color4]]
like image 184
Codo Avatar answered Nov 14 '22 21:11

Codo