Post by Psycho on Jul 17, 2010 13:30:35 GMT -5
As promised in my earlier "Drawing Arcs" thread, below is some code to create pie charts in RB. Unfortunately, Dan's example on my previous thread utilizing <canvas> to create these doesn't work on the IE versions being used at my facility.
This code is flawed but will work for my needs and may be of help to someone else. You can select regular or 3D effect pie charts. The percentages are selected at random for this demo.
John "Psycho" Siejkowski
This code is flawed but will work for my needs and may be of help to someone else. You can select regular or 3D effect pie charts. The percentages are selected at random for this demo.
'Demo program to show how to draw pie charts in Run Basic
'Pie code written by John "Psycho" Siejkowski, 2010
'Original arc subroutine written in Liberty Basic by Tom Nally for LB newsletter #109
'http://babek.info/libertybasicfiles/lbnews/nl109/arcs.htm
'Released as Open Source
'Requires the following 8 arguments passed to the GenPie drawing sub:
'startAngle
'endAngle
'segments
'centerX
'centerY
'radius
'fillColor$
'borderColor$
'define graphic area handle as global to allow drawing both inside and outside of sub
global #main, threeD, legendY
'add checkbox to select 3-D effect
checkbox #threeDbox, "3-D Effect", 0
button #clear, "Clear Graphic Box", [clear]
print space$(5);
button #drawPie, "Draw Pie", [draw]
print space$(5);
button #quit, "Quit", [quit]
wait
[clear]
html "<div style='position: relative; left: 5px; top: 5px; height:0px; width:0px;'>"
#main cls()
render #main
html "</div>"
wait
[quit]
cls
html "<b>Goodbye</b>"
end
[draw]
'determine segment size percentages
total=0
dim seg(6)
for x = 1 to 5
seg(x)=int(rnd(0)*70)+seg(x-1) '70*5=350 to never go over 360
next x
seg(6)=359 'to complete the pie and allow for line size of 2
'create graphic area
html "<div style='position: relative; left: 5px; top: 5px; height:0px; width:0px;'>"
graphic #main, 300, 440
#main cls()
'display legend with percentages
#main size(1)
'colors in legend$ should match fillColor$
legend$="red verylightgray yellow blue cyan green"
'legend2$ contains descriptions
legend2$=" Run, Make Ready, Non-Productive, Wash Up, PM, Repairs"
y=250
for legendcolor = 1 to 6
#main color(word$(legend$,legendcolor))
for x = 1 to 20
#main place(0,y)
#main box(x,x+y)
next x
#main font("arial", 12)
#main color("black")
#main place(25,x+y)
#main "\";word$(legend2$,legendcolor,",");using("###",100*(seg(legendcolor)-seg(legendcolor-1))/360);"%"
y=y+30
next legendcolor
'set variable values for center, radius, start/end degrees, no. of segments in arcs
XCenter = 150
YCenter = 120
Radius = 100
StartAngleDegs = 0
EndAngleDegs = 359 'to allow for line of size 2
NLSegs = 20
if #threeDbox value() then
threeD=1
else
threeD=0
end if
'call the sub which converts degrees to radians and generates pie
if threeD=1 then
loops = 12 'stack pies for 3-D effect
else
loops = 1
end if
for g = 1 to loops
call GenPie XCenter, YCenter, Radius, StartAngleDegs, seg(1), NLSegs, "red", "darkRed"
call GenPie XCenter, YCenter, Radius, seg(1)+1, seg(2), NLSegs, "verylightgray", "gray"
call GenPie XCenter, YCenter, Radius, seg(2)+1, seg(3), NLSegs, "yellow", "brown"
call GenPie XCenter, YCenter, Radius, seg(3)+1, seg(4), NLSegs, "blue", "navy"
call GenPie XCenter, YCenter, Radius, seg(4)+1, seg(5), NLSegs, "cyan", "darkCyan"
call GenPie XCenter, YCenter, Radius, seg(5)+1, seg(6), NLSegs, "green", "darkGreen"
XCenter=XCenter+1
YCenter=YCenter+1
next g
render #main
html "</div>"
wait
Sub GenPie Xc, Yc, R, StartD, EndD, NLS, fillColor$, borderColor$
'change line thickness based on radius size (good for <=140 radius)
if R>80 then
#main size(3)
else
#main size(2)
end if
'Plot the lines (rays)
for x = StartD+1 to EndD-1
#main color(fillColor$)
#main place(Xc, Yc)
#main north()
#main turn(90-x)
#main go(R)
next x
pi = 4*atn(1)
StartR = (StartD/360)*(2*pi)
EndR = (EndD/360)*(2*pi)
ArcElement = (EndR - StartR) / NLS
dim xplot(NLS)
dim yplot(NLS)
xplot(0) = Xc + R * cos(StartR)
yplot(0) = Yc - R * sin(StartR)
For i = 1 to NLS
xplot(i) = Xc + R * cos(StartR + i*ArcElement)
yplot(i) = Yc - R * sin(StartR + i*ArcElement)
Next i
'Plot the outer arcs
if threeD=1 then
#main color(borderColor$)
else
#main color(fillColor$)
end if
#main size(4) 'size 3 looks better on outer ring but sometimes leaves voids in 3-D effect
for i = 1 to NLS
#main line(xplot(i-1), yplot(i-1), xplot(i), yplot(i))
next i
'draw sides of pie pieces
#main size(2)
#main line(Xc, Yc, xplot(0), yplot(0))
#main line(Xc, Yc, xplot(NLS), yplot(NLS))
End Sub
John "Psycho" Siejkowski