# XY-Plot .bas # version 1.2 # HotPaw Basic applet # new c$= "," : rem comma seperator top: input "memo title", t$ if t$="" then stop m = db.find("memo",t$) if (m < 0 ) ? "memo <";t$;"> not found" stop endif dim x(200),y(200) open "memo",m as #1 input #1, a$ :' read title i = 0 sy = 0 : sy2 = 0 : sx = 0 : sx2 = 0 sxy = 0 # read data from memo while not eof input #1,a$ if a$ = "" then exit while t = val(field$(a$,1,c$)) sx = sx + t sx2 = sx2 + (t*t) x(i) = t t = val(field$(a$,2,c$)) sy = sy + t sy2 = sy2 + (t*t) sxy = sxy + (x(i)*t) y(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)) ax = sx / n vx= (n*sx2 - (sx)^2)/(n*(n-1)) r0=(n*sxy-sy*sx) d = sqr((n*sy2-(sy*sy)) * (n*sx2-(sx*sx))) if d <> 0 then r1=r0/d : else r1=0 r=r1 d = n*sx2-(sx^2) b = (n*sxy-sx*sy)/d a = (sy-b*sx)/n draw -1 form btn 80,147,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 "x mean",20,46 draw str$(ax),80,46 draw "x std.dev.",20,58 draw str$(sqr(vx)),80,58 draw "y mean",20,72 draw str$(ay),80,72 draw "y std.dev.",20,84 draw str$(sqr(vy)),80,84 draw "r = ",20,96 draw str$(r),80,96 draw "b = ",20,108 draw str$(b),80,108 draw str$(a),80,120 # wait for input a$ = input$(1) form reset 0,0,0,0,"XY Plot",-1 # call plot subroutine gosub plot(n) # find tap location while 1 x = fn pen(0) y = fn pen(2) if (y > 150) then exit while x=round((x-x0)/(xs+xs),1) y=round(-(y-y0)/(ys+ys),1) p$=" " & str$(x+x) & ", " p$=p$ & str$(y+y) & " " draw p$,54,149 wend goto top: 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) next i if ymin-ymax = 0 then ymax = 1.1*ymin rem ** calc y grid ** gosub grid(ymin,ymax) :' ->uad if ua0 < ymin then ymin = ua0 if ua1 > ymax then ymax = ua1 yad = uad rem ** scale y ** s = 128 x0 = 4 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 4,i,4+s,i,2 y = y+uad wend rem ** x axis ** gosub 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 = 4-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 4,16,s,s,4 draw str$(ymin),134,7+s draw str$(ymax),134,14 draw str$(xmin),3,18+s draw str$(xmax),s-3,18+s rem ** graph the data ** draw color 0xFF,0,0,1 : rem red for i = 0 to nxy-1 x3 = x0+xs*x(i)-1 y3 = y0-ys*y(i)-1 draw x3,y3,3,3,7 rem draw str$(i),x3,y3 next i 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 # # (c)2000 rhn@hotpaw.com