home *** CD-ROM | disk | FTP | other *** search
/ DTP Toolbox / DTPToolbox.iso / propage4.0 / arexx / makepiechart.pprx < prev    next >
Encoding:
Text File  |  1995-06-25  |  5.3 KB  |  205 lines

  1. /* This Genie draws Pie Charts. You must have gdarexxsupport.library in your libs: directory (normally installed with PPage 3).
  2. Written by Don Cox  May '92. Improved July 94, June 95.  */
  3.  
  4. /* $VER: MakePieChart June 95 */
  5.  
  6. call open("STDERR","ram:trace","W") 
  7. trace r
  8.  
  9. cr = '0a'x
  10. call SafeEndEdit.rexx()
  11. call ppm_AutoUpdate(0)
  12. call ppm_NewGroup()
  13.  
  14.  
  15. if ~show("l", "gdarexxsupport.library") then
  16.     if ~addlib("gdarexxsupport.library", 0, -30,0) then
  17.     do
  18.         call ppm_Inform(1,"Please install the gdarexxsupport.library in your libs: directory before running this Genie.")
  19.     end
  20.  
  21. units = ppm_GetUnits()
  22. call ppm_SetUnits(2)
  23.  
  24. signal on halt
  25. signal on break_c
  26. signal on break_e
  27. signal on break_d
  28.  
  29. box = ppm_ClickOnBox("Click on box to make chart..")
  30.  
  31. if box = 0 then
  32. do
  33.     call ppm_Inform(1, "No box selected",)
  34.     call ppm_ClearStatus()
  35.     exit
  36. end
  37.  
  38. /*  extract box attributes  */
  39. boxsize = ppm_GetBoxSize(box)
  40. boxpos = ppm_GetBoxPosition(box)
  41.  
  42. if ppm_Inform(2, "Delete box?",) = 1 then call ppm_DeleteBox(box)
  43.  
  44. boxwidth = word(boxsize, 1)
  45. boxheight = word(boxsize, 2)
  46. boxleft = word(boxpos, 1)
  47. boxtop = word(boxpos, 2)
  48. Xcentre = boxleft+(boxwidth/2)
  49. Ycentre = boxtop+(boxheight/2)
  50.  
  51. if boxwidth>boxheight then radius = boxheight*0.36
  52. else radius = boxwidth*0.3 /* allow room for labels */
  53.  
  54. nmsegs = GetUserText(4, "Number of Segments")
  55. if nmsegs > 18 then exit_msg("Max number of segments is 18")
  56.  
  57. form = ' Segment 1'
  58. do x = 2 while x <= nmsegs
  59.  form = form cr 'Segment' x
  60. end
  61.  
  62. fillsegs = 0
  63. if ppm_Inform(2,"Fill sectors with patterns?",)=1 then fillsegs = 1
  64.  
  65. form = ppm_GetForm("Type in Values",12,form)
  66. if form = "" then exit_msg("Operation Cancelled")
  67.  
  68. total = 0
  69. do x = 1 to nmsegs
  70.     parse var form bdata.x '0a'x form
  71.     if bdata.x = "" then bdata.x = 0
  72.     if ~datatype(bdata.x,n) then exit_msg("Not a number: "bdata.x)
  73.     total = total+bdata.x
  74. end
  75.  
  76. do x = 1 to nmsegs
  77.     cdata.x = (bdata.x * 360)/total   /* convert to %ages & then degrees */
  78.     end
  79.  
  80. form = ' Segment label 1'
  81. do x = 2 while x <= nmsegs
  82.    form = form cr 'Segment label' x
  83. end
  84.  
  85. form = ppm_GetForm("Labels for Segments",12,form)
  86. if form = "" then exit_msg("Operation Cancelled")
  87.  
  88. x = 1
  89. do forever
  90.    parse var form blabel.x '0a'x form
  91.    if blabel.x = "" then leave
  92.    x = x + 1
  93. end
  94. call ppm_SetLineWeight(1) /* 1-point lines */
  95. facelist = ppm_GetTypeFaceList()
  96. facelist = substr(facelist, pos('0a'x, facelist) +1) /*strip off the number*/
  97. face = ppm_SelectFromList("Select Typeface",32,18,0,facelist)
  98.  
  99.  
  100. oldface = ppm_GetFont()
  101. oldsize = ppm_GetSize()
  102. oldstyle = ppm_GetStyle()
  103. oldjust = ppm_GetJustification()
  104. call ppm_SetJustification(2)
  105. call ppm_SetFont(face)
  106. call ppm_SetSize(radius*3)  /* size in points */
  107. call ppm_SetStyle(N)
  108.  
  109. startangle=0
  110. do i=1 to nmsegs
  111.     call ppm_ShowStatus("Working on segment:" i)
  112.     angle=cdata.i
  113.     if fillsegs = 1 then call ppm_SetFillPattern(i//9) /* use modulo to cycle through the available patterns */
  114.     boxname=blabel.i
  115.     data = bdata.i
  116.     endangle = trunc(startangle+angle) /* use whole degrees to avoid gaps */
  117.     if i=nmsegs then endangle=360 /*close up gap */
  118.     manylines = Xcentre "0a"x Ycentre "0a"x radius "0a"x startangle "0a"x endangle "0a"x boxname "0a"x data "0a"x total
  119.     call drawsector(manylines)
  120.     startangle = endangle
  121.     end
  122.  
  123. call ppm_SetFont(oldface)
  124. call ppm_SetSize(oldsize)
  125. call ppm_SetStyle(oldstyle)
  126. call ppm_SetJustification(oldjust)
  127. exit_msg("Done")
  128. end
  129.  
  130. /* -------------------------------------------------------------------- */
  131.  
  132. /* Procedure to draw a sector for a pie chart */
  133.  
  134. drawsector: procedure
  135.  
  136. parse arg Xcentre "0a"x Ycentre "0a"x radius "0a"x startangle "0a"x endangle "0a"x boxname "0a"x data "0a"x total
  137. AA=6.2831853/360    /* 2pi divided by 360 */
  138.  
  139. drawstring = Xcentre Ycentre"0a"x  /* List all the points, beginning at the centre */
  140.  
  141. if startangle>endangle then
  142.     do
  143.     xx=startangle
  144.     startangle=endangle
  145.     endangle=xx
  146.     end
  147. if startangle=endangle then return
  148.  
  149. angle=startangle*AA /* convert to radians */
  150. arcsize = endangle-startangle 
  151. radarcsize = arcsize*AA /* convert to radians */
  152.  
  153. /* Set up values for label box before "angle" gets changed */
  154. labelXradius = radius*1.5
  155. labelYradius = radius*1.25
  156. labelboxheight = radius/4
  157. labelboxwidth = radius*1
  158. labelangle = angle+(radarcsize/2)
  159. labelleft = Xcentre+(labelXradius*cos(labelangle))-(radius/2)
  160. labeltop = Ycentre+(labelYradius*sin(labelangle))-(radius/9)
  161.  
  162. /* Draw the segment itself, in 1 degree steps to give a smooth curve */
  163. do i = 0 to arcsize
  164.     X2=Xcentre+(radius*cos(angle))
  165.     Y2=Ycentre+(radius*sin(angle))
  166.     angle=angle+AA
  167.     drawstring=drawstring||X2 Y2"0a"x
  168.     end
  169. drawstring = drawstring||Xcentre Ycentre"0a"x
  170. call ppm_SaveText("ram:arcdata",drawstring)
  171.  
  172. box = ppm_DrawPoly("ram:arcdata",boxname)
  173.  
  174. /* Now draw the box for the label */
  175. labelbox = ppm_CreateBox(labelleft, labeltop, labelboxwidth, labelboxheight, 0)
  176. datatr = trunc(data,2)
  177. dataper = trunc((data*100)/total, 2) /* two decimal places */
  178. overflow = ppm_TextIntoBox(labelbox,boxname "0a"x datatr"   "dataper||"%")
  179.  
  180. return
  181. end
  182.  
  183. /* --------------------------------------------------------------- */
  184.  
  185. /* Exit Routines */
  186. break_d:
  187. break_e:
  188. break_c:
  189. halt:
  190.     call exit_msg("User aborted Genie!")
  191.  
  192. error:
  193. syntax:
  194.     do exit_msg("Genie failed due to error: "errortext(rc))
  195.     end
  196.  
  197. exit_msg:
  198.     do
  199.     parse arg message
  200.     if message ~= "" then call ppm_Inform(1,message,"Resume")
  201.     call ppm_ClearStatus()
  202.     call ppm_AutoUpdate(1)
  203.     exit
  204.     end
  205.