Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gradient to bar chart #2722

Closed
gustavo-yy opened this issue Aug 15, 2017 · 10 comments
Closed

Add gradient to bar chart #2722

gustavo-yy opened this issue Aug 15, 2017 · 10 comments

Comments

@gustavo-yy
Copy link

gustavo-yy commented Aug 15, 2017

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

captura de pantalla 2017-08-15 a la s 17 35 25

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))
@liuxuan30
Copy link
Member

why can't you apply the code to other bars? What's the issue?

@gustavo-yy
Copy link
Author

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

@liuxuan30
Copy link
Member

how about you remove the gradient? All bars appear? Maybe better ask on SO...
Also maybe check the context state?

@gustavo-yy
Copy link
Author

gustavo-yy commented Aug 23, 2017

yes, if I remove the gradient all the bars appear normally

@gustavo-yy
Copy link
Author

I've added an if(j==10){} to draw the gradient at the bar 11 and it looks like this:
captura de pantalla 2017-08-28 a la s 12 37 51

I should have 28 bars but it stops at the 11 bar

@liuxuan30
Copy link
Member

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..

@liuxuan30
Copy link
Member

BTW, I just came across #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.

@sokalyambda
Copy link

sokalyambda commented Aug 31, 2017

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)
        }

@gustavo-yy
Copy link
Author

@sokalyambda thank you, it works!

@alxarsnk
Copy link

@sokalyambda thx, it's really works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants