Friday, September 27, 2013

Simple pie chart with Qbasic/Freebasic.

Just something to play with.




DIM Angle(1)
DIM MyData(0 TO 4)
DIM C(1 TO 4)

Pi = 4 * ATN(1)                                                   '  Can use 3.141593

Radius = 200                                                      '  Change these to suit your taste 
Xc = 319                                                          '  x coordinate of center of circle
Yc = 239                                                          '  y coordinate of center of circle

FOR i = 1 TO 4                                                    '  puts data in array,
  READ MyData(i)                                                  '  with the sum in  MyData(0)
  MyData(0) = MyData(0) + MyData(i)                               '
  READ C(i)                                                       '  sets colors
NEXT                                                              '

 ' Put your data here:  (use  Value, Color)
DATA 23,1                                                       
DATA 174,2
Data 17,3
DATA 48,4

SCREEN 12                                                          ' graphics mode with ok resolution

Angle(0) = 0                                                       ' Set initial angle

FOR i = 1 TO 4
  IF i = 4 THEN                                                    ' Set final angle of wedge
    Angle(1) = 2 * Pi
  ELSE
    Angle(1) = Angle(0) + 2 * Pi * MyData(i) / MyData(0)
  END IF

  FOR j = 0 TO 1   ''' Draw sides of wedges.  Neg sign on Radius because CIRCLE flips y coordinates
    LINE (Xc, Yc)-STEP(Radius * COS(Angle(j)), -Radius * SIN(Angle(j))), C(i)     
  NEXT                                                                            

  CIRCLE (Xc, Yc), Radius, C(i), Angle(0), Angle(1)                ' Draw arc

  AvgAngle = (Angle(1) + Angle(0)) / 2                             ' find middle of wedge and paint
  PAINT (Xc + Radius * COS(AvgAngle) / 2, Yc - Radius * SIN(AvgAngle) / 2), C(i)  

  Angle(0) = Angle(1)                                              ' update initial angle
NEXT
Input "",a

No comments:

Post a Comment