Charts: Rounding the corners of bars

Created on 24 May 2016  ·  21Comments  ·  Source: danielgindi/Charts

screen shot 2016-05-23 at 17 58 03

What would be the best way to achieve this effect? I’m guessing I’ll need to modify the way Charts draws in some way.

feature

Most helpful comment

That's what I investigated before, you can try first. I am still looking for other options.

in public func drawDataSet: of bar chart renderer

                let cornerRadius: CGFloat = CGRectGetWidth(barRect) <= 5 ? 1.0 : 2.0
                let bezierPath = UIBezierPath(roundedRect: barRect, byRoundingCorners: UIRectCorner.AllCorners, cornerRadii: CGSizeMake(cornerRadius, cornerRadius))

                let roundedPath = bezierPath.CGPath

                // if drawing the bar shadow is enabled
                if (drawBarShadowEnabled)
                {
                    barShadow.origin.x = barRect.origin.x
                    barShadow.origin.y = viewPortHandler.contentTop
                    barShadow.size.width = barRect.size.width
                    barShadow.size.height = viewPortHandler.contentHeight

                    CGContextSetFillColorWithColor(context, dataSet.barShadowColor.CGColor)
//                    CGContextFillRect(context, barShadow)
                    CGContextAddPath(context, roundedPath)
                    CGContextFillPath(context)
                }

                // Set the color for the currently drawn value. If the index is out of bounds, reuse colors.
                CGContextSetFillColorWithColor(context, dataSet.colorAt(j).CGColor)
//                CGContextFillRect(context, barRect)
                CGContextAddPath(context, roundedPath)
                CGContextFillPath(context)

All 21 comments

I have been investigated on this a few months ago. It's possible, however, we need to change how we create the bar rect. Not so hard, but some decent work is needed

That's what I investigated before, you can try first. I am still looking for other options.

in public func drawDataSet: of bar chart renderer

                let cornerRadius: CGFloat = CGRectGetWidth(barRect) <= 5 ? 1.0 : 2.0
                let bezierPath = UIBezierPath(roundedRect: barRect, byRoundingCorners: UIRectCorner.AllCorners, cornerRadii: CGSizeMake(cornerRadius, cornerRadius))

                let roundedPath = bezierPath.CGPath

                // if drawing the bar shadow is enabled
                if (drawBarShadowEnabled)
                {
                    barShadow.origin.x = barRect.origin.x
                    barShadow.origin.y = viewPortHandler.contentTop
                    barShadow.size.width = barRect.size.width
                    barShadow.size.height = viewPortHandler.contentHeight

                    CGContextSetFillColorWithColor(context, dataSet.barShadowColor.CGColor)
//                    CGContextFillRect(context, barShadow)
                    CGContextAddPath(context, roundedPath)
                    CGContextFillPath(context)
                }

                // Set the color for the currently drawn value. If the index is out of bounds, reuse colors.
                CGContextSetFillColorWithColor(context, dataSet.colorAt(j).CGColor)
//                CGContextFillRect(context, barRect)
                CGContextAddPath(context, roundedPath)
                CGContextFillPath(context)

Thanks so much for the feedback! I’ll play with this.

yeah I don't think it's a great one, especially your bars are very thin

its not working can anyone help me to do it ?

@liuxuan30请问下,这个在最新的版本还可以?

Thanks for your dearness

Sent from my iPhone

On Dec 27, 2016, at 15:30, CoderZyc notifications@github.com wrote:

@liuxuan30请问下,这个在最新的版本还可以?


You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.

@CoderZyc not merged yet. there is a PR for it.

Hey @liuxuan30 this looks awesome, any ETA on this?

not really. I don't think it's perfect, and not having time to investigate other options.

@liuxuan30 no worries, there is another solution that worked out nicely #1917

@acegreen basically they work the same way. Just wrap up nicely. Since more people asking, I am reopening this topic

Hi,
Can you please help me with adding vertical dashed lines in between bars in bar graph and making the top corner of the bar slightly curvy . I am uploading the screenshot for the sample.
screenshot_2018-09-06 zeplin - project

Any update on this?

It looks like this and a similar pull request - https://github.com/danielgindi/Charts/pull/1917 - have been dead for at least 2 years. Is it worth throwing away all the work they took?

Is there a way to achieve the same with the slices of a piechart?

+1

+1

If #4297 was merged, you could just override the renderer.

Not merged :/

Recently I have been working on a project, to which I have added a chart library and display a beautiful rounded bar chart in swift. We can achieve this functionality easily. Please check this demo.
Screenshot 2021-03-27 at 7 42 42 PM

Was this page helpful?
0 / 5 - 0 ratings