October 28, 2020, 10:39:04 PM

Author Topic: [bb] Pie chart code by Ross C [ 1+ years ago ]  (Read 1611 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Pie chart code by Ross C [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : Pie chart code
Author : Ross C
Posted : 1+ years ago

Description : 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.

Code :
Code: BlitzBasic
  1. Graphics 640,480
  2. SetBuffer BackBuffer()
  3. SeedRnd MilliSecs()
  4.  
  5. Type segment
  6.    Field angle#
  7.    Field value#
  8. End Type
  9.  
  10. ; just some sizes for the pie chart, based on the graphics resolution, so it will scale ok.
  11. Global pie_chart_start_x# = GraphicsWidth()/2
  12. Global pie_chart_start_y# = GraphicsHeight()/2
  13.  
  14. Global pie_chart_size# = GraphicsHeight()/2.2
  15. Global segment_text_position# = 0.75 ; a value between 0 and 1: 0 = at the middle. 1 = at the edge.
  16.  
  17. Global total_value ; this is calculated below, when generating the type object segments.
  18.  
  19. ; create 6 chart segments
  20. For loop = 1 To 6
  21.         s.segment = New segment
  22.         svalue# = Rand(10,100)
  23.         total_value = total_value + svalue
  24. Next
  25.  
  26. While Not KeyHit(1)
  27.  
  28.         Cls
  29.  
  30.         draw_pie_chart()
  31.  
  32.         Flip
  33. Wend
  34.  
  35. Function draw_pie_chart()
  36.  
  37.     ; draw the circle shape of the pie chart. use the start X and Y and the pie chart size here.
  38.         Oval pie_chart_start_x - pie_chart_size, pie_chart_start_y - pie_chart_size,pie_chart_size*2,pie_chart_size*2,0
  39.  
  40.         Local angle# = 90 ; set this to 90 to overcome blitz rotation system, so it start vertically.
  41.         Local angle_gap# ; used to work out the angle size, based on the value in the type object.
  42.                          ; as always: (value / total value)*100 gives you a percentage.
  43.         Local text_angle#; i take half of the angle_gap and use this angle to place the visual value.
  44.  
  45.         ; draw initial line
  46.         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)
  47.         ; draw the total value
  48.         Text pie_chart_start_x, pie_chart_start_y - (pie_chart_size*1.05), total_value,True,True
  49.  
  50.         For s.segment = Each segment
  51.                
  52.                 angle_gap = 360 * (svalue / total_value)
  53.                 text_angle = angle_gap/2
  54.                 angle = angle + angle_gap
  55.                
  56.                 ; BELOW: draw a line to show the segment.
  57.                 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)
  58.                 ; BELOW: place the visual value of the type object, halfway between the centre and circumfirance.
  59.                 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)),svalue,True,True
  60.  
  61.                
  62.         Next
  63.        
  64. End Function


Comments :


Ross C(Posted 1+ years ago)

 Would be nice to codebox that :o)


Ross C(Posted 1+ years ago)

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


Warpy(Posted 1+ years ago)

 You're cracking up, Ross.


Ross C(Posted 1+ years ago)

 That's comforting coming from you ;o)


Adam Novagen(Posted 1+ years ago)

 Tastes great, but no filling. XDSeriously though, this'd be a whole lot cooler if it actually filled the individual regions of the chart.


Ross C(Posted 1+ years ago)

 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 C(Posted 1+ years ago)

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


DheDarkhCustard(Posted 1+ years ago)

 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.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal