// Draw pie chart using SHAPE statement.
// Draws each segment in a different color.

// Note: Use light colors so dark text will show up.

//   title    amount (red,green,blue)
DATA Food,      7531, 216, 180,   0 // gold
DATA Mortgage, 15395, 128, 255, 255 // sky blue
DATA Util.,     2995, 255, 128,   0 // orange
DATA Taxes,     8250,   0, 255,   0 // green
DATA Car,       5004, 192, 128, 255 // lavender
DATA Fun,       9083, 255,   0, 128 // carnation red
DATA Misc,      7050,   0, 255, 128 // aquamarine 

DIM aa(30), aa$(30), red(30), green(30), blue(30) 
DEGREES  // do all angle calculations in degrees

scale=1.0  // change scale to make smaller physical graph

GRAPH hscale = 15 / scale, vscale = 15 / scale
GRAPH width  = 10 * scale, height = 7.5 * scale
// Make hscale and vscale the same for correct aspect
GRAPH noaxes,nogrid // Don't need axis and grid for pie chart

centerx = 75: centery = 60: radius = 40

print
// First read all the data into arrays and calculate total
// so we can figure the percentage of each.
total = 0
i = 0
while read(0) <> 0    // stop when no more data
  read aa$(i), aa(i), red(i), green(i), blue(i)
  print  using "###,###"; aa$(i), aa(i)
  total = total + aa(i)
  i = i + 1
wend
print
print  using "###,###"; "Total", total

// Draw the pie chart

shape center=(centerx, centery), radius=radius
start_pct = 0
for j = 0 to i-1 
  pct = aa(j) * 100 / total  // percent
  shape startangle = start_pct * 3.60  // 360 deg. / 100%
  shape endangle   = (pct + start_pct) *3.60
  shape fillcolor  = (red(j), green(j), blue(j))
  shape pie
  start_pct = start_pct + pct
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 = centerx + (radius * .7) * cos(mid_ang)
  ty = centery + (radius * .7) * sin(mid_ang)
  text string=aa$(j),x=tx, y=ty, size = 24 * scale
  start_p = start_p + p
next

// Put a Title at the bottom.

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

