Qbasicnews.com

Full Version: Creating a pie chart.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everybody, this is my first post so it may be a little lame but I need some help. I have never really tried to use graphics in QBASIC before, but I am currently working on a survey for my science project, and am in need of some directions on how to create a pie chart. I have tried repeatably, even checking a book out from the library and trying to follow by example, but I am still having some troubles. I am not understanding the concept at all. What I am trying to do is use for numerical values; "a", "b", "c", and "d". I am trying to design the algorithm so that the pie chart can vary according to the data that I am inputting, and a pie chart would really get the judges attention. I can get the data into percentages, and even to the point of an angle and that is where I get stuck, so if anyone could help, I would be very grateful.
well, you can find the endpoint of a line in percent by doing this I think (untested code)
Code:
CONST PI = 3.14159265358979323, PI2 = PI*2
percent = 40
percRad = percent/PI2
x = cos(percRad)
y = sin(percRad)

I dunno, I haven't learned trig yet...
Code:
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
I was making a pie chart function for StatLib... it fixes the problem you have when a wedge is over 50% in size, and exactly 50% in size... you have to do a select case on that but otherwise SCM's routine is fine.

(Note to SCM: set a minimum radius etc and make it into a function, for portability Smile)
Oracle,
About making it a function: it would be better, but i just hacked it out quickly.

About The cases of exactly 50% and over 50%: I think this handles those cases fine. If you are concerned about painting the wrong area, using AvgAngle and dividing the COS and SIN terms by 2 assures my Paint point is in the middle of the sector.
Code:
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)

The one place where it is weak, is using PAINT for small percentages (It can leave isolated pixels unpainted). This can be easily fixed by extending the wedges to the next value of Pi/2. That way there wouldn't be any isolated pixels to skip, and the excess area will be painted over by the next wedge. If anyone is interested I will update the program (or function).
Each slice is defined by two lines and a circle. So!

Code:
for y% = 1 to bla%
for x% = 1 to bla%
if in.circle% then
pset slice.color(slice.in%(x%,y%))
next slice%
end if
next x%, y%

function slice.in%(x%, y%)
'transform circle into square, get Y position on square....
'blablabla, get slice%
end function

function in.circle%

'blablabla....0 or 1...
end function
I don't quite get why, but it ain't running for me in QB4.5 :???:

Have you tested it with over 50%?
It tested fine at and over 50%, but when I posted it here, I edited the comments, putting comments at the end of data lines. When I copied my posted program and tried to run it, I got errors. Removing the comments on the data lines fixed that problem, and It again works fine (the code above has been fixed).

LESSON: No comments on data lines.

(I don't know the differences between 4.5 and 7.1, but I don't think I used any commands specific to 7.1)
Aah. Working now Smile