Hi, I'm trying to add a gradient to my bar charts but I can only draw the first bar:

I added the gradient at _BarChartRenderer->drawDataSet_ right after if _!isSingleColor{}_
let fillColors = [UIColor.white.cgColor, UIColor.yellow.cgColor]
let locations:[CGFloat] = [0.0, 1.0]
context.clip(to: barRect)
let gradient:CGGradient
let colorspace:CGColorSpace
colorspace = CGColorSpaceCreateDeviceRGB()
gradient = CGGradient(colorsSpace: colorspace, colors: fillColors as CFArray, locations: locations)!
//Vertical Gradient
let startPoint:CGPoint = CGPoint(x: 0.0, y: viewPortHandler.contentBottom)
let endPoint:CGPoint = CGPoint(x: 0.0, y: viewPortHandler.contentTop)
context.drawLinearGradient(gradient, start: startPoint, end: endPoint, options: .init(rawValue: 0))
why can't you apply the code to other bars? What's the issue?
That's the problem, i don't know why it's only taking effect to the first bar, in the image i should have around 16 bars but it looks like the others are transparent
how about you remove the gradient? All bars appear? Maybe better ask on SO...
Also maybe check the context state?
yes, if I remove the gradient all the bars appear normally
I've added an if(j==10){} to draw the gradient at the bar 11 and it looks like this:

I should have 28 bars but it stops at the 11 bar
it looks like a CoreGraphics issue to me. Have you tried to save/clean the state every time you want to render a bar? I don't know the reason yet..
BTW, I just came across https://github.com/danielgindi/Charts/issues/2515, when someone us using CALayer to draw gradient, and it works though.. CoreGraphics is underlying CALayer gradient so I guess you can refer something, or at least a work around.
I played with save/restore gState as mentioned before and now it works!Hope, this will help someone, thank you guys.
if !isSingleColor
{
let fillColors = [dataSet.color(atIndex: 0).cgColor, dataSet.color(atIndex: 1).cgColor]
let locations:[CGFloat] = [0.0, 1.0]
context.saveGState()
context.clip(to: barRect)
let gradient:CGGradient
let colorspace:CGColorSpace
colorspace = CGColorSpaceCreateDeviceRGB()
gradient = CGGradient(colorsSpace: colorspace, colors: fillColors as CFArray, locations: locations)!
//Vertical Gradient
let startPoint:CGPoint = CGPoint(x: 0.0, y: viewPortHandler.contentBottom)
let endPoint:CGPoint = CGPoint(x: 0.0, y: viewPortHandler.contentTop)
context.drawLinearGradient(gradient, start: startPoint, end: endPoint, options: .init(rawValue: 0))
context.restoreGState()
} else {
context.fill(barRect)
}
@sokalyambda thank you, it works!
@sokalyambda thx, it's really works
Most helpful comment
I played with save/restore gState as mentioned before and now it works!Hope, this will help someone, thank you guys.
if !isSingleColor
{
let fillColors = [dataSet.color(atIndex: 0).cgColor, dataSet.color(atIndex: 1).cgColor]
let locations:[CGFloat] = [0.0, 1.0]