Announcement

Collapse
No announcement yet.

gbChartMaster - Discussion

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

  • #41
    For Tim,
    Here's a very simple x,y scatterplot and trendline based on my previous trendline code.

    '
    Code:
    #COMPILE EXE
    #DEBUG ERROR ON
    #DEBUG DISPLAY ON
    #DIM ALL
    #INCLUDE ONCE "WIN32API.INC"
    
    FUNCTION PBMAIN () AS LONG
    LOCAL n, i AS LONG
    LOCAL slope, offset AS EXT
    LOCAL xMin,xMax AS EXT
    LOCAL yMin,yMax,ypmax AS EXT
    LOCAL hGraph AS DWORD
    LOCAL strGType AS STRING
    'Build a data set
        n = 99 ' 100 points -1 because we are using a zero based array
        xMin = 0: xMax = 100 ' range to plot on x Axis.
        DIM dp(n,1) AS EXT ' x values in dp(i,0), y values in dp(i,1)
        RANDOMIZE TIMER
        'Comment out one of the two following plot types
        'scatter plot
         strGType = "Scatter Plot"
        FOR i = 0 TO n
            dp(i,0) = RND(1,100)
            dp(i,1) =  RND(1,100)
            IF dp(i,1) > ypmax THEN ypmax = dp(i,1)
        NEXT
        'linear plot
    '    strGType = "Linear Plot"
    '    FOR i = 0 TO n
    '        dp(i,0) = i
    '        dp(i,1) =  RND(1,20) + i * RND(1,5) ' slowly increment average y
    '        IF dp(i,1) > ypmax THEN ypmax = dp(i,1)          
    '    NEXT
        'Get linear trendline formula
        Trendline dp(),slope,offset
        yMin = offset: yMax = slope * xMax + offset
        ? "Trendline is:  y = " & FORMAT$(slope,"0.00") & IIF$(offset >= 0,"x +","x ")  & FORMAT$(offset,"0.00") & $CRLF _
        &  USING$("Line from (#.# , #.# ) to (#.# , #.#)",xMin,yMin,xMax,yMax)
    
       '===== DRAW GRAPH ===================
       GRAPHIC WINDOW NEW strGType,100,100,600,600 TO hGraph
       'scale window
       GRAPHIC SCALE (-10,ypMax+10) - (xMax+10,-10) 'invert co-ordinate system and put border around graph
       'draw axes
       GRAPHIC LINE (0,0) - (0,ypmax)
       GRAPHIC LINE (0,0) - (xMax,0)
       'Plot points
       FOR i = 0 TO n
           GRAPHIC SET POS (dp(i,0),dp(i,1))
           GRAPHIC PRINT "x"
           'graphic line (dp(i,0),0)-  (dp(i,0),dp(i,1)),%BLACK
    
       NEXT
       'Draw Trendline
       GRAPHIC LINE (xmin,ymin) - (xMax,yMax)
    
       GRAPHIC WAITKEY$
       GRAPHIC WINDOW END hGraph
    END FUNCTION
    
    FUNCTION TrendLine(a() AS EXT,s AS EXT, o AS EXT) AS LONG
    LOCAL i,n,sX,sY,sXY,sX2,sY2 AS EXT
        n = UBOUND(a())
        FOR i = 0 TO n
            sX +=  a(i,0)
            sy +=  a(i,1)
            sXY += a(i,0) * a(i,1)
            sx2 += a(i,0)^2
            sY2 += a(i,1)^2
        NEXT
        s =  (n * sXY - sX*sY)/(n*sX2 - sX^2)
        o = (sY - s *sX)/n
     END FUNCTION
    '

    Comment


    • #42
      Stuart, thanks for responding to Tim!

      I've just not been able to break free to help out.

      Tim, if you still want something from me, please speak up!

      Comment


      • #43
        Tim,

        This thread might be of interest too..
        https://forum.powerbasic.com/forum/u...le-scatterplot
        Rgds, Dave

        Comment


        • #44
          Thank you Everyone , these are very good straight lines graphing programs

          Comment

          Working...
          X