Login | Register

Pie chart

September 4, 2006, 17:57 by Calle
[loading]
-->
Pie charts are more complicated than other charts, mainly because there are now draw pie function, so you have to do something else instead. There are several ways I suppose but this was the one I came up with. Not perfect, although it works. The logic is simple; as there are 360 degrees I draw 360 lines, one for each degree. This is how the code looks:

<div style="clear: both;"> value[0] = 50;
value[1] = 50;
value[2] = 50;
color[0] = c_red;
color[1] = c_blue;
color[2] = c_green;

tot = 3; // The total number of different values
b= 0;
i = 0;
c = 0;
total = 0;

circle_radius = 100; // The size of the circle
circle_x = room_width/2-circle_radius/2; //Concentrates it in the middle
circle_y = room_height/2-circle_radius/2;

draw_set_color(c_lime); //Look at the picture further down at this page
draw_circle(circle_x,circle_y,circle_radius,false); //Background circle

i= 0;

while(i<tot) {
total +=value[i]; //Calculates the total value
i+=1;
}

i=0;
while(i<tot) //Goes through all different values
{
percentage = value[i]/total*360; //Evaluates the numbers of lines
draw_set_color(color[i]); //drawn in the specific colour
b=0;
while(b<percentage)
{
draw_line(circle_x,circle_y,circle_x+lengthdir_x(circle_radius,b+c),
circle_y+lengthdir_y(circle_radius,b+c));
b+=1;
}
c+=value[i]/total*360;
i+=1;
}
</div>

I tried to comment it so that you should understand the most. However, this has, as you might have understood, a glitch. When the lines get too far from the centre of the circle, space is created between them. To cover up this space you would have to draw multiple lines within the same degree with only small differences in their target positions. Anyway, I didn’t do this, here is how it currently look:
<img src="http://gmtutorials.com/files/piechart.png">

If you feel there is something you need explained, please ask by commenting. I also include an example: http://gmtutorials.com/files/piechart.gm6

Comments

Loading comments... [loading]
.
Users logged in:

game maker articles, game maker examples, game maker tutorials, gmtutorials, game maker questions and answers, game maker crash course, how to create games