Jump to content
Sign in to follow this  
BuckMaster

Updating Graph

Recommended Posts

BuckMaster

For my first example script I've created a graph that updates at a set frequency and can graph a large number of variables at a time,

any color can be changed and the size and scale of the graph can change as well

please post any feedback or suggestions to make it better

#include <ScreenCapture.au3>
#include <GDIPlus.au3>
#include <Misc.au3>
#include <WinAPI.au3>

_GDIPlus_Startup ()

 opt( "MouseCoordMode", 2)

Global $hGUI, $hGraphic, $hPen, $hBackColor, $hBackground, $hBoarder, $hPen, $hLines
Global $X, $Y, $W, $H
Global $Frequency = 5
Global $Step = -1
Global $RedrawStart = 0
Global $Count = 4
Global $Redraw = False

Global $hBackgroundColor = 0xFFEBEBEB
Global $hAxisLinesColor = 0xFFDBDBDB
Global $hWindowBackColor = 0xFFEEEEEE
Global $hBoarderColor = 0xFF000000

Dim $LastDiff = 0
Dim $TimerSet = False
Dim $Timer

Global $Variables[5] = [ 4, 20, 40, 60, 80 ] ; add as many varriables as you want, just make sure to add a color for it too
Dim $LastVars[5] = [ 4, 20, 40, 60, 80 ]
Dim $Colors[5] = [ 4, 0xC5B6316C, 0xC52FAA96, 0xC5FF0000, 0xC566CC33]

$hGUI = GUICreate("Graph", 500, 300)
GUISetState()

$hGraph = _GUICtrlGraph_Create( $hGUI, 50, 50, 400, 200, $Frequency, $Variables, $Colors)
_GUICtrlGraph_SetTitle( $hGraph, "A Graph of Randomness" )


While 1
    $hMsg = GUIGetMsg()
    Switch $hMsg
        Case -3
            _GDIPlus_GraphicsDispose ($hGraphic)
            _GDIPlus_PenDispose( $hBoarder )
            _GDIPlus_PenDispose( $hLines )
            _GDIPlus_PenDispose( $hBackground )
            _GDIPlus_PenDispose( $hBackColor )
            _GDIPlus_PenDispose( $hPen )
            _GDIPlus_Shutdown ()
            Exit
    EndSwitch
    If $TimerSet = True Then
        $Diff = TimerDiff($Timer)
        If $LastDiff + ($Frequency*100) < $Diff Then
            _RandomizeVarriables()
            If $Redraw = True Then
                $hBitmap2 = _ScreenCapture_CaptureWnd("", $hGUI, $X +$Frequency + 4, $Y + 26 , $W+$X, $H+$Y+24, False)
                $hImage2 = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap2)
                _GDIPlus_GraphicsFillRect( $hGraphic, $X + 1, $Y + 1, $W - 2, $H - 2, $hBackground)
                _GUICtrlGraph_GridLinesY( $Frequency, $X, $Y, $W, $H )
                _GDIPlus_GraphicsDrawImage($hGraphic, $hImage2, $X + 1, $Y + 1)
                _GUICtrlGraph_GridNumbers($RedrawStart, $X, $Y, $W, $H, $Frequency, "X" )
                If $Count = 4 Then
                    $Count = 0
                    _GDIPlus_GraphicsDrawLine( $hGraphic, $X + $W - $Frequency, $Y +1 , $X + $W - $Frequency, $Y + $H - 1, $hLines)
                EndIf
                $Count += 1
                $RedrawStart += $Frequency
            EndIf
            _GDIPlus_GraphicsDrawRect( $hGraphic, $X, $Y, $W, $H, $hBoarder)
            $LastDiff = $Diff
            For $i = 1 to $Variables[0] Step 1
                If $Variables[$i] < 1 Then
                    $Variables[$i] = 1
                EndIf
                $hPen = _GDIPlus_PenCreate ( $Colors[$i], 2 )
                If ($Step * $Frequency) + $Frequency  >= $W Then
                    $Redraw = True
                    _GDIPlus_GraphicsDrawLine( $hGraphic, $X + $W-$Frequency-3, $Y + $H - $LastVars[$i], $X+$W, $Y + $H - $Variables[$i], $hPen)
                    $LastVars[$i] = $Variables[$i]
                Else
                    If $Step <> -1 Then
                        _GDIPlus_GraphicsDrawLine( $hGraphic, $X + ($Step * $Frequency)-$RedrawStart, $Y + $H - $LastVars[$i], $X + ($Step * $Frequency) + $Frequency-$RedrawStart, $Y + $H - $Variables[$i], $hPen)
                    EndIf
                    $LastVars[$i] = $Variables[$i]
                EndIf
            Next
            $Step += 1
        EndIf
    EndIf
    Sleep(1)
WEnd

Func _RandomizeVarriables()
    For $i = 1 to $Variables[0] Step 1
        If Random() < 0.5 Then
            $Variables[$i] += 1
        Else
            $Variables[$i] -= 1
        EndIf
    Next
EndFunc

Func _GUICtrlGraph_Create( $hWnd, $hX, $hY, $hW, $hH, $hFreq, $hVar, $hColor )
    If $hX <> "" and $hY <> "" and $hW <> "" and $hH <> "" and $hFreq <> "" Then
        Dim $X = $hX, $Y = $hY, $H = $hH, $W = $hW
        Dim $StartTime = 0
        $hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hGUI)
        $hBoarder = _GDIPlus_PenCreate ($hBoarderColor)
        $hLines = _GDIPlus_PenCreate ($hAxisLinesColor)
        $hBackground = _GDIPlus_BrushCreateSolid($hBackgroundColor)
        $hBackColor = _GDIPlus_BrushCreateSolid($hWindowBackColor)
        _GDIPlus_GraphicsSetSmoothingMode($hGraphic, 2)
        _GDIPlus_GraphicsDrawRect( $hGraphic, $hX, $hY, $hW, $hH, $hBoarder)
        _GDIPlus_GraphicsFillRect( $hGraphic, $hX + 1, $hY + 1, $hW - 2, $hH - 2, $hBackground)
        _GUICtrlGraph_Grid($hFreq, $hX, $hY, $hW, $hH, 0, "X" )
        _GUICtrlGraph_GridNumbers( $StartTime, $hX, $hY, $hW, $hH, $hFreq , "X" )
        _GUICtrlGraph_Grid($hFreq, $hX, $hY, $hW, $hH, 0, "Y" )
        _GUICtrlGraph_GridNumbers( $StartTime, $hX, $hY, $hW, $hH, $hFreq , "Y" )
        _GUICtrlGraph_GridLinesY( $hFreq, $hX, $hY, $hW, $hH )
        $TimerSet = True
        $Timer = TimerInit()
    EndIf
EndFunc

Func _GUICtrlGraph_Grid($hFreq, $hX, $hY, $hW, $hH, $hStart, $hAxis )
    _GDIPlus_GraphicsDrawString ($hGraphic, $hStart, $hX - 2, $hY + $hH + 6, "Arial", 5)
    If $hAxis = "X" Then
        _GDIPlus_GraphicsDrawLine( $hGraphic, $hX, $hY + $hH, $hX , $hY + $hH + 4, $hBoarder)
        $Number = 1
        For $i = $hFreq to $hW Step $hFreq
            If $Number = 1 or $Number = 2 or $Number = 3 Then
                _GDIPlus_GraphicsDrawLine( $hGraphic, $hX + $i, $hY + $hH, $hX + $i , $hY + $hH + 2, $hBoarder)
            ElseIf $Number = 4 Then
                If $i = $W Then
                    _GDIPlus_GraphicsDrawLine( $hGraphic, $hX + $i, $hY + $hH, $hX + $i , $hY + $hH + 4, $hBoarder)
                Else
                    _GDIPlus_GraphicsDrawLine( $hGraphic, $hX + $i, $hY + $hH, $hX + $i , $hY + $hH + 4, $hBoarder)
                    _GDIPlus_GraphicsDrawLine( $hGraphic, $hX + $i, $hY +1 , $hX + $i , $hY + $hH - 1, $hLines)
                EndIf
                $Number = 0
            EndIf
            $Number += 1
        Next
    ElseIf $hAxis = "Y" Then
        _GDIPlus_GraphicsDrawLine( $hGraphic, $hX - 4 , $hY+ $hH, $hX , $hY + $hH, $hBoarder)
        $Number = 1
        For $i = $hFreq to $hH Step $hFreq
            If $Number = 1 or $Number = 2 or $Number = 3 Then
                _GDIPlus_GraphicsDrawLine( $hGraphic, $hX - 2 , $hY + $hH - $i, $hX , $hY + $hH - $i, $hBoarder)
            ElseIf $Number = 4 Then
                If $i = $H Then
                    _GDIPlus_GraphicsDrawLine( $hGraphic, $hX - 4 , $hY+ $hH - $i, $hX , $hY + $hH - $i, $hBoarder)
                Else
                    _GDIPlus_GraphicsDrawLine( $hGraphic, $hX - 4 , $hY + $hH - $i, $hX , $hY + $hH - $i, $hBoarder)
                EndIf
                $Number = 0
            EndIf
            $Number += 1
        Next
    EndIf
EndFunc

Func _GUICtrlGraph_GridNumbers( $hStart, $hX, $hY, $hW, $hH, $hFreq, $hAxis)
    If $hAxis = "X" Then
        _GDIPlus_GraphicsFillRect( $hGraphic, $hX - 2, $hY + $hH + 5, $hW+10, 9, $hBackcolor)
        _GDIPlus_GraphicsDrawString ($hGraphic, $hStart, $hX - 2, $hY + $hH + 6, "Arial", 5)
        $Number = 1
        For $i = $hFreq to $hW Step $hFreq
            If $Number = 4 Then
                _GDIPlus_GraphicsDrawString ($hGraphic, $i + $hStart, $hX + $i - (StringLen($i)*2), $hY + $hH + 6, "Arial", 5)
                $Number = 0
            EndIf
            $Number += 1
        Next
    ElseIf $hAxis = "Y" Then
        _GDIPlus_GraphicsFillRect( $hGraphic, $hX - 15, $hY, 11, $hH, $hBackcolor)
        _GDIPlus_GraphicsDrawString ($hGraphic, $hStart, $hX - 11, $hY + $hH -3, "Arial", 5)
        $Number = 1
        For $i = $hFreq to $hH Step $hFreq
            If $Number = 4 Then
                _GDIPlus_GraphicsDrawString ($hGraphic, $i + $hStart, $hX - 7 - (StringLen($i)*4), $hY + $hH - $i - 3, "Arial", 5)
                $Number = 0
            EndIf
            $Number += 1
        Next
    EndIf
EndFunc

Func _GUICtrlGraph_GridLinesY($hFreq, $hX, $hY, $hW, $hH )
    $Number = 1
    For $i = $hFreq to $hH Step $hFreq
        If $Number = 4 Then
            _GDIPlus_GraphicsDrawLine( $hGraphic, $hX + 1 , $hY + $hH - $i , $hX + $hW + 1, $hY + $hH - $i, $hLines)
            $Number = 0
        EndIf
        $Number += 1
    Next
EndFunc

Func _GUICtrlGraph_SetTitle( $hGraph, $hTitle )
    _GDIPlus_GraphicsDrawString ($hGraphic, $hTitle, ($X + ($W/2)) - (StringLen($hTitle)*3), $Y - 12, "Arial", 7)
EndFunc

Share this post


Link to post
Share on other sites
smashly

Hi,

I like your script and thanks for sharing.

You need to clean up resources a little better.

Your not disposing of the $hBitmap2 or $hImage2 when your finished with them

Your re-creating a pen over and over in your loop and only disposing of the 1 pen.

For $i = 1 to $Variables[0] Step 1
                If $Variables[$i] < 1 Then
                    $Variables[$i] = 1
                EndIf
                $hPen = _GDIPlus_PenCreate ( $Colors[$i], 2 ) ;<- keep track of every pen or brush you create and dispose when finished with. Create the pen outside the loop and use _GDIPlus_PenSetColor if you only want to change colors. 
                If ($Step * $Frequency) + $Frequency  >= $W Then
                    $Redraw = True
                    _GDIPlus_GraphicsDrawLine( $hGraphic, $X + $W-$Frequency-3, $Y + $H - $LastVars[$i], $X+$W, $Y + $H - $Variables[$i], $hPen)
                    $LastVars[$i] = $Variables[$i]
                Else
                    If $Step <> -1 Then
                        _GDIPlus_GraphicsDrawLine( $hGraphic, $X + ($Step * $Frequency)-$RedrawStart, $Y + $H - $LastVars[$i], $X + ($Step * $Frequency) + $Frequency-$RedrawStart, $Y + $H - $Variables[$i], $hPen)
                    EndIf
                    $LastVars[$i] = $Variables[$i]
                EndIf
            Next

When the graph lines hits the end then something odd is happening with the graph is being drawn diagonally offset over and over again and again while memory use keeps climbing.

Keep up the good effort and I hope you don't mind my constructive advice.

Cheers

Edited by smashly

Share this post


Link to post
Share on other sites
BuckMaster

Thanks for pointing out my screw ups,

I've made multiple changes and fixed all the alignment errors,

Its more of a UDF now and seems to be working good,

Cant exactly figure out why the memory keeps climbing by a little bit.

Graph Example.au3

#include <GDIPlus.au3>
#include <Misc.au3>
#include <WinAPI.au3>
#include <Graph.au3>

_GDIPlus_Startup ()

 opt( "MouseCoordMode", 2)


Global $Variables[5] = [ 4, 20, 40, 60, 80 ] ; add as many varriables as you want, just make sure to add a color for it too
Dim $LastVars[5] = [ 4, 20, 40, 60, 80 ]
Dim $Colors[5] = [ 4, 0xC5B6316C, 0xC52FAA96, 0xC5FF0000, 0xC566CC33]

$hGUI = GUICreate("Graph", 500, 300, -1, -1, 0)
GUISetState()

$hGraph = _GUICtrlGraph_Create( $hGUI, 50, 25, 400, 200, 5, $Variables, $Colors)
_GUICtrlGraph_SetTitle( $hGraph, "A Graph of Randomness" )


While 1
    $hMsg = GUIGetMsg()
    Switch $hMsg
        Case -3
            _GDIPlus_GraphicsDispose ($hGraphic)
            _GDIPlus_PenDispose( $hBoarder )
            _GDIPlus_PenDispose( $hLines )
            _GDIPlus_PenDispose( $hBackground )
            _GDIPlus_PenDispose( $hBackColor )
            _GDIPlus_PenDispose( $hPen )
            _GDIPlus_Shutdown ()
            Exit
    EndSwitch
WEnd

Graph.au3

#include 
#include 
#include 

Global $hGUI, $hGraphic, $hPen, $hBackColor, $hBackground, $hBoarder, $hPen, $hLines, $Variables, $Colors, $LastVars, $hWindow
Global $hX, $hY, $hW, $hH

Global $hDifferential = 0
Global $hRedrawStart = 0
Global $hNumber2 = 0
Global $hFixVar = 0
Global $hStep = -1
Global $hCount = 4
Global $hFreq = 5

Global $hRedraw = False
Global $hToggle = True
Global $hFirst = True

Global $hBackgroundColor = 0xFFEBEBEB
Global $hAxisLinesColor = 0xFFDBDBDB
Global $hWindowBackColor = 0xFFEEEEEE
Global $hBoarderColor = 0xFF000000

Global $hTimer

Func _GUICtrlGraph_Create( $hWnd, $X, $Y, $W, $H, $aFreq, $hVar, $hColor )
    If $X <> "" and $Y <> "" and $W <> "" and $H <> "" and $aFreq <> "" Then
        dim $hFreq = $aFreq
        Dim $hX = $X, $hY = $Y, $hW = $W, $hH = $H
        Dim $Variables = $hVar
        Dim $hWindow = $hWnd
        Dim $StartTime = 0
        $hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hGUI)
        $hBoarder = _GDIPlus_PenCreate ($hBoarderColor)
        $hLines = _GDIPlus_PenCreate ($hAxisLinesColor)
        $hBackground = _GDIPlus_BrushCreateSolid($hBackgroundColor)
        $hBackColor = _GDIPlus_BrushCreateSolid($hWindowBackColor)
        $hPen = _GDIPlus_PenCreate ( 0xFF000000, 2 )
        _GDIPlus_GraphicsSetSmoothingMode($hGraphic, 2)
        _GDIPlus_GraphicsFillRect( $hGraphic, $hX + 1, $hY + 1, $hW - 2, $hH - 2, $hBackground)
        _GUICtrlGraph_Grid($hFreq, $hX, $hY, $hW, $hH, 0, "X" )
        _GUICtrlGraph_GridNumbers( $StartTime, $hX, $hY, $hW, $hH, $hFreq , "X" )
        _GUICtrlGraph_Grid($hFreq, $hX, $hY, $hW, $hH, 0, "Y" )
        _GUICtrlGraph_GridNumbers( $StartTime, $hX, $hY, $hW, $hH, $hFreq , "Y" )
        _GUICtrlGraph_GridLinesY( $hFreq, $hX, $hY, $hW, $hH )
        _GDIPlus_GraphicsDrawRect( $hGraphic, $hX, $hY, $hW, $hH, $hBoarder)
        AdlibRegister( "_GUICtrlGraph_Refresh" , $hFreq*100 )
    EndIf
EndFunc

Func _GUICtrlGraph_Refresh()
    _RandomizeVarriables()
    If $hRedraw = True Then
        $hBitmap2 = _ScreenCapture_CaptureWnd("", $hWindow, $hX + $hFreq + 4, $hY + 26 , $hW + $hX, $hH + $hY + 24, False)
        $hImage2 = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap2)
        _GDIPlus_GraphicsFillRect( $hGraphic, $hX + 1, $hY + 1, $hW - 2, $hH - 2, $hBackground)
        _GUICtrlGraph_GridLinesY( $hFreq, $hX, $hY, $hW, $hH )
        _GDIPlus_GraphicsDrawImage($hGraphic, $hImage2, $hX + 1, $hY + 1)
        _GUICtrlGraph_GridNumbers($hRedrawStart, $hX, $hY, $hW, $hH, $hFreq, "X" )
        If $hFreq >= 4 Then
            $hDifferential = ( 4 - $hFreq ) * 2 + $hFreq
            If $hNumber2 >= 4 Then
                $hNumber2 = 0
                _GDIPlus_GraphicsDrawLine( $hGraphic, $hX + $hW - $hFreq-$hFixVar, $hY +1 , $hX + $hW - $hFreq-$hFixVar, $hY + $hH - 1, $hLines)
            EndIf
        ElseIf $hFreq < 4 Then
            $hDifferential = ( 4 - $hFreq ) * 2 + $hFreq
            If $hNumber2 >= $hDifferential Then
                $hNumber2 = 0
                _GDIPlus_GraphicsDrawLine( $hGraphic, $hX + $hW - $hFreq-$hFixVar, $hY +1 , $hX + $hW - $hFreq-$hFixVar, $hY + $hH - 1, $hLines)
            EndIf
        EndIf
        $hNumber2 += 1
        $hRedrawStart += $hFreq

        _WinAPI_DeleteObject( $hBitMap2 )
        _GDIPlus_GraphicsDispose( $hImage2 )
    EndIf
    _GDIPlus_GraphicsDrawRect( $hGraphic, $hX, $hY, $hW, $hH, $hBoarder)
    For $i = 1 to $Variables[0] Step 1
        If $Variables[$i] < 1 Then
            $Variables[$i] = 1
        ElseIf $Variables[$i] > $hH Then
            $Variables[$i] = $hH - 1
        EndIf
        _GDIPlus_PenSetColor( $hPen, $Colors[$i] )
        If ($hStep * $hFreq) + ($hFreq*2) >= $hW Then
            $hRedraw = True
            If $hFirst = True Then
                _GDIPlus_GraphicsDrawLine( $hGraphic, $hX + $hW-$hFreq-4, $hY + $hH - $LastVars[$i], $hX+$hW, $hY + $hH - $Variables[$i], $hPen)
            Else
                _GDIPlus_GraphicsDrawLine( $hGraphic, $hX + $hW-$hFreq-3, $hY + $hH - $LastVars[$i], $hX+$hW, $hY + $hH - $Variables[$i], $hPen)
            EndIf
            If $i = $Variables[0] Then
                $hFirst = False
            EndIf
            $LastVars[$i] = $Variables[$i]
        Else
            If $hStep <> -1 Then
                _GDIPlus_GraphicsDrawLine( $hGraphic, $hX + ($hStep * $hFreq), $hY + $hH - $LastVars[$i], $hX + ($hStep * $hFreq) + $hFreq+1, $hY + $hH - $Variables[$i], $hPen)
            EndIf
            $LastVars[$i] = $Variables[$i]
        EndIf
    Next
    $hStep += 1
EndFunc

Func _GUICtrlGraph_Grid($hFreq, $hX, $hY, $hW, $hH, $hStart, $hAxis )
    Local $LastNumber
    _GDIPlus_GraphicsDrawString ($hGraphic, $hStart, $hX - 2, $hY + $hH + 6, "Arial", 5)
    If $hAxis = "X" Then
        _GDIPlus_GraphicsDrawLine( $hGraphic, $hX, $hY + $hH, $hX , $hY + $hH + 4, $hBoarder)
        For $i = $hFreq to $hW Step $hFreq
            $hNumber2 += 1
            If $hW < $i + $hFreq Then
                $hFixVar = $hW - $i
            EndIf
            If $hFreq >= 4 Then
                If $hNumber2 < 4 Then
                    _GDIPlus_GraphicsDrawLine( $hGraphic, $hX + $i, $hY + $hH, $hX + $i , $hY + $hH + 2, $hBoarder)
                ElseIf $hNumber2 = 4 Then
                    If $i = $hW Then
                        ConsoleWrite("! hW = "&$hW&"   hFreq = "&$hFreq&"   i = "&$i&@CRLF)
                        _GDIPlus_GraphicsDrawLine( $hGraphic, $hX + $i, $hY + $hH, $hX + $i , $hY + $hH + 4, $hBoarder)
                        $hNumber2 = 4
                    Else
                        _GDIPlus_GraphicsDrawLine( $hGraphic, $hX + $i, $hY + $hH, $hX + $i , $hY + $hH + 4, $hBoarder)
                        _GDIPlus_GraphicsDrawLine( $hGraphic, $hX + $i, $hY +1 , $hX + $i , $hY + $hH - 1, $hLines)
                        $hNumber2 = 0
                    EndIf
                EndIf
            ElseIf $hFreq < 4 Then
                $hDifferential = ( 4 - $hFreq ) * 2 + $hFreq
                If $hNumber2 <> $hDifferential Then
                    _GDIPlus_GraphicsDrawLine( $hGraphic, $hX + $i, $hY + $hH, $hX + $i , $hY + $hH + 2, $hBoarder)
                Else
                    If $i = $hW Then
                        _GDIPlus_GraphicsDrawLine( $hGraphic, $hX + $i, $hY + $hH, $hX + $i , $hY + $hH + 4, $hBoarder)
                    Else
                        _GDIPlus_GraphicsDrawLine( $hGraphic, $hX + $i, $hY + $hH, $hX + $i , $hY + $hH + 4, $hBoarder)
                        _GDIPlus_GraphicsDrawLine( $hGraphic, $hX + $i, $hY +1 , $hX + $i , $hY + $hH - 1, $hLines)
                    EndIf
                    $hNumber2 = 0
                EndIf
            EndIf
        Next
    ElseIf $hAxis = "Y" Then
        _GDIPlus_GraphicsDrawLine( $hGraphic, $hX - 4 , $hY+ $hH, $hX , $hY + $hH, $hBoarder)
        $hNumber = 1
        For $i = $hFreq to $hH Step $hFreq
            If $hNumber = 1 or $hNumber = 2 or $hNumber = 3 Then
                _GDIPlus_GraphicsDrawLine( $hGraphic, $hX - 2 , $hY + $hH - $i, $hX , $hY + $hH - $i, $hBoarder)
            ElseIf $hNumber = 4 Then
                If $i = $hH Then
                    _GDIPlus_GraphicsDrawLine( $hGraphic, $hX - 4 , $hY+ $hH - $i, $hX , $hY + $hH - $i, $hBoarder)
                Else
                    _GDIPlus_GraphicsDrawLine( $hGraphic, $hX - 4 , $hY + $hH - $i, $hX , $hY + $hH - $i, $hBoarder)
                EndIf
                $hNumber = 0
            EndIf
            $hNumber += 1
        Next
    EndIf
EndFunc

Func _GUICtrlGraph_GridNumbers( $hStart, $hX, $hY, $hW, $hH, $hFreq, $hAxis)
    If $hAxis = "X" Then
        _GDIPlus_GraphicsFillRect( $hGraphic, $hX - 2, $hY + $hH + 5, $hW+14, 9, $hBackcolor)
        _GDIPlus_GraphicsDrawString ($hGraphic, $hStart, $hX - 2, $hY + $hH + 6, "Arial", 5)
        $hNumber = 1
        For $i = $hFreq to $hW Step $hFreq
            If $hFreq >= 4 Then
                If $hNumber = 4 Then
                    _GDIPlus_GraphicsDrawString ($hGraphic, $i + $hStart, $hX + $i - (StringLen($i)*2), $hY + $hH + 6, "Arial", 5)
                    $hNumber = 0
                EndIf
            Else
                $hDifferential = ( 4 - $hFreq ) * 2 + $hFreq
                If $hNumber = $hDifferential Then
                    If StringLen( $i + $hStart ) > 3 Then
                        _GDIPlus_GraphicsDrawString ($hGraphic, $i + $hStart, $hX + $i - (StringLen($i)*2), $hY + $hH + 6, "Arial", 5)
                        $hNumber = 0
                    Else
                        _GDIPlus_GraphicsDrawString ($hGraphic, $i + $hStart, $hX + $i - (StringLen($i)*2), $hY + $hH + 6, "Arial", 5)
                        $hNumber = 0
                    EndIf
                EndIf
            EndIf
            $hNumber += 1
        Next
    ElseIf $hAxis = "Y" Then
        _GDIPlus_GraphicsFillRect( $hGraphic, $hX - 15, $hY, 11, $hH, $hBackcolor)
        _GDIPlus_GraphicsDrawString ($hGraphic, $hStart, $hX - 11, $hY + $hH -3, "Arial", 5)
        $hNumber = 1
        For $i = $hFreq to $hH Step $hFreq
            If $hNumber = 4 Then
                _GDIPlus_GraphicsDrawString ($hGraphic, $i + $hStart, $hX - 7 - (StringLen($i)*4), $hY + $hH - $i - 3, "Arial", 5)
                $hNumber = 0
            EndIf
            $hNumber += 1
        Next
    EndIf
EndFunc

Func _GUICtrlGraph_GridLinesY($hFreq, $hX, $hY, $hW, $hH )
    $hNumber = 1
    For $i = $hFreq to $hH Step $hFreq
        If $hNumber = 4 Then
            _GDIPlus_GraphicsDrawLine( $hGraphic, $hX + 1 , $hY + $hH - $i , $hX + $hW + 1, $hY + $hH - $i, $hLines)
            $hNumber = 0
        EndIf
        $hNumber += 1
    Next
EndFunc

Func _GUICtrlGraph_SetTitle( $hGraph, $hTitle )
    _GDIPlus_GraphicsFillRect( $hGraphic, ($hX + ($hW/2)) - (StringLen($hTitle)*2) - 2, $hY - 12, (StringLen($hTitle)*6), 11, $hBackcolor)
    _GDIPlus_GraphicsDrawString ($hGraphic, $hTitle, ($hX + ($hW/2)) - (StringLen($hTitle)*2), $hY - 12, "Arial", 7)
EndFunc

Func _GUICtrlGraph_SetAxisTitleX( $hTitle )
    _GDIPlus_GraphicsDrawString ($hGraphic, $hTitle, ($hX + ($hW/2)) - (StringLen($hTitle)*2), $hY + $hH + 15, "Georgia", 7)
EndFunc

Func _GUICtrlGraph_SetAxisTitleY( $hTitle )
    _GDIPlus_GraphicsDrawString ($hGraphic, $hTitle, $hX - 24, $hY + ($hH/2) - (StringLen($hTitle)*2), "Georgia", 7, 0x0003)
EndFunc

Func _GUICtrlGraph_SetBkColor( $hColor )
    $hBackgroundColor = $hColor
EndFunc

Func _GUICtrlGraph_SetAxisGridColor( $hColor )
    $hAxisLinesColor = $hColor
EndFunc

Func _GUICtrlGraph_SetWindowColor( $hColor )
    $hWindowBackColor = $hColor
EndFunc

Func _GUICtrlGraph_SetBoarderColor( $hColor )
    $hBoarderColor = $hColor
EndFunc

Func _RandomizeVarriables()
    For $i = 1 to $Variables[0] Step 1
        $rand = Random()
        If $rand < 0.25 Then
            $Variables[$i] += 1
        ElseIf $rand > 0.25 and $rand < 0.5 Then
            $Variables[$i] += 2
        ElseIf $rand > 0.5 and $rand < 0.75 Then
            $Variables[$i] -= 1
        ElseIf $rand > 0.75 and $rand < 1 Then
            $Variables[$i] -= 2
        EndIf
    Next
EndFunc
Edited by BuckMaster

Share this post


Link to post
Share on other sites
asdf8

BuckMaster

There are two problems:

1. if graph to remove for borders of the screen or overlay other window - the graph is cleared

Posted Image

2. when graph gets to right border, is got such:

Posted Image

Share this post


Link to post
Share on other sites
BuckMaster

@asdf8 - problem #1 is that its GDI+ and i dont really know a way not to make it erase if dragged off the screen,

i could make it redraw the entire graph each time but it might make it flash

Problem #2 I think is your windows theme has a different Window to Client spacing and it was throwing it off when it was copying

Solution

#include <ScreenCapture.au3>
#include <Timers.au3>
#include <GDIPlus.au3>

Global $hGUI, $hGraphic, $hPen, $hBackColor, $hBackground, $hBoarder, $hPen, $hLines, $Variables, $Colors, $LastVars, $hWindow
Global $hX, $hY, $hW, $hH

Global $hDifferential = 0
Global $hRedrawStart = 0
Global $hNumber2 = 0
Global $hFixVar = 0
Global $hStep = -1
Global $hCount = 4
Global $hFreq = 5
Global $ClientAreaSpacingX = 0
Global $ClientAreaSpacingY = 0

Global $hRedraw = False
Global $hToggle = True
Global $hFirst = True

Global $hBackgroundColor = 0xFFEBEBEB
Global $hAxisLinesColor = 0xFFDBDBDB
Global $hWindowBackColor = 0xFFEEEEEE
Global $hBoarderColor = 0xFF000000

Global $hTimer

Func _GUICtrlGraph_Create( $hWnd, $X, $Y, $W, $H, $aFreq, $hVar, $hColor )
    If $X <> "" and $Y <> "" and $W <> "" and $H <> "" and $aFreq <> "" Then
        $ClientAreaSpacingX = (_WinAPI_GetWindowWidth($hWnd) - _WinAPI_GetClientWidth($hWnd))/2
        $ClientAreaSpacingY = _WinAPI_GetWindowHeight($hWnd) - _WinAPI_GetClientHeight($hWnd) - $ClientAreaSpacingX
        dim $hFreq = $aFreq
        Dim $hX = $X, $hY = $Y, $hW = $W, $hH = $H
        Dim $Variables = $hVar
        Dim $hWindow = $hWnd
        Dim $StartTime = 0
        $hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hGUI)
        $hBoarder = _GDIPlus_PenCreate ($hBoarderColor)
        $hLines = _GDIPlus_PenCreate ($hAxisLinesColor)
        $hBackground = _GDIPlus_BrushCreateSolid($hBackgroundColor)
        $hBackColor = _GDIPlus_BrushCreateSolid($hWindowBackColor)
        $hPen = _GDIPlus_PenCreate ( 0xFF000000, 2 )
        _GDIPlus_GraphicsSetSmoothingMode($hGraphic, 2)
        _GDIPlus_GraphicsFillRect( $hGraphic, $hX + 1, $hY + 1, $hW - 2, $hH - 2, $hBackground)
        _GUICtrlGraph_Grid($hFreq, $hX, $hY, $hW, $hH, 0, "X" )
        _GUICtrlGraph_GridNumbers( $StartTime, $hX, $hY, $hW, $hH, $hFreq , "X" )
        _GUICtrlGraph_Grid($hFreq, $hX, $hY, $hW, $hH, 0, "Y" )
        _GUICtrlGraph_GridNumbers( $StartTime, $hX, $hY, $hW, $hH, $hFreq , "Y" )
        _GUICtrlGraph_GridLinesY( $hFreq, $hX, $hY, $hW, $hH )
        _GDIPlus_GraphicsDrawRect( $hGraphic, $hX, $hY, $hW, $hH, $hBoarder)
        AdlibRegister( "_GUICtrlGraph_Refresh" , $hFreq*100 )
    EndIf
EndFunc

Func _GUICtrlGraph_Refresh()
    _RandomizeVarriables()
    If $hRedraw = True Then
        $hBitmap2 = _ScreenCapture_CaptureWnd("", $hWindow, $hX + $hFreq + $ClientAreaSpacingX + 1, $hY + $ClientAreaSpacingY + 1 , $hW + $hX, $hH + $hY + $ClientAreaSpacingY - 1, False)
        $hImage2 = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap2)
        _GDIPlus_GraphicsFillRect( $hGraphic, $hX + 1, $hY + 1, $hW - 2, $hH - 2, $hBackground)
        _GUICtrlGraph_GridLinesY( $hFreq, $hX, $hY, $hW, $hH )
        _GDIPlus_GraphicsDrawImage($hGraphic, $hImage2, $hX + 1, $hY + 1)
        _GUICtrlGraph_GridNumbers($hRedrawStart, $hX, $hY, $hW, $hH, $hFreq, "X" )
        If $hFreq >= 4 Then
            $hDifferential = ( 4 - $hFreq ) * 2 + $hFreq
            If $hNumber2 >= 4 Then
                $hNumber2 = 0
                _GDIPlus_GraphicsDrawLine( $hGraphic, $hX + $hW - $hFreq-$hFixVar, $hY +1 , $hX + $hW - $hFreq-$hFixVar, $hY + $hH - 1, $hLines)
            EndIf
        ElseIf $hFreq < 4 Then
            $hDifferential = ( 4 - $hFreq ) * 2 + $hFreq
            If $hNumber2 >= $hDifferential Then
                $hNumber2 = 0
                _GDIPlus_GraphicsDrawLine( $hGraphic, $hX + $hW - $hFreq-$hFixVar, $hY +1 , $hX + $hW - $hFreq-$hFixVar, $hY + $hH - 1, $hLines)
            EndIf
        EndIf
        $hNumber2 += 1
        $hRedrawStart += $hFreq

        _WinAPI_DeleteObject( $hBitMap2 )
        _GDIPlus_GraphicsDispose( $hImage2 )
    EndIf
    _GDIPlus_GraphicsDrawRect( $hGraphic, $hX, $hY, $hW, $hH, $hBoarder)
    For $i = 1 to $Variables[0] Step 1
        If $Variables[$i] < 1 Then
            $Variables[$i] = 1
        ElseIf $Variables[$i] > $hH Then
            $Variables[$i] = $hH - 1
        EndIf
        _GDIPlus_PenSetColor( $hPen, $Colors[$i] )
        If ($hStep * $hFreq) + ($hFreq*2) >= $hW Then
            $hRedraw = True
            If $hFirst = True Then
                _GDIPlus_GraphicsDrawLine( $hGraphic, $hX + $hW-$hFreq-4, $hY + $hH - $LastVars[$i], $hX+$hW, $hY + $hH - $Variables[$i], $hPen)
            Else
                _GDIPlus_GraphicsDrawLine( $hGraphic, $hX + $hW-$hFreq-3, $hY + $hH - $LastVars[$i], $hX+$hW, $hY + $hH - $Variables[$i], $hPen)
            EndIf
            If $i = $Variables[0] Then
                $hFirst = False
            EndIf
            $LastVars[$i] = $Variables[$i]
        Else
            If $hStep <> -1 Then
                _GDIPlus_GraphicsDrawLine( $hGraphic, $hX + ($hStep * $hFreq), $hY + $hH - $LastVars[$i], $hX + ($hStep * $hFreq) + $hFreq+1, $hY + $hH - $Variables[$i], $hPen)
            EndIf
            $LastVars[$i] = $Variables[$i]
        EndIf
    Next
    $hStep += 1
EndFunc

Func _GUICtrlGraph_Grid($hFreq, $hX, $hY, $hW, $hH, $hStart, $hAxis )
    Local $LastNumber
    _GDIPlus_GraphicsDrawString ($hGraphic, $hStart, $hX - 2, $hY + $hH + 6, "Arial", 5)
    If $hAxis = "X" Then
        _GDIPlus_GraphicsDrawLine( $hGraphic, $hX, $hY + $hH, $hX , $hY + $hH + 4, $hBoarder)
        For $i = $hFreq to $hW Step $hFreq
            $hNumber2 += 1
            If $hW < $i + $hFreq Then
                $hFixVar = $hW - $i
            EndIf
            If $hFreq >= 4 Then
                If $hNumber2 < 4 Then
                    _GDIPlus_GraphicsDrawLine( $hGraphic, $hX + $i, $hY + $hH, $hX + $i , $hY + $hH + 2, $hBoarder)
                ElseIf $hNumber2 = 4 Then
                    If $i = $hW Then
                        _GDIPlus_GraphicsDrawLine( $hGraphic, $hX + $i, $hY + $hH, $hX + $i , $hY + $hH + 4, $hBoarder)
                        $hNumber2 = 4
                    Else
                        _GDIPlus_GraphicsDrawLine( $hGraphic, $hX + $i, $hY + $hH, $hX + $i , $hY + $hH + 4, $hBoarder)
                        _GDIPlus_GraphicsDrawLine( $hGraphic, $hX + $i, $hY +1 , $hX + $i , $hY + $hH - 1, $hLines)
                        $hNumber2 = 0
                    EndIf
                EndIf
            ElseIf $hFreq < 4 Then
                $hDifferential = ( 4 - $hFreq ) * 2 + $hFreq
                If $hNumber2 <> $hDifferential Then
                    _GDIPlus_GraphicsDrawLine( $hGraphic, $hX + $i, $hY + $hH, $hX + $i , $hY + $hH + 2, $hBoarder)
                Else
                    If $i = $hW Then
                        _GDIPlus_GraphicsDrawLine( $hGraphic, $hX + $i, $hY + $hH, $hX + $i , $hY + $hH + 4, $hBoarder)
                    Else
                        _GDIPlus_GraphicsDrawLine( $hGraphic, $hX + $i, $hY + $hH, $hX + $i , $hY + $hH + 4, $hBoarder)
                        _GDIPlus_GraphicsDrawLine( $hGraphic, $hX + $i, $hY +1 , $hX + $i , $hY + $hH - 1, $hLines)
                    EndIf
                    $hNumber2 = 0
                EndIf
            EndIf
        Next
    ElseIf $hAxis = "Y" Then
        _GDIPlus_GraphicsDrawLine( $hGraphic, $hX - 4 , $hY+ $hH, $hX , $hY + $hH, $hBoarder)
        $hNumber = 1
        For $i = $hFreq to $hH Step $hFreq
            If $hNumber = 1 or $hNumber = 2 or $hNumber = 3 Then
                _GDIPlus_GraphicsDrawLine( $hGraphic, $hX - 2 , $hY + $hH - $i, $hX , $hY + $hH - $i, $hBoarder)
            ElseIf $hNumber = 4 Then
                If $i = $hH Then
                    _GDIPlus_GraphicsDrawLine( $hGraphic, $hX - 4 , $hY+ $hH - $i, $hX , $hY + $hH - $i, $hBoarder)
                Else
                    _GDIPlus_GraphicsDrawLine( $hGraphic, $hX - 4 , $hY + $hH - $i, $hX , $hY + $hH - $i, $hBoarder)
                EndIf
                $hNumber = 0
            EndIf
            $hNumber += 1
        Next
    EndIf
EndFunc

Func _GUICtrlGraph_GridNumbers( $hStart, $hX, $hY, $hW, $hH, $hFreq, $hAxis)
    If $hAxis = "X" Then
        _GDIPlus_GraphicsFillRect( $hGraphic, $hX - 2, $hY + $hH + 5, $hW+14, 9, $hBackcolor)
        _GDIPlus_GraphicsDrawString ($hGraphic, $hStart, $hX - 2, $hY + $hH + 6, "Arial", 5)
        $hNumber = 1
        For $i = $hFreq to $hW Step $hFreq
            If $hFreq >= 4 Then
                If $hNumber = 4 Then
                    _GDIPlus_GraphicsDrawString ($hGraphic, $i + $hStart, $hX + $i - (StringLen($i)*2), $hY + $hH + 6, "Arial", 5)
                    $hNumber = 0
                EndIf
            Else
                $hDifferential = ( 4 - $hFreq ) * 2 + $hFreq
                If $hNumber = $hDifferential Then
                    If StringLen( $i + $hStart ) > 3 Then
                        _GDIPlus_GraphicsDrawString ($hGraphic, $i + $hStart, $hX + $i - (StringLen($i)*2), $hY + $hH + 6, "Arial", 5)
                        $hNumber = 0
                    Else
                        _GDIPlus_GraphicsDrawString ($hGraphic, $i + $hStart, $hX + $i - (StringLen($i)*2), $hY + $hH + 6, "Arial", 5)
                        $hNumber = 0
                    EndIf
                EndIf
            EndIf
            $hNumber += 1
        Next
    ElseIf $hAxis = "Y" Then
        _GDIPlus_GraphicsFillRect( $hGraphic, $hX - 15, $hY, 11, $hH, $hBackcolor)
        _GDIPlus_GraphicsDrawString ($hGraphic, $hStart, $hX - 11, $hY + $hH -3, "Arial", 5)
        $hNumber = 1
        For $i = $hFreq to $hH Step $hFreq
            If $hNumber = 4 Then
                _GDIPlus_GraphicsDrawString ($hGraphic, $i + $hStart, $hX - 7 - (StringLen($i)*4), $hY + $hH - $i - 3, "Arial", 5)
                $hNumber = 0
            EndIf
            $hNumber += 1
        Next
    EndIf
EndFunc

Func _GUICtrlGraph_GridLinesY($hFreq, $hX, $hY, $hW, $hH )
    $hNumber = 1
    For $i = $hFreq to $hH Step $hFreq
        If $hNumber = 4 Then
            _GDIPlus_GraphicsDrawLine( $hGraphic, $hX + 1 , $hY + $hH - $i , $hX + $hW + 1, $hY + $hH - $i, $hLines)
            $hNumber = 0
        EndIf
        $hNumber += 1
    Next
EndFunc

Func _GUICtrlGraph_SetTitle( $hGraph, $hTitle )
    _GDIPlus_GraphicsFillRect( $hGraphic, ($hX + ($hW/2)) - (StringLen($hTitle)*2) - 2, $hY - 12, (StringLen($hTitle)*6), 11, $hBackcolor)
    _GDIPlus_GraphicsDrawString ($hGraphic, $hTitle, ($hX + ($hW/2)) - (StringLen($hTitle)*2), $hY - 12, "Arial", 7)
EndFunc

Func _GUICtrlGraph_SetAxisTitleX( $hTitle )
    _GDIPlus_GraphicsDrawString ($hGraphic, $hTitle, ($hX + ($hW/2)) - (StringLen($hTitle)*2), $hY + $hH + 15, "Georgia", 7)
EndFunc

Func _GUICtrlGraph_SetAxisTitleY( $hTitle )
    _GDIPlus_GraphicsDrawString ($hGraphic, $hTitle, $hX - 24, $hY + ($hH/2) - (StringLen($hTitle)*2), "Georgia", 7, 0x0003)
EndFunc

Func _GUICtrlGraph_SetBkColor( $hColor )
    $hBackgroundColor = $hColor
EndFunc

Func _GUICtrlGraph_SetAxisGridColor( $hColor )
    $hAxisLinesColor = $hColor
EndFunc

Func _GUICtrlGraph_SetWindowColor( $hColor )
    $hWindowBackColor = $hColor
EndFunc

Func _GUICtrlGraph_SetBoarderColor( $hColor )
    $hBoarderColor = $hColor
EndFunc

Func _RandomizeVarriables()
    For $i = 1 to $Variables[0] Step 1
        $rand = Random()
        If $rand < 0.25 Then
            $Variables[$i] += 1
        ElseIf $rand > 0.25 and $rand < 0.5 Then
            $Variables[$i] += 2
        ElseIf $rand > 0.5 and $rand < 0.75 Then
            $Variables[$i] -= 1
        ElseIf $rand > 0.75 and $rand < 1 Then
            $Variables[$i] -= 2
        EndIf
    Next
EndFunc

Share this post


Link to post
Share on other sites
MvGulik

For some potential additional inspirations.

3D Bar Graph (with native AutoIt functions, no GDI)


"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites
asdf8

@asdf8 - problem #1 is that its GDI+ and i dont really know a way not to make it erase if dragged off the screen,

i could make it redraw the entire graph each time but it might make it flash

Problem #2 I think is your windows theme has a different Window to Client spacing and it was throwing it off when it was copying

Solution

...

1. You can see how it's done in

2. Ok

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×