1# Draw a piechart given sizes, labels and colors of slices. 2# 3begin piechart nSlices, sliceSizes, sliceLabels, sliceColors 4 5 local radius 6 local counter, total 7 local sweep, just 8 9 let radius = 8 10 linestyle 0.1 11 font "Helvetica", 3 12 rotate 90 13 14 # Sum up values of all slices. 15 # 16 let counter = 1, total = 0 17 while counter <= nSlices 18 do 19 let total = total + sliceSizes[counter] 20 let counter = counter + 1 21 done 22 23 # Draw each pie slice. 24 # 25 let counter = 1 26 while counter <= nSlices 27 do 28 # Skip empty slices 29 # 30 if sliceSizes[counter] > 0 31 then 32 let sweep = sliceSizes[counter] / total * 360 33 clearpath 34 wedge 0, 0, radius, 0, -sweep 35 color sliceColors[counter] 36 fill 37 color "black" 38 stroke 39 40 # Draw label halfway around pie slice with 41 # a line back into the pie. 42 # 43 rotate -sweep / 2 44 if sliceLabels[counter] ne "" 45 then 46 # Calculate alignment for label, based on its 47 # position in pie. 48 # 49 if Mapyrus.rotation > -90 and Mapyrus.rotation < 90 50 then 51 let just = "left" 52 else 53 let just = "right" 54 endif 55 if Mapyrus.rotation > 45 and Mapyrus.rotation < 135 56 then 57 let just = just . ",bottom" 58 elif Mapyrus.rotation > -135 and Mapyrus.rotation < -45 59 then 60 let just = just . ",top" 61 else 62 let just = just . ",middle" 63 endif 64 justify just 65 66 clearpath 67 move radius + 2, 0 68 label sliceLabels[counter] 69 draw radius - 3, 0 70 stroke 71 endif 72 73 # Rotate axes ready to draw next slice. 74 # 75 rotate -sweep / 2 76 endif 77 let counter = counter + 1 78 done 79end 80