home *** CD-ROM | disk | FTP | other *** search
/ Programming Tool Box / SIMS_2.iso / libbasic / grapher1.bas < prev    next >
BASIC Source File  |  1992-10-07  |  6KB  |  185 lines

  1.  
  2.  
  3.   'This is a sample application as described in the chapter
  4.   'Liberty BASIC Graphical Features
  5.  
  6.     'The purpose of this application is to allow the entry of three
  7.     'sets of numerical data and then to plot them in contrasting
  8.     'colors superimposing each other.    The concepts utilized here
  9.     'include handling the spreadsheet, using buttons, and drawing
  10.     'into a graphical window using several colors
  11.  
  12.     nomainwin  ' do not open a main window
  13.  
  14.     'create 3 arrays
  15.     dim columnOne(10)
  16.     dim columnTwo(10)
  17.     dim columnThree(10)
  18.  
  19.     'set up the size of the spreadsheet window
  20.     WindowWidth = 500
  21.     WindowHeight = 348
  22.  
  23.     'set up the buttons
  24.     bmpbutton #sheet, "grphbttn.bmp", [graph], LR, -17, -15
  25.     bmpbutton #sheet, "loadbttn.bmp", [load], LR, 18, -15
  26.     bmpbutton #sheet, "savebttn.bmp", [save], LR, 53, -15
  27.  
  28.     'open the window
  29.     open "GRAPHER, a Liberty BASIC application"  for spreadsheet as #sheet
  30.  
  31.     print #sheet, "trapclose [quit]"
  32.     print #sheet, "indirect"        'use indirect control mode
  33.     print #sheet, "select B4"       'position the selector at B4
  34.  
  35.     'display some simple instructions and also the column headings
  36.     print #sheet, "cell A1 'Please enter 3 columns of data below and click on Graph."
  37.     print #sheet, "cell A3 'Item #     Set 1      Set 2      Set 3"
  38.  
  39.     'place the numbers 1 to 10 to the left of the column information
  40.     for index = 1 to 10
  41.         print #sheet, "cell A"; str$(index+3); " '"; str$(index)
  42.     next index
  43.  
  44.     'specify columns B,C,D from 4 to 13 as user (entry) columns and to accept numbers
  45.     for index = 4 to 13
  46.         print #sheet, "user B"; str$(index); " number"
  47.         print #sheet, "user C"; str$(index); " number"
  48.         print #sheet, "user D"; str$(index); " number"
  49.     next index
  50.  
  51.     'display title header and set up the cell to hold it to accept a string
  52.     print #sheet, "cell A15 'Title for the graph:"
  53.     print #sheet, "user A16 string"
  54.  
  55.     'force display of the spreadsheet
  56.     print #sheet, "flush"
  57.  
  58.  
  59. [inputLoop]    ' wait for input (button clicks)
  60.     input r$
  61.     goto [inputLoop]
  62.  
  63.  
  64. [graph]     'display a graph of the data in the spreadsheet
  65.  
  66.     'if a graph is already displayed, then close its window
  67.     if plotFlag > 0 then close #graph
  68.     plotFlag = 1
  69.  
  70.  
  71.     'get the column data from the spreadsheet
  72.     'and look for the peak (greatest) y value
  73.     peak = 0
  74.     for index = 4 to 13
  75.         print #sheet, "result? B"; str$(index)
  76.         input #sheet, r$ : columnOne(index-3) = val(r$)
  77.         if val(r$) > peak then peak = val(r$)
  78.         print #sheet, "result? C"; str$(index)
  79.         input #sheet, r$ : columnTwo(index-3) = val(r$)
  80.         if val(r$) > peak then peak = val(r$)
  81.         print #sheet, "result? D"; str$(index)
  82.         input #sheet, r$ : columnThree(index-3) = val(r$)
  83.         if val(r$) > peak then peak = val(r$)
  84.     next index
  85.  
  86.     'set up the size of the graph window based on peak
  87.     WindowWidth = 320 + 70
  88.     WindowHeight = 64 + peak + 25 + 20
  89.     yScale = 1
  90.     if WindowHeight > 400 then yScale = 400 / WindowHeight : WindowHeight = 450
  91.  
  92.     'get the title of the graph from the spreadsheet
  93.     print #sheet, "result? A16"
  94.     input #sheet, title$
  95.  
  96.     ' open a window for the graph with one button
  97.     button #graph, Done, [done], LR, 5, 5
  98.     open title$ for graphics_nsb as #graph
  99.  
  100.     'draw a scale
  101.     print #graph, "place 0 "; 15 * yScale + 35
  102.     print #graph, "down"
  103.     print #graph, "\   "; peak
  104.     print #graph, "size 2 ; place 50 "; 15 * yScale + 35
  105.     print #graph, "goto 55 "; 15 * yScale + 35
  106.     print #graph, "goto 55 "; (peak + 5 + 15) * yScale + 35
  107.     print #graph, "goto 305 "; (peak + 5 + 15) * yScale + 35
  108.  
  109.     'set the size of the graphics pen
  110.     print #graph, "size 3"
  111.  
  112.     'add ten to peak to move it away from the top of the window
  113.     peak = peak + 15
  114.  
  115.     'plot columnOne in red
  116.     print #graph, "color red"
  117.     print #graph, "up"
  118.     print #graph, "goto 60 "; ((peak - columnOne(1)) * yScale) + 35
  119.     print #graph, "down"
  120.     for x = 60 to 285 step 25
  121.         print #graph, "goto "; x; " "; (peak - columnOne(int((x-35)/25))) * yScale + 35
  122.     next x
  123.  
  124.     'plot columnTwo in green
  125.     print #graph, "color green"
  126.     print #graph, "up"
  127.     print #graph, "goto 60 "; ((peak - columnTwo(1)) * yScale) + 35
  128.     print #graph, "down"
  129.     for x = 60 to 285 step 25
  130.         print #graph, "goto "; x; " "; (peak - columnTwo(int((x-35)/25))) * yScale + 35
  131.     next x
  132.  
  133.     'plot columnThree in blue
  134.     print #graph, "color blue"
  135.     print #graph, "up"
  136.     print #graph, "goto 60 "; ((peak - columnThree(1)) * yScale) + 35
  137.     print #graph, "down"
  138.     for x = 60 to 285 step 25
  139.         print #graph, "goto "; x; " "; (peak - columnThree(int((x-35)/25))) * yScale + 35
  140.     next x
  141.  
  142.     'display the title in black in the upper left corner
  143.     print #graph, "color black"
  144.     print #graph, "up"
  145.     print #graph, "goto 50 "; 20
  146.     print #graph, "font Roman 10 30"
  147.     print #graph, "\   "; title$
  148.  
  149.     'force display of the graph
  150.     print #graph, "flush"
  151.  
  152.     goto [inputLoop]    ' drawing finished, go back and poll for input
  153.  
  154.  
  155. [done]      'close graph window if Done button is clicked
  156.  
  157.     plotFlag = 0
  158.     close #graph
  159.     goto [inputLoop]
  160.  
  161. [load]   'load a spreadsheet
  162.  
  163.     filedialog "Load Grapher File", "*.ABC", fileName$
  164.     if fileName$ = "" then [inputLoop]
  165.     print #sheet, "load "; fileName$
  166.     goto [inputLoop]
  167.  
  168.  
  169. [save]   'save a spreadsheet
  170.  
  171.     filedialog "Save Grapher File", "*.ABC", fileName$
  172.     if fileName$ = "" then [inputLoop]
  173.     print #sheet, "save "; fileName$
  174.     goto [inputLoop]
  175.  
  176.  
  177. [quit]      'exit the Grapher application if desired
  178.  
  179.     confirm "Quit Grapher?"; quit$
  180.     if quit$ = "no" then [inputLoop]
  181.     if plotFlag = 1 then close #graph
  182.     close #sheet
  183.  
  184.     end
  185.