Code archives/Graphics/Pie chart code

This code has been declared by its author to be Public Domain code.

Download source code

Pie chart code by Ross C2008
This code will create a pie chart, based on the number of entries in a type collection. It is coded in such a way, that it's size, text positions etc, are all easily changable. It currently will scale to fit the screen resolution.
Graphics 640,480
SetBuffer BackBuffer()
SeedRnd MilliSecs()

Type segment
   Field angle#
   Field value#
End Type

; just some sizes for the pie chart, based on the graphics resolution, so it will scale ok.
Global pie_chart_start_x# = GraphicsWidth()/2
Global pie_chart_start_y# = GraphicsHeight()/2

Global pie_chart_size# = GraphicsHeight()/2.2
Global segment_text_position# = 0.75 ; a value between 0 and 1: 0 = at the middle. 1 = at the edge.

Global total_value ; this is calculated below, when generating the type object segments.

; create 6 chart segments
For loop = 1 To 6
	s.segment = New segment
	s\value# = Rand(10,100)
	total_value = total_value + s\value
Next

While Not KeyHit(1)

	Cls

	draw_pie_chart()

	Flip
Wend

Function draw_pie_chart()

    ; draw the circle shape of the pie chart. use the start X and Y and the pie chart size here.
	Oval pie_chart_start_x - pie_chart_size, pie_chart_start_y - pie_chart_size,pie_chart_size*2,pie_chart_size*2,0

	Local angle# = 90 ; set this to 90 to overcome blitz rotation system, so it start vertically.
	Local angle_gap# ; used to work out the angle size, based on the value in the type object.
	                 ; as always: (value / total value)*100 gives you a percentage.
	Local text_angle#; i take half of the angle_gap and use this angle to place the visual value.

	; draw initial line
	Line pie_chart_start_x, pie_chart_start_y, pie_chart_start_x - (Cos(angle)*pie_chart_size), pie_chart_start_y - (Sin(angle)*pie_chart_size)
	; draw the total value
	Text pie_chart_start_x, pie_chart_start_y - (pie_chart_size*1.05), total_value,True,True

	For s.segment = Each segment
		
		angle_gap = 360 * (s\value / total_value)
		text_angle = angle_gap/2
		angle = angle + angle_gap
		
		; BELOW: draw a line to show the segment.
		Line pie_chart_start_x, pie_chart_start_y, pie_chart_start_x - (Cos(angle)*pie_chart_size), pie_chart_start_y - (Sin(angle)*pie_chart_size)
		; BELOW: place the visual value of the type object, halfway between the centre and circumfirance.
		Text pie_chart_start_x - (Cos(angle - text_angle)*(pie_chart_size*segment_text_position)), pie_chart_start_y - (Sin(angle - text_angle)*(pie_chart_size*segment_text_position)),s\value,True,True

		
	Next
	
End Function

Comments

Ross C2008
Would be nice to codebox that :o)


Ross C2009
Why have i commented on my own code... Odd.


Warpy2009
You're cracking up, Ross.


Ross C2009
That's comforting coming from you ;o)


Adam Novagen2009
Tastes great, but no filling. XD

Seriously though, this'd be a whole lot cooler if it actually filled the individual regions of the chart.


Ross C2009
There is a fill command in the archives, although, i could just use the line command at small intervals to fill. I will update it!


Ross C2009
Scratch that, too much work for me as i'm in the middle of summit else :P


DheDarkhCustard2009
Nice example. It would be good with colour though. draw it once on an image buffer and draw the image. That way it won't slow down the process.


Code Archives Forum