Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » generating a pie chart

This thread is locked; no one can reply to it. rss feed Print
generating a pie chart
shadyvillian
Member #12,426
December 2010

I'm trying to generate a pie chart using only allegro 5 primitives. Each "slice" of the pie is a different color. I was going to make triangles for each slice then put a circle behind it to make it look rounded. But I'm having trouble calculating the x and y positions of the triangles.

Software Engineer by day, hacker by night.

J-Gamer
Member #12,491
January 2011
avatar

I assume your slices store the percentage they have. So you can iterate over all the slices, multiply the percentage by 2*PI and you have the angle range. Then, just iterate over the slices and keep adding each range while using sin/cos*radius to get the y/x coordinates.

" There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo
"If your body was a business, thought would be like micro-management and emotions would be like macro-management. If you primarily live your life with emotions, then you are prone to error on the details. If you over-think things all the time you tend to lose scope of priorities." - Mark Oates

james_lohr
Member #1,947
February 2002

I was going to make triangles for each slice then put a circle behind it to make it look rounded

I don't see how this could work, unless you're planning to have a very bumpy pie.

SiegeLord
Member #7,827
October 2006
avatar

Untested:

float percentages[] = {10, 20, 40, 30};
size_t num_pieces = 4;
float cur_theta = 0;
float cx = 200, cy = 200, r = 100;
ALLEGRO_COLOR colors[] = {...}; 

for(size_t ii = 0; ii < num_pieces; ii++)
{
    float delta_theta = percentages[ii] / 100.0 * 2.0 * ALLEGRO_PI;
    al_draw_arc(cx, cy, r / 2, cur_theta, delta_theta, color[ii], r / 2);
    cur_theta += delta_theta;
}

You can also swipe the al_draw_filled_pieslice from the 5.1 branch, which you would use instead of the al_draw_arc. One bonus of using al_draw_arc here, though, is that you can make those ring charts instead of simple pie charts.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

shadyvillian
Member #12,426
December 2010

SiegeLord said:

You can also swipe the al_draw_filled_pieslice from the 5.1 branch

Wheres the 5.1 branch?

EDIT: I think I found it but I'm guessing I have to build it myself.

Software Engineer by day, hacker by night.

SiegeLord
Member #7,827
October 2006
avatar

Well, the question to ask is why my solution isn't acceptable for you. It's only issue is that it draws 2x more polygons than it should. Either way, instead of building the entire 5.1 I'd just go here and copy-paste al_draw_filled_pieslice for your own use.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

shadyvillian
Member #12,426
December 2010

Oh, I was just curious. I'm testing it right now, but theres a bug my data analyzer so i need to get that working right to see what my results are.

EDIT: ok I got it working everything looks good.
EDIT2: would anti-aliasing help the edge of the circle look smoother?

Software Engineer by day, hacker by night.

Go to: