Announcement

Collapse
No announcement yet.

converting pb cc code to pbwin

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • converting pb cc code to pbwin

    I found a graphic plotting PBCC program at https://forum.powerbasic.com/forum/u...029#post601029
    but was not able to compile it in PBWin 10 ? how do I convert it from PBCC to PBWin ?

  • #2
    In this case it might not be too hard.

    Most of the code is GRAPHIC Window, no need to change that.

    The console and print commands (other than graphic print) should be changed to TXT.something. You'll need to add a TXT.WINDOW

    Changing console part to GUI possible, but harder to explain.

    Cheers,
    Dale

    Comment


    • #3
      Found another good program at https://forum.powerbasic.com/forum/u...185#post602185

      and got it listed out here:

      'PBCC6 program. 'Simple' scatterplot
      #COMPILE EXE
      #DIM ALL
      #INCLUDE "ComDlg32.inc"

      FUNCTION CalculateStepSize(dmin AS SINGLE, dmax AS SINGLE, Steps AS LONG) AS SINGLE
      LOCAL dRange, tempstep, StepSize AS SINGLE
      LOCAL mag, magPow, magMsd, mult AS LONG

      Steps = 10 ' eg maximum marks on axis
      dRange = dmax -dmin : ? "Range: " dRange
      IF dRange = 0 THEN EXIT FUNCTION

      ' calculate an initial guess at step size
      tempStep = dRange/Steps : ? "tempstep" tempStep
      ' scale up low magnitude step sizes (avoid negative log10 values)
      WHILE tempstep < 1
      TempStep = TempStep * 10
      INCR mult
      WEND : ? "mult" mult
      ' get the magnitude of the step size
      mag = FIX(LOG10(tempStep)) : ? "mag" mag
      magPow = 10^mag : ? "magPow" magPow
      ' calculate most significant digit of the new step size
      magMsd = ((tempStep/magPow) + 0.5) : ? "magMsd" magMsd
      ' promote the MSD TO either 1, 2, OR 5
      IF (magMsd > 5.0) THEN
      magMsd = 10.0
      ELSEIF (magMsd > 2.0) THEN
      magMsd = 5.0
      ELSEIF (magMsd > 1.0) THEN
      magMsd = 2.0
      END IF
      StepSize = magMsd*magPow
      ' reduce scaled up magnitudes if required
      StepSize = StepSize / 10^mult : ? "StepSize" StepSize
      ' Adjust _min and _max
      dmin = StepSize * INT(dmin/StepSize) : ? "Min " dmin
      dmax = StepSize * CEIL(dmax/StepSize) : ? "Max " dmax
      FUNCTION = StepSize
      ?
      END FUNCTION
      '------------------/CalculateStepSize

      SUB GetFileName (sFilename As String)
      OpenFileDialog(BYVAL %HWND_DESKTOP, _ ' function in ComDlg32.inc
      "Find data file", _
      sFilename, _
      EXE.PATH$, _
      "Data Files (*.DAT, *.CSV)|*.DAT;*.CSV|", _
      "", _
      %OFN_EXPLORER OR %OFN_FILEMUSTEXIST OR %OFN_NODEREFERENCELINKS )
      END SUB
      '------------------/GetFileName

      FUNCTION PBMAIN () AS LONG
      LOCAL hBmp, n, hFont1, hFont2, Clk AS LONG
      LOCAL r, x, y, xmax, ymax, xmin, ymin, xmark, ymark AS SINGLE
      LOCAL Steps AS LONG ' marks per axis
      LOCAL lm, tm, rm, bm AS SINGLE ' margins
      LOCAL slm, stm, srm, sbm AS SINGLE ' scaled margins
      LOCAL file, title, xaxis, yaxis, sKey AS STRING

      FONT NEW "Lucida Console", 10, 0, 0, 0, 0 TO hFont1 ' set up fonts to be used later
      FONT NEW "Lucida Console", 10, 0, 0, 0, 900 TO hFont2 ' rotated font (needs to be true type)

      CON.NEW ' only one console at a time permitted
      CONSOLE SET LOC 1, 1
      CONSOLE SET SCREEN 30, 30

      file = "ltest.dat" ' example data
      GetFileName file ' browse for data file
      title = "Simple Auto-scaling scatterplot"
      xaxis = "X-Axis"
      yaxis = "Y-Axis"

      lm = 70 : rm = 70 : tm = 50 : bm = 70 ' Margins (pixels). Space for labels etc

      ERR = 0
      OPEN file$ FOR INPUT AS #1
      IF ERR THEN ? "File Error: " ERROR$ : GOTO Fin
      INPUT#1, n ' First line = number of points
      DIM a(1 TO n, 1 TO 2) AS DOUBLE ' Note most Graphic sizes etc are single.

      GRAPHIC WINDOW "test", 300, 300, 800, 500 TO hBmp&
      GRAPHIC ATTACH hBmp&, 0
      GRAPHIC CLEAR %WHITE
      GRAPHIC COLOR %BLACK, %WHITE

      FOR r = 1 TO n ' Read data -> array (check data range)
      INPUT#1, a(r, 1), a(r, 2)
      xmax = MAX(a(r, 1), xmax) ' Take note of max & min values
      ymax = MAX(a(r, 2), ymax)
      xmin = IIF(r = 1, a(1,1), MIN(a(r, 1), xmin)) ' Initially set min to first value,
      ymin = IIF(r = 1, a(1,2), MIN(a(r, 2), ymin)) ' update in subsequent loops
      NEXT

      PRINT "x data: " xmin "To" xmax
      PRINT "y data: " ymin "To" ymax
      PRINT

      ' calculate 'pretty' mark spacing based on data range / values
      : ? "x Axis"
      xmark = CalculateStepSize(xmin, xmax, Steps)
      : ? "y Axis"
      ymark = CalculateStepSize(ymin, ymax, Steps)

      PRINT "Xmarks" xmark " Ymarks" ymark

      ' Calc scaled margins
      slm = lm * (xmax-xmin)/(GRAPHIC(CANVAS.X) - lm - rm) : srm = rm * (xmax-xmin)/(GRAPHIC(CANVAS.X) - lm - rm)
      stm = tm * (ymax-ymin)/(GRAPHIC(CANVAS.Y) - tm - bm) : sbm = bm * (ymax-ymin)/(GRAPHIC(CANVAS.Y) - tm - bm)
      ' PRINT "Margins l,r,t,b" Ceil(slm) Ceil(srm) Ceil(stm) Ceil(sbm)

      ' set custom coordinates for direct plotting of points (length of axes plus margins)
      GRAPHIC SCALE (xmin-slm, ymax+stm) - (xmax+srm, ymin-sbm)

      ' draw axes, marks, labels
      GRAPHIC SET FONT hFont1 ' set font1
      GRAPHIC SET POS (xmax-(xmax-xmin)/2 - GRAPHIC(TEXT.SIZE.X, title)/2, ymax + stm/2) ' centralized
      GRAPHIC PRINT title
      GRAPHIC SET POS (xmax-(xmax-xmin)/2 - GRAPHIC(TEXT.SIZE.X, xaxis$)/2, ymin - sbm/2) ' centralized
      GRAPHIC PRINT xaxis$ ' xaxis label

      GRAPHIC LINE (xmin, ymin) - (xmax, ymin) ' x axis line

      FOR r = xmin TO xmax STEP xmark ' marks
      GRAPHIC LINE (r, ymin) - (r, ymin-GRAPHIC(CELL.SIZE.Y)/2)
      GRAPHIC SET POS STEP (-GRAPHIC(CELL.SIZE.X)*1.5, -GRAPHIC(CELL.SIZE.Y))
      IF xmark < .1 THEN
      GRAPHIC PRINT FORMAT$(r, "0.00") ' print scale values
      ELSEIF xmark < 1 THEN
      GRAPHIC PRINT FORMAT$(r, "0.0")
      ELSE
      GRAPHIC PRINT r
      END IF
      NEXT

      GRAPHIC SET FONT hFont2 ' font2 (rotated font)
      GRAPHIC SET POS (xmin-slm*.9, (ymax-(ymax-ymin)/2) - GRAPHIC(TEXT.SIZE.X, yaxis$)/2) ' centralized
      GRAPHIC PRINT yaxis$ ' yaxis label
      GRAPHIC SET FONT hFont1 ' back to font1

      GRAPHIC LINE (xmin, ymin) - (xmin, ymax) ' y axis line

      FOR r = ymin TO ymax STEP ymark ' marks
      GRAPHIC LINE (xmin, r) - (xmin-GRAPHIC(CELL.SIZE.X)/2, r)
      GRAPHIC SET POS STEP (-GRAPHIC(CELL.SIZE.X)*5, GRAPHIC(CELL.SIZE.Y)/2)
      IF ymark < .1 THEN
      GRAPHIC PRINT FORMAT$(r, "0.00") ' print scale values
      ELSEIF ymark < 1 THEN
      GRAPHIC PRINT FORMAT$(r, "0.0")
      ELSE
      GRAPHIC PRINT r
      END IF
      NEXT

      IF xmin < 0 THEN ' mark zero line
      GRAPHIC LINE (0, ymin) - (0, ymax), %RGB_LIGHTGRAY
      GRAPHIC LINE (0, ymin) - (0, ymin-GRAPHIC(CELL.SIZE.Y)/2)
      END IF

      IF ymin < 0 THEN ' mark zero line
      GRAPHIC LINE (xmin, 0) - (xmax, 0), %RGB_LIGHTGRAY
      GRAPHIC LINE (xmin, 0) - (xmin-GRAPHIC(CELL.SIZE.X)/2, 0)
      END IF

      ' GRAPHIC PRINT points..
      FOR r = 1 TO n
      x = a(r, 1) - GRAPHIC(CELL.SIZE.X) / 2 ' adjust point positions to suit "X" center
      y = a(r, 2) + GRAPHIC(CELL.SIZE.Y) / 2
      ' PRINT a(r, 1), a(r, 2)
      GRAPHIC SET POS (x, y)
      GRAPHIC PRINT "X"
      ' GRAPHIC SET PIXEL (a(r, 1),a(r, 2)), %RED ' check alignment of point mark
      NEXT r
      GRAPHIC SET CAPTION "Click screen for values"

      ' Interaction..
      DO WHILE GRAPHIC(DC) <> 0 ' loop while Graphic Window exists
      GRAPHIC INKEY$ TO sKey ' check keyboard input to graphic window
      SLEEP 1
      IF sKey <> "" THEN EXIT LOOP ' any key to quit loop (focus goes to console)

      GRAPHIC WINDOW CLICK hBmp TO Clk, x, y ' check if graphic window clicked

      IF Clk = 2 THEN ' Double Click
      GRAPHIC SET CAPTION "test" ' clear old pos info
      ELSEIF Clk = 1 THEN ' Single click
      IF xmax < 10 OR ymax < 10 THEN
      GRAPHIC SET CAPTION USING$("x: #.# y: #.#", x, y) ' show x, y 'click' position
      ELSE
      GRAPHIC SET CAPTION USING$("x: # y: #", x, y) ' decimal places not needed
      END IF
      END IF
      LOOP
      GRAPHIC WINDOW END
      Fin:
      PRINT "Press any key to end."
      CONSOLE SET FOCUS
      WAITKEY$
      CLOSE
      CON.END
      END FUNCTION
      '------------------/

      Comment


      • #4
        Compiles. No data file, so no test run. Let me know how it goes.
        '
        Code:
        'Was PBCC6 program. Now also PBWin10 'Simple' scatterplot
        #compile exe
        #dim all
        global ghTxtWin as dword
        #include "ComDlg32.inc"
        
        function CalculateStepSize(dmin as single, dmax as single, Steps as long) as single
          local dRange, tempstep, StepSize as single
          local mag, magPow, magMsd, mult as long
        
          Steps = 10 ' eg maximum marks on axis
          dRange = dmax -dmin : txt.print "Range: " dRange
          if dRange = 0 then exit function
           ''replace "?" with txt.print several times  <<====
          ' calculate an initial guess at step size
          tempStep = dRange/Steps : txt.print "tempstep" tempStep
          ' scale up low magnitude step sizes (avoid negative log10 values)
          while tempstep < 1
            TempStep = TempStep * 10
            incr mult
          wend : txt.print "mult" mult
         ' get the magnitude of the step size
          mag = fix(log10(tempStep)) : txt.print "mag" mag
          magPow = 10^mag : txt.print "magPow" magPow
          ' calculate most significant digit of the new step size
          magMsd = ((tempStep/magPow) + 0.5) : txt.print "magMsd" magMsd
          ' promote the MSD TO either 1, 2, OR 5
          if (magMsd > 5.0) then
            magMsd = 10.0
          elseif (magMsd > 2.0) then
            magMsd = 5.0
          elseif (magMsd > 1.0) then
            magMsd = 2.0
          end if
          StepSize = magMsd*magPow
          ' reduce scaled up magnitudes if required
          StepSize = StepSize / 10^mult : txt.print "StepSize" StepSize
          ' Adjust _min and _max
          dmin = StepSize * int(dmin/StepSize) : txt.print "Min " dmin
          dmax = StepSize * ceil(dmax/StepSize) : txt.print "Max " dmax
          function = StepSize
          txt.print
        end function
        '------------------/CalculateStepSize
        
        sub GetFileName (sFilename as string)
          OpenFileDialog(byval %hwnd_desktop, _ ' function in ComDlg32.inc
             "Find data file", _
             sFilename, _
             exe.path$, _ '<<=== change to other dir, ok for testing
             "Data Files (*.DAT, *.CSV)|*.DAT;*.CSV|", _
             "", _
             %ofn_explorer or %ofn_filemustexist or %ofn_nodereferencelinks )
        end sub
        '------------------/GetFileName
        
        function pbmain () as long
          local hBmp, n, hFont1, hFont2, Clk as long
          local r, x, y, xmax, ymax, xmin, ymin, xmark, ymark as single
          local Steps as long ' marks per axis
          local lm, tm, rm, bm as single ' margins
          local slm, stm, srm, sbm as single ' scaled margins
          local file, title, xaxis, yaxis, sKey as string
        
          font new "Lucida Console", 10, 0, 0, 0, 0 to hFont1 ' set up fonts to be used later
          font new "Lucida Console", 10, 0, 0, 0, 900 to hFont2 ' rotated font (needs to be true type)
        
          'con.new ' only one console at a time permitted
          'console set loc 1, 1
          'console set screen 30, 30
          txt.window("Scatter Plot", 1, 1, 30, 30) to ghTxtWin
        
          file = "ltest.dat" ' example data
          GetFileName file ' browse for data file
          title = "Simple Auto-scaling scatterplot"
          xaxis = "X-Axis"
          yaxis = "Y-Axis"
        
          lm = 70 : rm = 70 : tm = 50 : bm = 70 ' Margins (pixels). Space for labels etc
        
          err = 0
          open file$ for input as #1
          if err then txt.print "File Error: " + error$ : goto Fin
          input#1, n ' First line = number of points
          dim a(1 to n, 1 to 2) as double ' Note most Graphic sizes etc are single.
        
          graphic window "test", 300, 300, 800, 500 to hBmp&
          graphic attach hBmp&, 0
          graphic clear %white
          graphic color %black, %white
        
          for r = 1 to n ' Read data -> array (check data range)
            input#1, a(r, 1), a(r, 2)
            xmax = max(a(r, 1), xmax) ' Take note of max & min values
            ymax = max(a(r, 2), ymax)
            xmin = iif(r = 1, a(1,1), min(a(r, 1), xmin)) ' Initially set min to first value,
            ymin = iif(r = 1, a(1,2), min(a(r, 2), ymin)) ' update in subsequent loops
          next
        
          txt.print "x data: " xmin "To" xmax
          txt.print "y data: " ymin "To" ymax
          txt.print
        
          ' calculate 'pretty' mark spacing based on data range / values
          txt.print "x Axis"
          xmark = CalculateStepSize(xmin, xmax, Steps)
          txt.print "y Axis"
          ymark = CalculateStepSize(ymin, ymax, Steps)
        
          txt.print "Xmarks" xmark " Ymarks" ymark
        
          ' Calc scaled margins
          slm = lm * (xmax-xmin)/(graphic(canvas.x) - lm - rm) : srm = rm * (xmax-xmin)/ _
             (graphic(canvas.x) - lm - rm)
          stm = tm * (ymax-ymin)/(graphic(canvas.y) - tm - bm) : sbm = bm * (ymax-ymin)/ _
             (graphic(canvas.y) - tm - bm)
          ' PRINT "Margins l,r,t,b" Ceil(slm) Ceil(srm) Ceil(stm) Ceil(sbm)
        
          ' set custom coordinates for direct plotting of points (length of axes plus margins)
          graphic scale (xmin-slm, ymax+stm) - (xmax+srm, ymin-sbm)
        
          ' draw axes, marks, labels
          graphic set font hFont1 ' set font1
          graphic set pos (xmax-(xmax-xmin)/2 - graphic(text.size.x, title)/2, ymax + stm/2) ' centralized
          graphic print title
          graphic set pos (xmax-(xmax-xmin)/2 - graphic(text.size.x, xaxis$)/2, ymin - sbm/2) ' centralized
          graphic print xaxis$ ' xaxis label
        
          graphic line (xmin, ymin) - (xmax, ymin) ' x axis line
        
          for r = xmin to xmax step xmark ' marks
            graphic line (r, ymin) - (r, ymin-graphic(cell.size.y)/2)
            graphic set pos step (-graphic(cell.size.x)*1.5, -graphic(cell.size.y))
            if xmark < .1 then
              graphic print format$(r, "0.00") ' print scale values
            elseif xmark < 1 then
              graphic print format$(r, "0.0")
            else
              graphic print r
            end if
          next
        
          graphic set font hFont2 ' font2 (rotated font)
          graphic set pos (xmin-slm*.9, (ymax-(ymax-ymin)/2) - graphic(text.size.x, yaxis$)/2) ' centralized
          graphic print yaxis$ ' yaxis label
          graphic set font hFont1 ' back to font1
        
          graphic line (xmin, ymin) - (xmin, ymax) ' y axis line
        
          for r = ymin to ymax step ymark ' marks
            graphic line (xmin, r) - (xmin-graphic(cell.size.x)/2, r)
            graphic set pos step (-graphic(cell.size.x)*5, graphic(cell.size.y)/2)
            if ymark < .1 then
              graphic print format$(r, "0.00") ' print scale values
            elseif ymark < 1 then
              graphic print format$(r, "0.0")
            else
              graphic print r
            end if
          next
        
          if xmin < 0 then ' mark zero line
            graphic line (0, ymin) - (0, ymax), %rgb_lightgray
            graphic line (0, ymin) - (0, ymin-graphic(cell.size.y)/2)
          end if
        
          if ymin < 0 then ' mark zero line
            graphic line (xmin, 0) - (xmax, 0), %rgb_lightgray
            graphic line (xmin, 0) - (xmin-graphic(cell.size.x)/2, 0)
          end if
        
        ' GRAPHIC PRINT points..
          for r = 1 to n
            x = a(r, 1) - graphic(cell.size.x) / 2 ' adjust point positions to suit "X" center
            y = a(r, 2) + graphic(cell.size.y) / 2
            ' PRINT a(r, 1), a(r, 2)
            graphic set pos (x, y)
            graphic print "X"
            ' GRAPHIC SET PIXEL (a(r, 1),a(r, 2)), %RED ' check alignment of point mark
          next r
          graphic set caption "Click screen for values"
        
          ' Interaction..
          do while graphic(dc) <> 0 ' loop while Graphic Window exists
            graphic inkey$ to sKey ' check keyboard input to graphic window
            sleep 1
            if sKey <> "" then exit loop ' any key to quit loop (focus goes to console)
        
            graphic window click hBmp to Clk, x, y ' check if graphic window clicked
        
            if Clk = 2 then ' Double Click
              graphic set caption "test" ' clear old pos info
            elseif Clk = 1 then ' Single click
              if xmax < 10 or ymax < 10 then
                graphic set caption using$("x: #.# y: #.#", x, y) ' show x, y 'click' position
              else
                graphic set caption using$("x: # y: #", x, y) ' decimal places not needed
              end if
            end if
          loop
          graphic window end
          Fin:
            txt.print "Press any key to end."
            SetFocus ghTxtWin 'change from console set focus
            txt.waitkey$
            close
            txt.end
        end function
        '------------------/
        '
        '
        Easier convert than even I thought at first. Since PBCC 6 also has TXT.WINDOW, this should now run
        as is in both!

        Cheers,
        Dale

        Comment


        • #5
          Thank you Dale

          I used Dave's data (as detailed below) and it works beautifully !

          11
          -1.4,6.1
          1.7,2.4
          2.4,6.5
          2.5,-6.9
          -2.7,5.4
          3.3,9.3
          3.4,1.7
          3.7,8.9
          4.0,10.0
          4.1,-9.0
          4.2,9.7

          Comment

          Working...
          X