Announcement

Collapse
No announcement yet.

simple scatterplot

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

  • simple scatterplot

    I'm converting my TurboBasic programs to PBCC6 and am still a beginner with PBCC. I need a simple way of plotting a scatterplot and have looked at the examples and on the forum but all of them seem overly complex when all I want to do is to plot several X-Y points. I have my points in a 2-dimensional array a(1:n,1:2) and I have maxx, minx, maxy and miny so it should be a doddle to plot the points but all the examples a way too complex. Can anybody give me a simple example so that I can get to know the techniques?

  • #2
    Mike,
    does this help?

    Paul.
    Code:
    'PBCC6 program
    FUNCTION PBMAIN () AS LONG
    
    LOCAL x,y,r AS LONG
    
      CONSOLE SET LOC 1,1
      CONSOLE SET SCREEN 5,30
      GRAPHIC WINDOW "test",100,100,800,500 TO hBmp&
    
      GRAPHIC ATTACH hBmp&, 0
      GRAPHIC CLEAR %BLACK
    
      FOR r = 1 TO 100
          x = RND(1,800)
          y = RND(1,500)
          GRAPHIC SET PIXEL (x,y),%YELLOW
      NEXT
    
    
      PRINT "Press any key to end."
      CONSOLE SET FOCUS
      WAITKEY$
    END FUNCTION

    Comment


    • #3
      That's a good start, Paul. I've now got to find how to draw and label the axes and how to set the size of the points but that's got the basics upon which I can build. Thank you.

      Comment


      • #4
        Now what I have to do is to scale x and y to the size of the graphic window, set the size of the points and draw and label the axes:

        Code:
        'PBCC6 program
        FUNCTION PBMAIN () AS LONG
        
        LOCAL x,y,r,n AS LONG
        file$="ltest.dat"
        OPEN file$ FOR INPUT AS #1
        INPUT#1,n   'first line of file has the number of pairs
                    'all other lines have pairs of numbers separated by commas
        DIM a(n,2) AS DOUBLE
          CONSOLE SET LOC 1,1
          CONSOLE SET SCREEN 15,25
          GRAPHIC WINDOW "test",300,300,800,500 TO hBmp&
          GRAPHIC ATTACH hBmp&, 0
          GRAPHIC CLEAR %BLACK
          FOR r = 1 TO n
              INPUT#1,a(r,1),a(r,2)
              x = a(r,1)
              y = a(r,2)
              PRINT a(r,1),a(r,2)
              GRAPHIC SET PIXEL (x,y),%YELLOW
          NEXT r
          PRINT "Press any key to end."
          CONSOLE SET FOCUS
          WAITKEY$
        CLOSE
        END FUNCTION

        Comment


        • #5
          Now what I have to do is to scale x and y to the size of the graphic window,..
          Easier to scale the Graphic Window to suit the data
          Code:
          'PBCC6 program
          FUNCTION PBMAIN () AS LONG
           
          LOCAL x,y,r,n,xmax,ymax, hFont AS LONG
          file$="ltest.dat"                 ' see comments at end of source
          OPEN file$ FOR INPUT AS #1
          INPUT#1,n   'first line of file has the number of pairs
                      'all other lines have pairs of numbers separated by commas
          DIM a(1 TO n, 1 TO 2) AS DOUBLE
            CONSOLE SET LOC 1,1
            CONSOLE SET SCREEN 15,25
            GRAPHIC WINDOW "test",300,300,800,500 TO hBmp&
            GRAPHIC ATTACH hBmp&, 0
            GRAPHIC CLEAR %BLACK
            GRAPHIC COLOR %YELLOW, %BLACK
           
            FOR r = 1 TO n
                INPUT#1,a(r,1),a(r,2)
                xmax = MAX(a(r,1), xmax) : ymax = MAX(a(r,2),ymax)
            NEXT
            PRINT "xmax" xmax, "ymax" ymax                    ' eg 100, 90
            GRAPHIC SCALE (-5,-10) - (xmax+10, ymax+15)       ' customize coordinates to suit data
            '  GRAPHIC SCALE (-5, ymax+10) - (xmax+10,-15)       ' eg change orientation
           
            GRAPHIC SET POS (xmax/2,-9)
            GRAPHIC PRINT "X-AXIS"
           
            GRAPHIC LINE (0,0) - (0,ymax+5)                   ' x axis
            FOR r = 0 TO ymax STEP n                          ' marks
              GRAPHIC LINE (0,r) - (-1,r)
              GRAPHIC SET POS STEP (-2,0)
              GRAPHIC PRINT r                                 ' scale values
            NEXT
           
            FONT NEW "Arial", 8, 0, 0, 0, 900 TO hFont        ' true type font for escapement
            GRAPHIC SET FONT hFont
            GRAPHIC SET POS (-5,ymax/2)
            GRAPHIC PRINT "Y-AXIS"
            GRAPHIC SET FONT 0                                ' default font (MS Sans Serif 8 point)
           
            GRAPHIC LINE (0,0) - (xmax+5,0)                   ' y axis
            FOR r = 0 TO xmax STEP n                          ' marks
              GRAPHIC LINE (r,0) - (r,-2)
              GRAPHIC SET POS STEP (0,-2)
              GRAPHIC PRINT r                                 ' scale values
            NEXT
           
            FOR r = 1 TO n
                x = a(r,1)
                y = a(r,2)
                PRINT a(r,1),a(r,2)
                GRAPHIC ELLIPSE (x-1,y-1) - (x+1,y+1), %RED, %RED
                GRAPHIC SET PIXEL (x,y), %YELLOW
            NEXT r
            PRINT "Press any key to end."
            CONSOLE SET FOCUS
            WAITKEY$
          CLOSE
          END FUNCTION
           
          #IF 0
          ' sample data for ltest.dat
           
          10
          10,5
          20,10
          30,20
          40,30
          50,40
          60,50
          70,60
          80,70
          90,80
          100,90
           
          #ENDIF
          Last edited by Dave Biggs; 2 Feb 2012, 06:05 PM. Reason: Forgot to include commented sample data for ltest.dat. add eg. for orientation change.
          Rgds, Dave

          Comment


          • #6
            Thanks, Dave. That gives me lots of ideas. I've changed a couple of things: I've set the font to Lucida Console which I think looks nicer; was there a particular reason for you choosing Arial and the default font? I've also set the points using an X character rather than drawing an ellipse (just personal preference). Now I'm trying to work out the scaling so that the numbers print at sensible intervals. This could be difficult! I have also changed the data at the end of the file to be my own test data. Does the #IF mean that if the file is not present it would use those data?

            Code:
             'PBCC6 program
            FUNCTION PBMAIN () AS LONG
            
            LOCAL x,y,r,n,xmax,ymax, hFont AS LONG
            file$="ltest.dat"                 ' see comments at end of source
            b$="Title"
            xaxis$="X-Axis"
            yaxis$="Y-Axis"
            OPEN file$ FOR INPUT AS #1
            INPUT#1,n
            DIM a(1 TO n, 1 TO 2) AS DOUBLE
              CONSOLE SET LOC 1,1
              CONSOLE SET SCREEN 15,25
              GRAPHIC WINDOW "test",300,300,800,500 TO hBmp&
              GRAPHIC ATTACH hBmp&, 0
              GRAPHIC CLEAR %WHITE
              GRAPHIC COLOR %BLACK, %WHITE
              GRAPHIC FONT "Lucida Console", 12, 0
              GRAPHIC PRINT SPC((80-LEN(b$))/2);b$
              FOR r = 1 TO n
                  INPUT#1,a(r,1),a(r,2)
                  xmax = MAX(a(r,1), xmax) : ymax = MAX(a(r,2),ymax)
              NEXT
              PRINT "xmax" xmax, "ymax" ymax
            '  GRAPHIC SCALE (-5,-10) - (xmax+10, ymax+15)       ' customize coordinates to suit data
              GRAPHIC SCALE (-5, ymax+10) - (xmax+10,-15)       ' eg change orientation
            
              GRAPHIC SET POS (xmax/2,-9)
              GRAPHIC PRINT xaxis$
            
              GRAPHIC LINE (0,0) - (0,ymax+5)                   ' x axis
              FOR r = 0 TO ymax STEP n                          ' marks
                GRAPHIC LINE (0,r) - (-1,r)
                GRAPHIC SET POS STEP (-2,0)
                GRAPHIC PRINT r                                 ' scale values
              NEXT
            
            REM   FONT NEW "Arial", 8, 0, 0, 0, 900 TO hFont        ' true type font for escapement
            REM   GRAPHIC SET FONT hFont
              GRAPHIC SET POS (-5,ymax/2)
              GRAPHIC PRINT yaxis$
            REM   GRAPHIC SET FONT 0                                ' default font (MS Sans Serif 8 point)
            
              GRAPHIC LINE (0,0) - (xmax+5,0)                   ' y axis
              FOR r = 0 TO xmax STEP n                          ' marks
                GRAPHIC LINE (r,0) - (r,-2)
                GRAPHIC SET POS STEP (0,-2)
                GRAPHIC PRINT r                                 ' scale values
              NEXT
            
              FOR r = 1 TO n
                  x = a(r,1)
                  y = a(r,2)
                  PRINT a(r,1),a(r,2)
                  GRAPHIC SET POS (x,y)
                  GRAPHIC PRINT "X"
            REM       GRAPHIC ELLIPSE (x-1,y-1) - (x+1,y+1), %RED, %RED
            REM       GRAPHIC SET PIXEL (x,y), %YELLOW
              NEXT r
              PRINT "Press any key to end."
              CONSOLE SET FOCUS
              WAITKEY$
            CLOSE
            END FUNCTION
            
            #IF 0
            ' sample data for ltest.dat
            
            11
            14,61
            17,24
            24,65
            25,69
            27,54
            33,93
            34,17
            37,89
            40,100
            41,90
            42,97
            
            #ENDIF

            Comment


            • #7
              Hi Mike,

              I've tweaked the code a bit more. I didn't have any reason for the font choices before - just needed to change from default to any true type font to demo use of a rotated label for the Y-Axis.

              In the code below I have added adjustment of your "X" positioning so that the center of the character lies on the point to be plotted.

              I've ajusted the scale marks and added point value display in the caption bar upon mouse-click (clear w/ double-click) too.

              The #IF 0 / #ENDIF metastatements just mark a comment block that the compiler should ignore.
              Code:
              'PBCC6 program
              FUNCTION PBMAIN () AS LONG
               
              LOCAL x,y,r,n,xmax, ymax, hFont1, hFont2 AS LONG
              file$="ltest.dat"                 ' see example data at end of source
              b$="Title"
              xaxis$="X-Axis"
              yaxis$="Y-Axis"
              OPEN file$ FOR INPUT AS #1
              INPUT#1,n
              DIM a(1 TO n, 1 TO 2) AS DOUBLE
                CONSOLE SET LOC 1,1
                CONSOLE SET SCREEN 15,25
                GRAPHIC WINDOW "test",300,300,800,500 TO hBmp&
                GRAPHIC ATTACH hBmp&, 0
                GRAPHIC CLEAR %WHITE
                GRAPHIC COLOR %BLACK, %WHITE
                FONT NEW "Lucida Console", 12, 0, 0, 0 ,0 TO hFont1      ' set up fonts to be used
                FONT NEW "Lucida Console", 12, 0, 0, 0, 900 TO hFont2    ' rotated font (must be true type)
               
                FOR r = 1 TO n
                    INPUT#1,a(r,1),a(r,2)
                    xmax = MAX(a(r,1), xmax) : ymax = MAX(a(r,2),ymax)
                NEXT
                PRINT "xmax" xmax, "ymax" ymax
                GRAPHIC SCALE (-5, ymax+10) - (xmax+10,-15)       ' set scale / orientation
               
                GRAPHIC SET FONT hFont1                           ' set font
                GRAPHIC SET POS (xmax/2, ymax+8) : GRAPHIC PRINT b$
                GRAPHIC SET POS (xmax/2,-9)      : GRAPHIC PRINT xaxis$
               
                GRAPHIC LINE (0,0) - (0,ymax+5)                   ' x axis
                FOR r = 0 TO ymax STEP 5                          ' marks
                  IF r MOD 10 = 0 THEN
                    GRAPHIC LINE (0,r) - (-.5,r)
                    GRAPHIC SET POS STEP (-3.3,1.5)
                    GRAPHIC PRINT r                               ' scale values
                  ELSE
                    GRAPHIC LINE (0,r) - (-.2,r)
                  END IF
                NEXT
               
                GRAPHIC SET FONT hFont2                           ' font2 rotated font
                GRAPHIC SET POS (-4.5,ymax/2)
                GRAPHIC PRINT yaxis$
                GRAPHIC SET FONT hFont1                           ' font1
               
                GRAPHIC LINE (0,0) - (xmax+5,0)                   ' y axis
                FOR r = 0 TO xmax STEP 5                          ' marks
                  IF r MOD 10 = 0 THEN
                    GRAPHIC LINE (r,0) - (r,-2)
                    GRAPHIC SET POS STEP (-1,-2)
                    GRAPHIC PRINT r                               ' scale values
                  ELSE
                    GRAPHIC LINE (r,0) - (r,-1)
                  END IF
                NEXT
               
                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 POS (1, ymax+8) : GRAPHIC PRINT "Click screen for values"
                NEXT r
               
                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 POS (1, ymax+8) : GRAPHIC SET CAPTION "test" ' clear old pos info
                  ELSEIF Clk& = 1 THEN
                    GRAPHIC SET POS (1, ymax+8) : GRAPHIC PRINT "                       "
                    GRAPHIC SET CAPTION " x:"+STR$(x&)+", y:"+STR$(y&)  ' show 'click' position in graphic window
                  END IF
                LOOP
               
                PRINT "Press any key to end."
                CONSOLE SET FOCUS
                WAITKEY$
               CLOSE
              END FUNCTION
               
              #IF 0                       '* metastatement defines (e.g. comment) section for compiler to ignore
              ' sample data for ltest.dat
               
              11
              14,61
              17,24
              24,65
              25,69
              27,54
              33,93
              34,17
              37,89
              40,100
              41,90
              42,97
               
              #ENDIF                      '*
              Rgds, Dave

              Comment


              • #8
                Thanks for the latest lesson, Dave! I like the rotated font and the correct position of the Xs and the click on the screen is a nice demonstration which I may well use in other programs but it is not necessary in this one. I've changed the 'LOCAL' statements to split the integers from the reals, so that x,y,xmax and ymax are double precision as they are rarely integers in real life - OK, in the test data set they are integers but usually they would not be. The main problem now is to make the program work for all conceivable data sets. I tried it with the same data except that I moved the decimal point one place to the left in all the X values and the axis scaling and point setting went haywire. As I said in my last message it's the scaling to accommodate all conceivable data that's the problem. I solved it in my TurboBasic programs but I'm having difficulty in seeing how to do it in PBCC. In any case, in my TurboBasic programs it always resulted in messy axis divisions and I'm hopeful that in PBCC I can tidy that up. I hope that you don't mind spending time helping me on this - just let me know when you get bored with it. You've already given me some excellent pointers and I'm very grateful.

                Mike

                Comment


                • #9
                  Not bored yet

                  What's needed are general algorithms to work out the range of data and appropriate marks and labels on the axes. Somewhat dependant on the actual data to be plotted of course.

                  This is what I have so far..
                  Code:
                  ' Determine range of values to be plotted and axes required
                   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)                           ' note max & min values
                      ymax = Max(a(r, 2), ymax)
                      xmin = IIF(xmin > 0, Min(a(r, 1), xmin), a(1,1))    ' initially set min to first value
                      ymin = IIF(ymin > 0, Min(a(r, 2), ymin), a(1,2))    ' update in subsequent loops
                   NEXT
                    PRINT "x" xmin "-" xmax, "y" ymin "-" ymax
                   
                    xmin = IIF(xmin > 10, Fix (xmin/10)*10,  Fix(xmin))   ' round down minimum values &
                    xmax = IIF(xmax > 10, Ceil(xmax/10)*10, Ceil(xmax))   ' round up maximums for scale ranges
                    ymin = IIF(ymin > 10, Fix (ymin/10)*10,  Fix(ymin))
                    ymax = IIF(ymax > 10, Ceil(ymax/10)*10, Ceil(ymax))
                    PRINT "x scale" xmin "-" xmax
                    PRINT "y scale" ymin "-" ymax
                   
                    ' To Do: 
                    ' calculate mark spacing based on data range / values
                    ' ..divide scale by .5, 5, 50 bigger marks at MOD 1, 10, 100 ?
                   
                    ' draw axes marks, labels
                   
                    ' GRAPHIC SET CLIP (margins to set aside area for plotting)
                    ' .. ~ 70 pixels on left and bottom, 30 at top for Title, 60 on the right for balance?
                   
                    ' GRAPHIC SCALE (xmin, ymin) - (xmax, ymax)  ' set custom coordinates for direct plotting of points
                   
                    ' GRAPHIC PRINT points..
                  '------------------/
                   
                  'Sample data, results:
                  x 1.4 - 4.2   y 1.7 - 10         x 14 - 42     y 17 - 100
                   
                  x scale 1 - 5                    x scale 10 - 50
                  y scale 1 - 10                   y scale 10 - 100
                   1.4           6.1                14            61
                   1.7           2.4                17            24
                   2.4           6.5                24            65
                   2.5           6.9                25            69
                   2.7           5.4                27            54
                   3.3           9.3                33            93
                   3.4           1.7                34            17
                   3.7           8.9                37            89
                   4             10                 40            100
                   4.1           9                  41            90
                   4.2           9.7                42            97
                  Rgds, Dave

                  Comment


                  • #10
                    Took that forward a little in the code below but it's not such a 'simple scatterplot' anymore

                    Seems that the trick will be finding a method of analysing the data to deciding what start / stop / interval markers to use.

                    I've tried to make a generic solution below and it kind of works with the three data sets listed at the end of the source. (Well ok that's not 'all conceivable data sets' but it's heading in that direction )

                    PB's GRAPHIC SCALE statement makes plotting the data a breeze but actually drawing the axes, marks and labels consistently is a bit trickier.
                    Use of the new GRAPHIC Functions like GRAPHIC(CANVAS.X) and GRAPHIC(CELL.SIZE.X) helps a lot there as they are scaled along with the window when a different data set is loaded.

                    Code:
                    'PBCC6 program
                    #DIM ALL
                     
                    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 lm, tm, rm, bm AS SINGLE                           ' margins
                     LOCAL slm, stm, srm, sbm AS SINGLE                       ' scaled margins
                     LOCAL file, b, 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)
                      CONSOLE SET LOC 1, 1
                      CONSOLE SET SCREEN 20, 30
                     
                      file  = "ltest.dat"                                     ' see example data at end of source
                      b     = "Title that is quite long in the scheme of things"
                      xaxis = "X-Axis"
                      yaxis = "Y-Axis"
                     
                      lm = 70 : rm = 70 : tm = 40 : bm = 70                   ' Margins (pixels). Space for labels etc
                     
                      OPEN file$ FOR INPUT AS #1
                        INPUT#1, n
                        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(xmin > 0, MIN(a(r, 1), xmin), a(1,1))    ' initially set min to first value
                          ymin = IIF(ymin > 0, MIN(a(r, 2), ymin), a(1,2))    ' update in subsequent loops
                        NEXT
                     
                        PRINT "x" xmin "-" xmax, "y" ymin "-" ymax
                        xmin = IIF(xmin > 10, FIX (xmin/10)*10,  FIX(xmin))   ' round down minimum values &
                        xmax = IIF(xmax > 10, CEIL(xmax/10)*10, CEIL(xmax))   ' round up maximums for scale ranges
                        ymin = IIF(ymin > 10, FIX (ymin/10)*10,  FIX(ymin))
                        ymax = IIF(ymax > 10, CEIL(ymax/10)*10, CEIL(ymax))
                        PRINT "x scale" xmin "-" xmax
                        PRINT "y scale" ymin "-" ymax
                     
                        ' 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)
                     
                        ' calculate mark spacing based on data range / values
                        xmark = SWITCH(xmax-xmin =< 10, 0.5, xmax-xmin =< 100, 5, xmax-xmin =< 500, 50, xmax-xmin =< 1000, 100)  'eg
                        ymark = SWITCH(ymax-ymin =< 10, 0.5, ymax-ymin =< 100, 5, ymax-ymin =< 500, 50, ymax-ymin =< 1000, 100)
                        PRINT "xmark" xmark "ymark" ymark
                        ' ToDo? ..align scale with nearest .5, 5, 50 w/ bigger marks at MOD 1, 10, 100 etc??
                     
                        ' set custom coordinates for direct plotting of points
                        GRAPHIC SCALE (xmin-slm, ymax+stm) - (xmax+srm, ymin-sbm)  ' set custom coordinates for direct plotting of points
                     
                        ' draw axes marks, labels
                        GRAPHIC SET FONT hFont1                                 ' set font
                        GRAPHIC SET POS (xmax-(xmax-xmin)/2 - GRAPHIC(TEXT.SIZE.X, b$)/2, ymax + stm/2)
                        GRAPHIC PRINT b$
                        GRAPHIC SET POS (xmax-(xmax-xmin)/2 - GRAPHIC(TEXT.SIZE.X, xaxis$)/2, ymin - sbm/2)
                        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.X)*5)
                        IF xmax < 10 THEN
                          GRAPHIC PRINT FORMAT$(r, "0.0")                       ' print scale values
                        ELSE
                          GRAPHIC PRINT r
                        END IF
                        NEXT
                     
                        GRAPHIC SET FONT hFont2                                 ' font2 (rotated font)
                        GRAPHIC SET POS (xmin-slm*.9, ymax / 2)
                        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 ymax =< 10 THEN
                            GRAPHIC PRINT FORMAT$(r, "0.0")                     ' print scale values
                          ELSE
                          GRAPHIC PRINT r
                        END IF
                        NEXT
                     
                        ' 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
                          GRAPHIC SET CAPTION "Click screen for values"
                        NEXT r
                     
                      ' 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
                     
                        PRINT "Press any key to end."
                        CONSOLE SET FOCUS
                        WAITKEY$
                      CLOSE
                     
                    END FUNCTION
                    '------------------/
                     
                    #IF 0                       ' * metastatement defines (e.g. comment) section for compiler to ignore
                     
                    ' sample data sets for ltest.dat
                        11              11          11
                        1.4, 6.1        14, 61      140, 610
                        1.7, 2.4        17, 24      170, 240
                        2.4, 6.5        24, 65      240, 650
                        2.5, 6.9        25, 69      250, 690
                        2.7, 5.4        27, 54      270, 540
                        3.3, 9.3        33, 93      330, 930
                        3.4, 1.7        34, 17      340, 170
                        3.7, 8.9        37, 89      370, 890
                        4.0, 10.0       40, 100     400, 999
                        4.1, 9.0        41, 90      410, 900
                        4.2, 9.7        42, 97      420, 970
                     
                    #ENDIF
                    Rgds, Dave

                    Comment


                    • #11
                      Hi,

                      In the attached ZIP file please find the source code of a simple, auto-scaling, static plotter, the compiled EXE, and a 540 points testing CSV file.
                      Even though the testing file was made of sequential points, the program accepts scattered points, provided the highest 'y' value is in the last record of the file. Since the code includes just Basic commands, it compiles both in PBCC GW and PBWin.

                      I think it can be easily modified to fit the intended use.

                      Regards,
                      Attached Files
                      Last edited by Manuel Valdes; 7 Feb 2012, 06:24 AM. Reason: Fix some glitches in the program

                      Comment


                      • #12
                        That's brilliant, Dave. I will take a day or two to go through your program learning, as I go, because my wife fell and broke her leg the other day and I'm needed to help her get around.

                        Manuel: thanks for the link but when I try to download plotter.zip all I get is a PHP page. What am I doing wrong?

                        Comment


                        • #13
                          Hi Mike,

                          Just double Left (not right) click on the ZIP and select the 'Save' Option in the Pop Up Menu.

                          Regards


                          Later: In order to be able to download an attachment you must be logged in
                          Last edited by Manuel Valdes; 7 Feb 2012, 08:19 AM.

                          Comment


                          • #14
                            Downloaded OK for me using IE8 (have to be logged in to the site, not just visiting)

                            Another fine example Manuel, you do good work!

                            It'll take me a while to work through the source though - I'm renaming the variables as I go to help me to figure out what's being done where!

                            I can see though that you are scaling the data values before plotting which defintely helps in drawing the axes and positioning other elements on the window, independent of the data values.

                            I've been making life more difficult by the use of custom coordinates via GRAPHIC SCALE to enable direct plotting of the data without needing to scale the values.

                            Something I want to investigate is the use of SET CLIP to protect the other information, labels titles etc from the effects of using GRAPHIC SCALE to set up the plotting area... First, though I must finish mining your work to see what I can pinch

                            Thanks for sharing!
                            Rgds, Dave

                            Comment


                            • #15
                              Hi Dave,

                              It's good to learn that the program looks interesting to you. I never miss a post of yours, as I always have learned a lot from them. Sorry for my short nondescriptive variable names, a custom which came from my old TB and PBDOS days.

                              Since I have a few days left from my vacations, I am right now adding to the program the capability to handle two variables simultaneously, which doesn't look so difficult.

                              Best regards,
                              Last edited by Manuel Valdes; 7 Feb 2012, 02:53 PM.

                              Comment


                              • #16
                                Manuel, I've tried double-left click but it just downloads a PHP file. I use Firefox so I tried IE9 but with the same result. I'm also using a download accelerator, so I disabled it and it worked! Thanks.

                                Comment


                                • #17
                                  Dual Channel

                                  Hi,

                                  This is a dual channel version. It works OK provided both channels display variables with the same name, and having values in the same order of magnitud. Next step is to add an option for independent names and scaling.

                                  Regards
                                  Attached Files

                                  Comment


                                  • #18
                                    Final Version

                                    Hi,

                                    Final version of Dual Channel Plotter. I hope it can contain some useful ideas for Mike's purposes. I'll appreciate any suggestion, criticism or comment.

                                    Regards,
                                    Attached Files
                                    Last edited by Manuel Valdes; 9 Feb 2012, 05:36 AM.

                                    Comment


                                    • #19
                                      Scalable Single Channel Plotter

                                      This is an scalable parametric version of the code. Using the function keys F2 to F5, it's possible to change the width and height of the GW, along with the graphic itself.

                                      The testing file is now the US Consumer Price Index from Jan 1913 to Dec 2011 (the later is a forecast, since the official one will be available in a couple days)

                                      Regards,
                                      Attached Files

                                      Comment


                                      • #20
                                        Dave, we've been somewhat trumped by Manuel's tour de force! As you say, there's lots there to learn from but for my initial purposes the simple plot that we (well, you) were working on was adequate. All that had to be done was to solve the scaling of the axes and making sure that it coped with whatever numbers (including negative ones) that were thrown at it. I guess that some of the methods used by Manuel could be transferred to the simple plot but I admit that I'm lost in his code - trying run before I can walk in PBCC. I'll persevere and keep looking at his code to see how he scales the axes. Thanks, both of you, for taking up the challenge.

                                        Comment

                                        Working...
                                        X