// Draw pie chart

GRAPH landscape, hscale=15, vscale=15,noaxes,nogrid
// Make hscale and vscale the same for correct aspect
// Don't need axis and grid for pie chart

DATA Food, 7531, Mortgage,15395, Util., 2995, Taxes, 8250
DATA Car, 5004, Fun, 9083, Misc, 7050  

DIM aa(30), aa$(30) 
degrees  // do all angle calculations in degrees
print

// and determine the scale factor for the segments
total = 0
i = 0
while read(0) <> 0    // stop when no more data
  read aa$(i), aa(i)
  print  using "###,###"; aa$(i), aa(i)
  total = total + aa(i)
  i = i + 1
wend

// Draw the pie chart

plot color=(255,0,0),unsorted
radius = 40
start_p = 0
for j = 0 to i-1 
  p = aa(j) * 100 / total  // percent
  rc = fndraw_pie_seg (75,60,radius,start_p, p)
  start_p = start_p + p
next

//draw the text inside each "slice"  

start_p = 0
for j = 0 to i-1
  p = aa(j) * 100 / total   // percent
  mid_ang = (p/2 + start_p) * 3.6   // in degrees
  tx = 75 + (radius * .7) * cos(mid_ang)
  ty = 60 + (radius * .7) * sin(mid_ang)
  text string=aa$(j),x=tx, y=ty, size=24
  start_p = start_p + p
next
print
print  using "###,###"; "Total", total

tt$ = "Budget for January"
text string=tt$,v=10,color=(0,0,255),size=30
end

//--------------------------------------
def fndraw_pie_seg (h, v, r, pct_start, pct) = gosub 10040
10040
plot x=h, y=v
for ang=pct_start * 3.60 to (pct+pct_start) *3.60 step 5
  plot x=h+r*cos(ang), y=v+r*sin(ang)
next
ang=(pct+pct_start) * 3.60
plot x=h+r*cos(ang), y=v+r*sin(ang) // finish
plot x=h,y=v // back to center
return
// ------------------------------------

