MPAndroidChart icon indicating copy to clipboard operation
MPAndroidChart copied to clipboard

Rounded bar chart with customize background

Open sbron opened this issue 11 years ago • 21 comments

I would like add rounded corners on bar chart and add a customized background. In Android, I know I can create rounded corner for rectangle on a drawable but this library doesn't support "barchart.setBackground(R.drawable.my_custom_back);" It is possible to add this function? Or, may be, is there an another solution?

sbron avatar Jun 24 '15 14:06 sbron

http://stackoverflow.com/questions/11947603/setbackground-vs-setbackgrounddrawable-android

PhilJay avatar Jun 25 '15 06:06 PhilJay

Sorry, it's my fault. I would like create rounded BAR!

sbron avatar Jun 26 '15 06:06 sbron

@sbron Here's what I did to accomplish what you're looking for...

  1. Make custom render Class that extends BarChartRenderer
  2. Override method drawDataSet (easiest approach is to copy over drawDataSet method code from BarChartRenderer.java)
  3. Change occurrences of "drawRect" to "drawRoundedRect"
  4. Set the renderer to your bar chart...
    barChart.setRenderer(new MyRenderer(barChart, barChart.getAnimator(), barChart.getViewPortHandler()));

You can also add any other customizations for drawing the bars in that method. Good luck!

alliecurry avatar Jul 24 '15 19:07 alliecurry

@alliecurry Ty it works! There so much to account for though.

Like If you round the rect, it rounds all 4 corners. If I want to round just the top 2 corners, a solution might be to draw a regular square rect on bottom half and have it overlap the rounded rect.

Then you have to account for the highlight color which is a completely separate bar that is overlayed on top the regular bar. That also needs to be rounded. The highlight color has some transparency to it so if you draw a square rect on bottom of the rounded highlight bar, the bottom part will be visibly darker than the top half.

I'll just stick with the square bar, imo it looks a lot more cleaner anyways.

marcochin avatar Jan 31 '16 11:01 marcochin

@alliecurry I'm using method you described. But for some reason I get only top corners rounded. Is there a reason for that?

 fun drawRoundRect(canvas: Canvas, left: Float, top: Float, right: Float, bottom: Float) {
    val rad = (right - left) / 2
    canvas.drawRoundRect(left, top, right, bottom, rad, rad, mRenderPaint)
}

mjurkus avatar Apr 22 '16 09:04 mjurkus

@alliecurry, thanks for the tip. I got it to work here.

ifeomaro avatar Aug 17 '16 15:08 ifeomaro

i need to add round corner to radar chat in Xamarin. pls help

sridharsaminathan avatar Mar 27 '17 13:03 sridharsaminathan

Is there a 2018 in-library solution for this?

Aditya94A avatar Jun 27 '18 04:06 Aditya94A

^

  • Wrong language
  • Wrong library

... @idofle?

Aditya94A avatar Jul 14 '18 07:07 Aditya94A

@AdityaAnand1 have you solve this issues and make a success to build a rounded bar charts?

titiarimba avatar Nov 22 '18 04:11 titiarimba

@alliecurry I'm using method you described. But for some reason I get only top corners rounded. Is there a reason for that?

 fun drawRoundRect(canvas: Canvas, left: Float, top: Float, right: Float, bottom: Float) {
    val rad = (right - left) / 2
    canvas.drawRoundRect(left, top, right, bottom, rad, rad, mRenderPaint)
}

I am also getting only top corners to be round and I need also bottom corners to be round. Any one has accomplished having all 4 corners round in the barchart?

For rounding the top corners I followed this link: https://stackoverflow.com/questions/30761082/mpandroidchart-round-edged-bar-chart/

Dansanchez avatar Dec 03 '18 19:12 Dansanchez

Hey everyone I'm trying to achieve this: chart

And this is what i've done so far: screenshot_20181226-153130

Now i need to overlap first value bar to the next one and so on how can i do this please help

tabish075 avatar Dec 26 '18 09:12 tabish075

@tabish075 i'm also running on same issue. have you got a solution

Hashan1599 avatar Apr 08 '19 06:04 Hashan1599

@Hashan1599 Not with this library.. no

tabish075 avatar Apr 08 '19 07:04 tabish075

@tabish075 how can i get rid of this issue. what library should i want to tryout

Hashan1599 avatar Apr 08 '19 10:04 Hashan1599

I'll make a gist and share soon !

tabish075 avatar Apr 08 '19 10:04 tabish075

Hi, for stacked bar chart radius https://gist.github.com/timtsj/28e0da04b2037d7988e266636e10a277

timtsj avatar Oct 05 '20 03:10 timtsj

How to draw rounded corners on both top and bottom?

anmol-max avatar Oct 22 '21 10:10 anmol-max

@anmol-max Dansanchez any update on it? I am also trying to achieve the same bar chart with all the borders rounded

Priyanshi-Z avatar Mar 13 '22 20:03 Priyanshi-Z

welcome to view this post https://juejin.cn/post/7100863388850503693

gs666 avatar May 23 '22 09:05 gs666