Reputation: 4632
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
Upvotes: 6
Views: 3296
Reputation: 78795
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]]
Upvotes: 5