RAAAAM
RAAAAM

Reputation: 3380

How to draw a bar diagram on Android

I need to draw a bar diagram like the picture showing below. I am able to draw a ordinary bar diagram with filling a color on text view or some layouts. But this is a bit different. How do I draw a bar diagram with slanting bars?

Enter image description here.

Upvotes: 2

Views: 1995

Answers (2)

shijin
shijin

Reputation: 3046

Do something like this in your onDraw of View.

RectF oval = new RectF();
Paint paint  = new Paint();
paint.setColor(ContextCompat.getColor(context, R.color.color1));
paint.setStrokeWidth(widthOfArc);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
canvas.drawPath(path, yourPaint);
oval.set(x1,y1,x2,y2);
//eg:-  startAngle = 10, sweepAngle = 40
canvas.drawArc(oval, startAngle, sweepAngle, false, paint);

Please read about how RectF works. I will update the answer if I found any related blog/tutorial

Calculating start and end angle is pure Mathematics refer this http://www.html5canvastutorials.com/tutorials/html5-canvas-arcs/

You have to change the color and keep drawing Arc to get the result.

Upvotes: 0

nurisezgin
nurisezgin

Reputation: 1570

Try Android canvas and custom View. You can use View's onDraw method. The method provides the canvas. You should try drawArc, drawLine, etc. If your bar view contains a textView, you must use a custom viewgroup.

Upvotes: 1

Related Questions