'** 12/22/02 8:13:11 PM '** Plot It! True = 1 : False = 0 Dim xy(500,2) [InitColors] ForegroundColor$ = "Black" BackgroundColor$ = "Darkcyan" TexteditorColor$ = "White" TextboxColor$ = "White" ComboboxColor$ = "White" ListboxColor$ = "White" [WindowSetup] NoMainWin WindowWidth = 652 : WindowHeight = 413 UpperLeftX = Int((DisplayWidth-WindowWidth)/2) UpperLeftY = Int((DisplayHeight-WindowHeight)/2) [ControlSetup] Menu #w1, "&File", "Load Data", [load.click], "Save Data", [save.click], "Save graph to BMP", [savebmp], "Quit", [quit] Menu #w1, "Edit" Menu #w1, "&Options", "Set Grids Manually", [setgrids] Graphicbox #w1.g1, 195, 5, 445, 355 plotpass(2)=445 'graphicsbox width plotpass(3)=355 'graphicsbox height Button #w1.button1, "Load Points",[load.click],UL, 5, 240, 105, 25 Button #w1.button2, "Save Points",[save.click],UL, 5, 270, 105, 25 Button #w1.button3, "Plot Points",[plot.click],UL, 5, 300, 105, 25 Button #w1.button3, "Quit",[quit],UL, 5, 330, 105, 25 Texteditor #w1.texteditor1, 5, 5, 185, 230 Open "Plot It!" For Window As #w1 Print #w1, "trapclose [quit]" Print #w1.g1, "down; fill darkblue; backcolor darkblue; color cyan" text$="Enter xy pairs as comma separated, one pair per Line, e.g. 7,3" Print #w1.g1, "place 15 175" Print #w1.g1, "\";text$ text$="using the white textbox. Once data is entered press 'plot points'." Print #w1.g1, "place 15 200" Print #w1.g1, "\";text$ Print #w1.g1, "flush" Print #w1.g1, "setfocus; when mouseMove [MouseChange1]" Print #w1, "font ms_sans_serif 10" [loop] Wait [quit] Close #w1 : End [MouseChange1] 'MouseX and MouseY contain mouse coordinates GoTo [loop] [load.click] FileDialog "Open file..", "*.txt;*.dat;*.csv", fileName$ If fileName$="" Then GoTo [loop] Open fileName$ For Input As #intxt Print #w1.texteditor1, "!contents #intxt"; Close #intxt GoTo [loop] [save.click] Print #w1.texteditor1, "!contents? userText$" FileDialog "Save file as..", "*.*", filenameText$ If filenameText$="" Then GoTo [loop] Open filenameText$ For Output As #17 Print #17, userText$ Close #17 GoTo [loop] [plot.click] Call loadarray If plotpass(1)>0 Then Call plotit End If GoTo [loop] [savebmp] 'save the graph as a bitmap file on disk Print #w1.g1, "getbmp MyBmp 0 0 ";plotpass(2);" ";plotpass(3); 'save the bitmap to disk with BMPSAVE: FileDialog "Save graph as..", "*.bmp", bmpfile$ If bmpfile$="" Then [loop] BmpSave "MyBmp", bmpfile$ 'unload the bitmap from memory: UnloadBmp "MyBmp" GoTo [loop] [setgrids] Call setgrids If plotpass(1)>0 then Call plotit End If GoTo [loop] '******************************************************* 'End of control loop '******************************************************* Sub plotit '******************************************************* 'Start of plotting routine 'plotit requires the following arrays: 'xy(500,2) for input points 'plotpass(1) for number of points to be entered -- done automatically in sub loadarrays 'plotpass(2) width of graphicsbox 'plotpass(3) height of graphics box 'plotpass(4) last segment 'plotpass(5) is a flag for using manual grids (0=use auto, 1=use manual settings) 'plotpass(6) is xmin set manually for grids 'plotpass(7) is xgridstep set manually 'plotpass(8) is ymin set manually 'plotpass(9) is ymingridstep set manually '******************************************************* 'Clean up old graphs If plotpass(4)>0 Then Print #w1.g1, "delsegment ";plotpass(4) End If n=plotpass(1) 'plotpass(1) is the number of xy pairs Print #w1.g1, "cls; fill cyan" 'number of x and y grid lines nxg=10 nyg=10 WinWidth = plotpass(2) WinHeight = plotpass(3) 'definitions 'actual is from xy points entered 'virtual is converted data to screen coordinates 'xmin -- actual value of xmin 'ymin -- actual value of ymin 'xmax -- actual value of xmax 'ymax -- actual value of ymax 'xrange -- actual value of abs(xmax-xmin) 'yrange -- actual value of abs(ymax-ymin) 'xract -- actual value of xrange rounded up using base 10, ie 230 becomes 300 'yract -- actual value of yrange rounded up using base 10, ie 230 becomes 300 'xminact -- actual value of xmin rounded down using base 10, ie 230 becomes 200 'yminact -- actual value of ymin rounded down using base 10, ie 230 becomes 200 'number of pixels to leave at the bottom of the screen fro x labels vytextshift=30 'number of pixels on the left side of the screen for y labels (times length of y label max) vxtextshift=6 'are we going to use auto grids or manual? xman=0 yman=0 If plotpass(9)=1 Then 'is xgrid auto or manual? If plotpass(6)>0 Then xman=1 If plotpass(8)>0 Then yman=1 End If Sort xy(),1,n,2 'find ymin/max nfy=0 ymin=Abs(xy(1,2)) If xy(1,2)<0 Then nfy=1 ymax=xy(n,2) Sort xy(),1,n,1 'find xmin/max nfx=0 xmin=Abs(xy(1,1)) If xy(1,1)<0 Then nfx=1 xmax=xy(n,1) 'step 1 'ok, we need an x value to start from 'xminact is xmin rounded down If xman=1 Then xminact=plotpass(5) Else If xmin<>0 Then xl=Log(xmin)/Log(10) Select Case Case xl>1 xlint=Int(xl) 'rounded down range by ^10 --> i.e. 230 gives 200 If nfx=0 Then xminact=Int(xmin/10^xlint)*10^xlint Else xminact=Int(xmin/10^xlint+1)*10^xlint End If Case xl>=0 'covers values less than 10 If nfx=0 Then xminact=0 Else xminact=10 End If Case xl<0 'this happens when the range is less than one xlint=Int(xl)-1 'rounded down range by ^10 --> i.e. .130 gives .1 If nfx=0 Then xminact=Int(xmin/10^xlint)*10^xlint Else xminact=Int(xmin/10^xlint+1)*10^xlint End If End Select If nfx=1 Then xminact=xminact*-1 Else xminact=0 End If End If 'step 2 'same for ymin 'yminact is the rounded down version of ymin If yman=1 Then yminact=plotpass(7) Else If ymin<>0 Then yl=Log(ymin)/Log(10) Select Case Case yl>1 ylint=Int(yl) 'rounded down range by ^10 --> i.e. 230 gives 200 If nfy=0 Then yminact=Int(ymin/10^ylint)*10^ylint Else yminact=Int(ymin/10^ylint+1)*10^ylint End If Case yl>=0 'covers values less than 10 If nfy=0 Then yminact=0 Else yminact=10 End If Case yl<0 'this happens when the range is less than one ylint=Int(yl)-1 'rounded down range by ^10 --> i.e. .130 gives .1 If nfy=0 Then yminact=Int(ymin/10^ylint)*10^ylint Else yminact=Int(ymin/10^ylint+1)*10^ylint End If End Select If nfy=1 Then yminact=yminact*-1 Else yminact=0 End If End If 'step 3 'now get a rounded range for x If xman=1 Then xgridval=plotpass(6) Else xrange1=Abs(xmax-xminact) Print xrange1 'using a round up version of xrange/10 (we use 10 grid lines) xl=Log(xrange1)/Log(10) Select Case Case xl>=1 xlint=Int(xl) 'rounded up range by ^10 --> i.e. 230 gives 300 xract=Int(xrange1/10^xlint+1)*10^xlint 'if xrange were an exact multiple of 10^xlint we want to leave range alone If Log(xrange1+10^xlint)/Log(10)-Log(xract)/Log(10)=0 Then xract=xrange1 Case xl>0 xract=10 Case xl=0 'this only happens when range is exactly 1 -- which is fine xract=1 Case xl<0 'this happens when the range is less than one xlint=Int(xl)-1 'rounded up range by ^10 --> i.e. .13 gives .2 xract=Int(xrange1/10^xlint+1)*10^xlint 'if xrange were an exact multiple of 10^xlint we want to leave range alone If Log(xrange1+10^xlint)/Log(10)-Log(xract)/Log(10)=0 Then xract=xrange1 End Select 'value of each grid increment xgridval=xract/10 End If 'step 4 'using a round up version of yrange/10 (we use 10 grid lines) If yman=1 Then ygridval=plotpass(8) Else yrange1=Abs(ymax-yminact) yl=Log(yrange1)/Log(10) Select Case Case yl>=1 ylint=Int(yl) 'rounded up range by ^10 --> i.e. 230 gives 300 yract=Int(yrange1/10^ylint+1)*10^ylint 'if yrange were an exact multiple of 10^ylint we want to leave range alone If Log(yrange1+10^ylint)/Log(10)-Log(yract)/Log(10)=0 Then yract=yrange1 Case yl>0 yract=10 Case yl=0 'this only happens when range is exactly 1 -- which is fine yract=1 Case yl<0 'this happens when the range is less than one ylint=Int(yl)-1 'rounded up range by ^10 --> i.e. .13 gives .2 yract=Int(yrange1/10^ylint+1)*10^ylint 'if yrange were an exact multiple of 10^ylint we want to leave range alone If Log(yrange1+10^ylint)/Log(10)-Log(yract)/Log(10)=0 Then yract=yrange1 End Select 'value of each grid increment ygridval=yract/10 End If 'ok, time to work on the graphics screen 'first let's see how much we need to shift over to print y grid values ymaxlabellength=0 ty=yminact ymaxll=Len(Str$(ty)) For z=2 to 10 ymaxll=Len(Str$(ty+ygridval)) If ymaxll>ymaxlabellength Then ymaxlabellength=ymaxll ty=ty+ygridval Next z 'let's figure vxtextshift pixels to the letter vshiftright=vxtextshift*(2+ymaxlabellength) 'and for x we will just leave 30 pixels at bottom vshiftup=vytextshift 'the right edge and boot boundaries are -- origin is set to top, left vrightend=plotpass(2) vdownend=plotpass(3) 'the origin becomes vxorigin=vshiftright vyorigin=vdownend-vshiftup 'length of grid lines becomes vvertgridlen=vdownend-vshiftup vhortgridlen=vrightend-vshiftright 'these lines are divided into 10 segments vvertdiv=vvertgridlen/10 vhortdiv=vhortgridlen/10 'ok, lets draw the vertical grids vxjump=0 For z=1 to 10 vxplace=vxorigin+vxjump vyplace=vyorigin Print #w1.g1, "color darkcyan" Print #w1.g1, "place ";vxplace;" ";vyplace Print #w1.g1, "down" Print #w1.g1, "goto ";vxplace;" ";0 Print #w1.g1, "up" vxjump=vxjump+vhortdiv Next z 'ok, lets draw the horizontal grids vyjump=0 For z=1 to 10 vxplace=vxorigin vyplace=vyorigin-vyjump Print #w1.g1, "color darkcyan" Print #w1.g1, "place ";vxplace;" ";vyplace Print #w1.g1, "down" Print #w1.g1, "goto ";vrightend;" ";vyplace Print #w1.g1, "up" vyjump=vyjump+vvertdiv Next z 'Grids are drawn -- let's place labels 'the y grid is easy so let's do it first 'yminact is the rounded down min value of y 'ygridval is the grid increment vyjump=0 znum=yminact For z=1 to 10 text$=Str$(znum) vyplace=vyorigin-vyjump Print #w1.g1, "down; color blue; font arial 10; backcolor cyan" 'Print #w1.g1, "down;color blue; backcolor cyan" Print #w1.g1, "place ";vxtextshift;" ";vyplace Print #w1.g1, "\";text$ Print #w1.g1, "up" vyjump=vyjump+vvertdiv znum=znum+ygridval Next z y2=znum 'x labels next vxjump=0 znum=xminact For z=1 to 10 text$=Str$(znum) vxplace=vytextshift+vxjump Print #w1.g1, "down; color blue; font arial 10; backcolor cyan" 'Print #w1.g1, "down;color blue; backcolor cyan" Select Case z Case 1,3,5,7,9 Print #w1.g1, "place ";vxplace;" ";vdownend-vxtextshift*2.6 Case 2,4,6,8 Print #w1.g1, "place ";vxplace;" ";vdownend-vxtextshift/2 End Select 'don't want to print the last one, but znum is still being calculated If z<10 Then Print #w1.g1, "\";text$ End If Print #w1.g1, "up" vxjump=vxjump+vhortdiv znum=znum+xgridval Next z x2=znum 'Now we just have to map the points x1=xminact vx1=vxorigin vx2=vrightend y1=yminact vy1=vyorigin vy2=0 For q=1 to n xinput=xy(q,1) vx=vx2-((x2-xinput)/(x2-x1))*(vx2-vx1) yinput=xy(q,2) vy=vy2-((y2-yinput)/(y2-y1))*(vy2-vy1) If vxvx2 Then GoTo [jumphere] If vyvy1 Then GoTo [jumphere] Print #w1.g1, "down; color red" Print #w1.g1, "place ";vx;" ";vy Print #w1.g1, "backcolor red" Print #w1.g1, "circlefilled 3" Print #w1.g1, "up" [jumphere] Next q 'clean up and close plot Print #w1.g1, "segment currentpic" plotpass(4)=currentpic Print #w1.g1, "flush" End Sub '*********************** 'End of plotting routine '*********************** Sub loadarray For z=1 to 500 xy(z,1)=0 xy(z,2)=0 Next z numxy=0 Print #w1.texteditor1, "!lines countVariable"; i=1 ii=0 While ii0 Then ii=ii+1 xnum=Val(Left$(oneline$,numcom-1)) xy(ii,1)=xnum ynum=Val(Mid$(oneline$,numcom+1)) xy(ii,2)=ynum numxy=numxy+1 End If i=i+1 Wend plotpass(1)=numxy 'Make sure the data is sorted smallest x to largest x Sort xy(),1,numxy,1 End Sub Sub setgrids 'plotpass(5) is a flag for using manual grids (0=use auto, 1=use manual settings) 'plotpass(6) is xmin set manually for grids 'plotpass(7) is xgridstep set manually 'plotpass(8) is ymin set manually 'plotpass(9) is ymingridstep set manually [InitColors] ForegroundColor$ = "Black" BackgroundColor$ = "Darkcyan" TexteditorColor$ = "White" TextboxColor$ = "White" ComboboxColor$ = "White" ListboxColor$ = "White" [WindowSetup] WindowWidth = 216 : WindowHeight = 220 UpperLeftX = Int((DisplayWidth-WindowWidth)/2) UpperLeftY = Int((DisplayHeight-WindowHeight)/2) [ControlSetup] Statictext #w2.static1, "Manually set one or both grids:", 5, 0, 205, 25 Statictext #w2.static2, "x grid starting point:", 5, 25, 135, 25 Statictext #w2.static3, "x grid increment (>0):", 5, 50, 135, 25 Statictext #w2.static4, "y grid starting point:", 5, 75, 135, 25 Statictext #w2.static3, "y grid increment (>0):", 5, 100, 135, 25 Button #w2.button2, "Cancel",[cancel],UL, 5, 165, 95, 25 Button #w2.button3, "Replot",[calc.click],UL, 105, 165, 95, 25 Textbox #w2.textbox1, 140, 25, 60, 24 Textbox #w2.textbox2, 140, 50, 60, 24 Textbox #w2.textbox3, 140, 75, 60, 24 Textbox #w2.textbox4, 140, 100, 60, 24 Checkbox #w2.cbox1, "Use automatic grids",[cbox1.check],[cbox1.blank], 5, 135, 205, 25 Open "Set grids" For Dialog As #w2 Print #w2, "trapclose [quitw2]" Print #w2, "font ms_sans_serif 10" If plotpass(9)=0 Then Print #w2.cbox1, "set" Else Print #w2.cbox1, "reset" Print #w2.textbox1, Str$(plotpass(5)) Print #w2.textbox2, Str$(plotpass(6)) Print #w2.textbox3, Str$(plotpass(7)) Print #w2.textbox4, Str$(plotpass(8)) End If ci=plotpass(9) xsi=plotpass(5) xii=plotpass(6) ysi=plotpass(7) yii=plotpass(8) [loopw2] Wait [cbox1.check] plotpass(9)=0 Print #w2.textbox1, "" Print #w2.textbox2, "" Print #w2.textbox3, "" Print #w2.textbox4, "" GoTo [loopw2] [cbox1.blank] plotpass(9)=1 Print #w2.textbox1, "!setfocus" GoTo [loopw2] [cancel] plotpass(9)=ci plotpass(5)=xsi plotpass(6)=xii plotpass(7)=ysi plotpass(8)=yii GoTo [quitw2] [calc.click] 'is there data in the boxes? Print #w2.textbox1, "!contents? xs$" Print #w2.textbox2, "!contents? xi$" Print #w2.textbox3, "!contents? ys$" Print #w2.textbox4, "!contents? yi$" 'if there is no data, then set it back to auto noxg=0 noyg=0 If Trim$(xs$)="" and Trim$(xi$)="" Then noxg=1 If Trim$(ys$)="" and Trim$(yi$)<>"" Then noyg=1 If noxg=1 and noyg=1 Then plotpass(9)=0 GoTo [quitw2] Else plotpass(5)=Val(xs$) xs=plotpass(5) plotpass(6)=Val(xi$) xi=plotpass(6) plotpass(7)=Val(ys$) ys=plotpass(7) plotpass(8)=Val(yi$) yi=plotpass(8) [quitw2] Close #w2 End Sub