# Memo-Plot.bas # # HotPaw Basic applet # # Copy this into your MemoPad. # Make sure first line starts with # # and ends with .bas # (4 characters, no trailing space!) # Run using HotPaw (cbasPad Pro) Basic # version 1.07 or later. # # Reads 2 columns of comma seperated # data from the Memopad. # Calculates the mean and # standard deviation. # Plots an autoscaled x,y graph. # # (c)2000 rhn@hotpaw.com # new c$= "," : rem comma seperator input "memo title", t$ m = db.find("memo",t$) if (m < 0 ) ? "memo <";t$;"> not found" stop endif dim x(200),y(200),z(200) open "memo",m as #1 input #1, a$ :' read title i = 0 sy = 0 : sy2 = 0 : sz = 0 : sz2 = 0 # read data from memo while not eof input #1,a$ if a$ = "" then exit while x(i) = i+1 t = val(field$(a$,1,c$)) sy = sy + t sy2 = sy2 + (t*t) y(i) = t t = val(field$(a$,2,c$)) if t > 0 then z$="1" sz = sz + t sz2 = sz2 + (t*t) z(i) = t i = i + 1 wend n = i if (n < 2) then stop # #calculate statistics ay=sy / n vy= (n*sy2 - (sy)^2)/(n*(n-1)) az = sz / n vz= (n*sz2 - (sz)^2)/(n*(n-1)) draw -1 form btn 80,104,40,12,"Plot It",1 draw "Memo Title",20,20 draw t$, 80,20 draw "item count",20,32 draw str$(n),80,32 draw "y mean",20,46 draw str$(ay),80,46 draw "y std.dev.",20,58 draw str$(sqr(vy)),80,58 draw "z mean",20,72 draw str$(az),80,72 draw "z std.dev.",20,84 draw str$(sqr(vz)),80,84 # wait for input a$ = input$(1) form reset 0,0,0,0,"Plot",-1 # call plot subroutine plot(n) # wait for tap to exit x = fn pen(0) end # 3000 rem #plot(n) subroutine sub plot(nxy, i,j,k,ymax,ymin, s,yad,xmax,xmin,ya0,ya1) draw -1 draw 0,14,160,146,-7 ymin = y(0) : ymax = y(0) xmin = x(0) : xmax = x(nxy-1) for i = 1 to nxy-1 if x(i) < xmin then xmin = x(i) if x(i) > xmax then xmax = x(i) if y(i) < ymin then ymin = y(i) if y(i) > ymax then ymax = y(i) if z(i) < ymin then ymin = z(i) if z(i) >ymax then ymax=z(i) next i if ymin-ymax = 0 then ymax = 1.1*ymin rem ** calc y grid ** call grid(ymin,ymax) :' ->uad if ua0 < ymin then ymin = ua0 if ua1 > ymax then ymax = ua1 yad = uad rem scale s = 128 x0 = 2 y0 = 16+s ys = s/(ymax-ymin) y0 = y0+ys*ymin draw color 0x99,0x33,0,1 y=ymin while y < ymax i = y0-y*ys draw 2,i,2+s,i,2 y = y+uad wend rem ** x axis ** grid(xmin,xmax) : rem uad if ua0 < xmin then xmin = ua0 if ua1 > xmax then xmax = ua1 xad = uad rem scale xs = s/(xmax-xmin) x0 = 2-xs*xmin x=xmin while x < xmax i = x0+x*xs draw i,16,i,16+abs(s),2 x = x+xad wend draw color 0x33,0x33,0x33,1 draw 2,16,s,s,4 # draw color 0,0,0,3 draw str$(ymin),133,7+s draw str$(ymax),133,14 draw str$(xmin),2,18+s draw str$(xmax),s-4,18+s rem ** graph the data ** draw color 0xFF,0,0,1 : rem red moveto x0+xs*x(0),y0-ys*y(0) for i = 1 to nxy-1 lineto x0+xs*x(i),y0-ys*y(i) next i if (z$<>"") draw color 0,0x99,0x33,1 moveto x0+xs*x(0),y0-ys*z(0) for i = 1 to nxy-1 lineto x0+xs*x(i),y0-ys*z(i) next i endif rem return #grid size subroutine #grid ( umin, umax ) -> ua0,uad sub grid(umin,umax, u,v,i,a1,fm, na1,m) u = abs(umax-umin)/8 a1 = log(u)/log(10) na1 = int(a1+0.49) if u < 1 then na1 = na1-1 v = u/(10^na1) uad = 10^na1 if v > sqr(2) then uad = 2*(10^na1) if v > sqr(5) then uad = 5*(10^na1) if v > sqr(10) then uad = 10*(10^na1) # if uad < (umax-umin)/nxy then uad = (umax-umin)/nxy m = int(umin/uad) if umin < 0 then m = m-1 ua0 = uad*m m = int(umax/uad)+1 ua1 = uad*m if ya1 < umax then ya1 = umax end sub end