Jump to content

GraphGDIPlus UDF - Create GDI+ line graphs


andybiochem
 Share

Recommended Posts

This is a really great UDF! Thank you very much!!!

I managed to get it to work in my situation. But there's something I can't figure out how to do: I need the Y axis to show dollar amounts (up to maybe $100,000). But the labels are too narrow, so they are cut off on the left end. And they only show unformatted values (like 21560, which is hard to read).

I would like to format the labels such as "$21,560". But when I format them, they are no longer real numbers, so it doesn't plot the values.

Is there any way to show formatted values?

Link to comment
Share on other sites

  • 5 months later...

Point of interest if using this very useful UDF to create multiple graphs in separate GUis simultaneously:

The $aGraphArray that this UDF relies upon to keep track of a graph's properties does not store the GUI where that graph is defined as a control. Consequently, if you decide to change the X/Y scale later on for any graph except the last-defined one, you'll need to GUIswitch() to the appropriate GUI before calling _GraphGDIPlus_Set_Range#(), otherwise the new tickmark labels will be associated with the wrong graph. So if you, like me, are dealing with multiple dynamically updated graphs, you'll need to put in place some admin handling for that (in your own code).

Just thought I'd mention it, as it might save you spending lots of time hunting a non-existent bug, as I just did. :

Edited by RTFC
Link to comment
Share on other sites

  • 9 months later...

I got an error after only including your UDF (did not yet use any functions of it)

Line 635: Const $SRCCOPY = 0x00CC0020

error: $SRCCOPY previously declared as a 'Const'.

 

ok found the problem;  $SRCCOPY is used in WindowsConstants.au3 which is used for extended GUI styles

Edited by QuimiQ
Link to comment
Share on other sites

  • 3 months later...

I got a problem when I try to use _GraphGDIPlus_Set_Range#() on the same graph multiple times(I only have one graph so what RTFC says above shouldn't be the problem ?) 

 

I have tried to clear it with _GraphGDIPlus_Clear() but this only removes the graph not the ticks.

 

Is it possible to do this without destroying the graph and then recreating it?

Link to comment
Share on other sites

  • 9 months later...

Just to keep this UDF up to date and working.

I had to replace $ghGDIPDll  with $__g_hGDIPDll since it got renamed.

Now it works fine. :)

 

;#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6

; #INDEX# ===============================================================================
; Title .........: GraphGDIPlus
; AutoIt Version: 3.3.0.0+
; Language: English
; Description ...: A Graph control to draw line graphs, using GDI+, also double-buffered.
; Notes .........:
; =======================================================================================



; #VARIABLES/INCLUDES# ==================================================================
#include-once
#include <GDIplus.au3>

Global $aGraphGDIPlusaGraphArrayINTERNAL[1]
; =======================================================================================



; #FUNCTION# ============================================================================
; Name...........: _GraphGDIPlus_Create
; Description ...: Creates graph area, and prepares array of specified data
; Syntax.........: _GraphGDIPlus_Create($hWnd,$iLeft,$iTop,$iWidth,$iHeight,$hColorBorder = 0xFF000000,$hColorFill = 0xFFFFFFFF)
; Parameters ....:  $hWnd - Handle to GUI
;                   $iLeft - left most position in GUI
; $iTop - top most position in GUI
; $iWidth - width of graph in pixels
; $iHeight - height of graph in pixels
;                $hColorBorder - Color of graph border (ARGB)
;                $hColorFill - Color of background (ARGB)
; Return values .: Returns array containing variables for subsequent functions...
; Returned Graph array is:
; [1] graphic control handle
; [2] left
; [3] top
; [4] width
; [5] height
; [6] x low
; [7] x high
; [8] y low
; [9] y high
; [10] x ticks handles
; [11] x labels handles
; [12] y ticks handles
; [13] y labels handles
;                    [14] Border Color
;                    [15] Fill Color
;                    [16] Bitmap Handle
;                    [17] Backbuffer Handle
;                    [18] Last used x pos
;                    [19] Last used y pos
;                    [20] Pen (main) Handle
;                    [21] Brush (fill) Handle
;                    [22] Pen (border) Handle
;                    [23] Pen (grid) Handle
; =======================================================================================
Func _GraphGDIPlus_Create($hWnd, $iLeft, $iTop, $iWidth, $iHeight, $hColorBorder = 0xFF000000, $hColorFill = 0xFFFFFFFF, $iSmooth = 2)
    Local $graphics, $bitmap, $backbuffer, $brush, $bpen, $gpen, $pen
    Local $ahTicksLabelsX[1]
    Local $ahTicksLabelsY[1]
    Local $ahTicksX[1]
    Local $ahTicksY[1]
    Local $aGraphArray[1]

    ;----- Set GUI transparency to SOLID (prevents GDI+ glitches) -----
    ;WinSetTrans($hWnd, "", 255) - causes problems when more than 2 graphs used
    ;----- GDI+ Initiate -----
    _GDIPlus_Startup()
    $graphics = _GDIPlus_GraphicsCreateFromHWND($hWnd) ;graphics area
    $bitmap = _GDIPlus_BitmapCreateFromGraphics($iWidth + 1, $iHeight + 1, $graphics);buffer bitmap
    $backbuffer = _GDIPlus_ImageGetGraphicsContext($bitmap) ;buffer area
    _GDIPlus_GraphicsSetSmoothingMode($backbuffer, $iSmooth)

    ;----- Set background Color -----
    $brush = _GDIPlus_BrushCreateSolid($hColorFill)
    _GDIPlus_GraphicsFillRect($backbuffer, 0, 0, $iWidth, $iHeight, $brush)
    ;----- Set border Pen + color -----
    $bpen = _GDIPlus_PenCreate($hColorBorder)
    _GDIPlus_PenSetEndCap($bpen, $GDIP_LINECAPROUND)
    ;----- Set Grid Pen + color -----
    $gpen = _GDIPlus_PenCreate(0xFFf0f0f0)
    _GDIPlus_PenSetEndCap($gpen, $GDIP_LINECAPROUND)
    ;----- set Drawing Pen + Color -----
    $pen = _GDIPlus_PenCreate() ;drawing pen initially black, user to set
    _GDIPlus_PenSetEndCap($pen, $GDIP_LINECAPROUND)
    _GDIPlus_GraphicsDrawRect($backbuffer, 0, 0, $iWidth, $iHeight, $pen)
    ;----- draw -----
    _GDIPlus_GraphicsDrawImageRect($graphics, $bitmap, $iLeft, $iTop, $iWidth + 1, $iHeight + 1)
    ;----- register redraw -----
    GUIRegisterMsg(0x0006, "_GraphGDIPlus_ReDraw") ;0x0006 = win activate
    GUIRegisterMsg(0x0003, "_GraphGDIPlus_ReDraw") ;0x0003 = win move
    ;----- prep + load array -----
    Dim $aGraphArray[24] = ["", $graphics, $iLeft, $iTop, $iWidth, $iHeight, 0, 1, 0, 1, _
            $ahTicksX, $ahTicksLabelsX, $ahTicksY, $ahTicksLabelsY, $hColorBorder, $hColorFill, _
            $bitmap, $backbuffer, 0, 0, $pen, $brush, $bpen, $gpen]
    ;----- prep re-draw array for all graphs created -----
    ReDim $aGraphGDIPlusaGraphArrayINTERNAL[UBound($aGraphGDIPlusaGraphArrayINTERNAL) + 1]
    $aGraphGDIPlusaGraphArrayINTERNAL[UBound($aGraphGDIPlusaGraphArrayINTERNAL) - 1] = $aGraphArray

    Return $aGraphArray
EndFunc ;==>_GraphGDIPlus_Create
Func _GraphGDIPlus_ReDraw($hWnd)
    ;----- Allows redraw of the GDI+ Image upon window min/maximize -----
    Local $i
    _WinAPI_RedrawWindow($hWnd, 0, 0, 0x0100)
    For $i = 1 To UBound($aGraphGDIPlusaGraphArrayINTERNAL) - 1
        If $aGraphGDIPlusaGraphArrayINTERNAL[$i] = 0 Then ContinueLoop
        _GraphGDIPlus_Refresh($aGraphGDIPlusaGraphArrayINTERNAL[$i])
    Next
EndFunc ;==>_GraphGDIPlus_ReDraw



; #FUNCTION# ============================================================================
; Name...........: _GraphGDIPlus_Delete
; Description ...: Deletes previously created graph and related ticks/labels
; Syntax.........: _GraphGDIPlus_Delete($hWnd,ByRef $aGraphArray)
; Parameters ....:  $hWnd - GUI handle
;                   $aGraphArray - the array returned from _GraphGDIPlus_Create
;                   $iKeepGDIPlus - if not zero, function will not _GDIPlus_Shutdown()
; =======================================================================================
Func _GraphGDIPlus_Delete($hWnd, ByRef $aGraphArray, $iKeepGDIPlus = 0)
    If IsArray($aGraphArray) = 0 Then Return
    Local $ahTicksX, $ahTicksLabelsX, $ahTicksY, $ahTicksLabelsY, $i, $aTemp
    ;----- delete x ticks/labels -----
    $ahTicksX = $aGraphArray[10]
    $ahTicksLabelsX = $aGraphArray[11]
    For $i = 1 To (UBound($ahTicksX) - 1)
        GUICtrlDelete($ahTicksX[$i])
    Next
    For $i = 1 To (UBound($ahTicksLabelsX) - 1)
        GUICtrlDelete($ahTicksLabelsX[$i])
    Next
    ;----- delete y ticks/labels -----
    $ahTicksY = $aGraphArray[12]
    $ahTicksLabelsY = $aGraphArray[13]
    For $i = 1 To (UBound($ahTicksY) - 1)
        GUICtrlDelete($ahTicksY[$i])
    Next
    For $i = 1 To (UBound($ahTicksLabelsY) - 1)
        GUICtrlDelete($ahTicksLabelsY[$i])
    Next
    ;----- delete graphic control -----
    _GDIPlus_GraphicsDispose($aGraphArray[17])
    _GDIPlus_BitmapDispose($aGraphArray[16])
    _GDIPlus_GraphicsDispose($aGraphArray[1])
    _GDIPlus_BrushDispose($aGraphArray[21])
    _GDIPlus_PenDispose($aGraphArray[20])
    _GDIPlus_PenDispose($aGraphArray[22])
    _GDIPlus_PenDispose($aGraphArray[23])
    If $iKeepGDIPlus = 0 Then _GDIPlus_Shutdown()
    _WinAPI_InvalidateRect($hWnd)
    ;----- remove form global redraw array -----
    For $i = 1 To UBound($aGraphGDIPlusaGraphArrayINTERNAL) - 1
        $aTemp = $aGraphGDIPlusaGraphArrayINTERNAL[$i]
        If IsArray($aTemp) = 0 Then ContinueLoop
        If $aTemp[1] = $aGraphArray[1] Then $aGraphGDIPlusaGraphArrayINTERNAL[$i] = 0
    Next
    ;----- close array -----
    $aGraphArray = 0
EndFunc ;==>_GraphGDIPlus_Delete



; #FUNCTION# ============================================================================
; Name...........: _GraphGDIPlus_Clear
; Description ...: Clears graph content
; Syntax.........: _GraphGDIPlus_Clear(ByRef $aGraphArray)
; Parameters ....: $aGraphArray - the array returned from _GraphGDIPlus_Create
; =======================================================================================
Func _GraphGDIPlus_Clear(ByRef $aGraphArray)
    If IsArray($aGraphArray) = 0 Then Return
    ;----- Set background Color -----
    _GDIPlus_GraphicsFillRect($aGraphArray[17], 0, 0, $aGraphArray[4], $aGraphArray[5], $aGraphArray[21])
    ;----- set border + Color -----
    _GraphGDIPlus_RedrawRect($aGraphArray)
EndFunc ;==>_GraphGDIPlus_Clear



; #FUNCTION# =============================================================================
; Name...........: _GraphGDIPlus_Refresh
; Description ...: refreshes the graphic
; Syntax.........: _GraphGDIPlus_Refresh(ByRef $aGraphArray)
; Parameters ....: $aGraphArray - the array returned from _GraphGDIPlus_Create
; ========================================================================================
Func _GraphGDIPlus_Refresh(ByRef $aGraphArray)
    If IsArray($aGraphArray) = 0 Then Return
    ;----- draw -----
    _GDIPlus_GraphicsDrawImageRect($aGraphArray[1], $aGraphArray[16], $aGraphArray[2], _
            $aGraphArray[3], $aGraphArray[4] + 1, $aGraphArray[5] + 1)
EndFunc ;==>_GraphGDIPlus_Refresh



; #FUNCTION# ============================================================================
; Name...........: _GraphGDIPlus_Set_RangeX
; Description ...: Allows user to set the range of the X axis and set ticks and rounding levels
; Syntax.........: _GraphGDIPlus_Set_RangeX(ByRef $aGraphArray,$iLow,$iHigh,$iXTicks = 1,$bLabels = 1,$iRound = 0)
; Parameters ....: $aGraphArray - the array returned from _GraphGDIPlus_Create
; $iLow - the lowest value for the X axis (can be negative)
; $iHigh - the highest value for the X axis
; $iXTicks - [optional] number of ticks to show below axis, if = 0 then no ticks created
; $bLabels - [optional] 1=show labels, any other number=do not show labels
; $iRound - [optional] rounding level of label values
; =======================================================================================
Func _GraphGDIPlus_Set_RangeX(ByRef $aGraphArray, $iLow, $iHigh, $iXTicks = 1, $bLabels = 1, $iRound = 0)
    If IsArray($aGraphArray) = 0 Then Return
    Local $ahTicksX, $ahTicksLabelsX, $i
    ;----- load user vars to array -----
    $aGraphArray[6] = $iLow
    $aGraphArray[7] = $iHigh
    ;----- prepare nested array -----
    $ahTicksX = $aGraphArray[10]
    $ahTicksLabelsX = $aGraphArray[11]
    ;----- delete any existing ticks -----
    For $i = 1 To (UBound($ahTicksX) - 1)
        GUICtrlDelete($ahTicksX[$i])
    Next
    Dim $ahTicksX[1]
    ;----- create new ticks -----
    For $i = 1 To $iXTicks + 1
        ReDim $ahTicksX[$i + 1]
        $ahTicksX[$i] = GUICtrlCreateLabel("", (($i - 1) * ($aGraphArray[4] / $iXTicks)) + $aGraphArray[2], _
                $aGraphArray[3] + $aGraphArray[5], 1, 5)
        GUICtrlSetBkColor(-1, 0x000000)
        GUICtrlSetState(-1, 128)
    Next
    ;----- delete any existing labels -----
    For $i = 1 To (UBound($ahTicksLabelsX) - 1)
        GUICtrlDelete($ahTicksLabelsX[$i])
    Next
    Dim $ahTicksLabelsX[1]
    ;----- create new labels -----
    For $i = 1 To $iXTicks + 1
        ReDim $ahTicksLabelsX[$i + 1]
        $ahTicksLabelsX[$i] = GUICtrlCreateLabel("", _
                ($aGraphArray[2] + (($aGraphArray[4] / $iXTicks) * ($i - 1))) - (($aGraphArray[4] / $iXTicks) / 2), _
                $aGraphArray[3] + $aGraphArray[5] + 10, $aGraphArray[4] / $iXTicks, 13, 1)
        GUICtrlSetBkColor(-1, -2)
    Next
    ;----- if labels are required, then fill -----
    If $bLabels = 1 Then
        For $i = 1 To (UBound($ahTicksLabelsX) - 1)
            GUICtrlSetData($ahTicksLabelsX[$i], _
                    StringFormat("%." & $iRound & "f", _GraphGDIPlus_Reference_Pixel("p", (($i - 1) * ($aGraphArray[4] / $iXTicks)), _
                    $aGraphArray[6], $aGraphArray[7], $aGraphArray[4])))
        Next
    EndIf
    ;----- load created arrays back into array -----
    $aGraphArray[10] = $ahTicksX
    $aGraphArray[11] = $ahTicksLabelsX
EndFunc ;==>_GraphGDIPlus_Set_RangeX



; #FUNCTION# ============================================================================
; Name...........: _GraphGDIPlus_Set_RangeY
; Description ...: Allows user to set the range of the Y axis and set ticks and rounding levels
; Syntax.........: _GraphGDIPlus_SetRange_Y(ByRef $aGraphArray,$iLow,$iHigh,$iYTicks = 1,$bLabels = 1,$iRound = 0)
; Parameters ....: $aGraphArray - the array returned from _GraphGDIPlus_Create
; $iLow - the lowest value for the Y axis (can be negative)
; $iHigh - the highest value for the Y axis
; $iYTicks - [optional] number of ticks to show next to axis, if = 0 then no ticks created
; $bLabels - [optional] 1=show labels, any other number=do not show labels
; $iRound - [optional] rounding level of label values
; =======================================================================================
Func _GraphGDIPlus_Set_RangeY(ByRef $aGraphArray, $iLow, $iHigh, $iYTicks = 1, $bLabels = 1, $iRound = 0)
    If IsArray($aGraphArray) = 0 Then Return
    Local $ahTicksY, $ahTicksLabelsY, $i
    ;----- load user vars to array -----
    $aGraphArray[8] = $iLow
    $aGraphArray[9] = $iHigh
    ;----- prepare nested array -----
    $ahTicksY = $aGraphArray[12]
    $ahTicksLabelsY = $aGraphArray[13]
    ;----- delete any existing ticks -----
    For $i = 1 To (UBound($ahTicksY) - 1)
        GUICtrlDelete($ahTicksY[$i])
    Next
    Dim $ahTicksY[1]
    ;----- create new ticks -----
    For $i = 1 To $iYTicks + 1
        ReDim $ahTicksY[$i + 1]
        $ahTicksY[$i] = GUICtrlCreateLabel("", $aGraphArray[2] - 5, _
                ($aGraphArray[3] + $aGraphArray[5]) - (($aGraphArray[5] / $iYTicks) * ($i - 1)), 5, 1)
        GUICtrlSetBkColor(-1, 0x000000)
        GUICtrlSetState(-1, 128)
    Next
    ;----- delete any existing labels -----
    For $i = 1 To (UBound($ahTicksLabelsY) - 1)
        GUICtrlDelete($ahTicksLabelsY[$i])
    Next
    Dim $ahTicksLabelsY[1]
    ;----- create new labels -----
    For $i = 1 To $iYTicks + 1
        ReDim $ahTicksLabelsY[$i + 1]
        $ahTicksLabelsY[$i] = GUICtrlCreateLabel("", $aGraphArray[2] - 40, _
                ($aGraphArray[3] + $aGraphArray[5]) - (($aGraphArray[5] / $iYTicks) * ($i - 1)) - 6, 30, 13, 2)
        GUICtrlSetBkColor(-1, -2)
    Next
    ;----- if labels are required, then fill -----
    If $bLabels = 1 Then
        For $i = 1 To (UBound($ahTicksLabelsY) - 1)
            GUICtrlSetData($ahTicksLabelsY[$i], StringFormat("%." & $iRound & "f", _GraphGDIPlus_Reference_Pixel("p", _
                    (($i - 1) * ($aGraphArray[5] / $iYTicks)), $aGraphArray[8], $aGraphArray[9], $aGraphArray[5])))
        Next
    EndIf
    ;----- load created arrays back into array -----
    $aGraphArray[12] = $ahTicksY
    $aGraphArray[13] = $ahTicksLabelsY
EndFunc ;==>_GraphGDIPlus_Set_RangeY



; #FUNCTION# =============================================================================
; Name...........: _GraphGDIPlus_Plot_Start
; Description ...: Move starting point of plot
; Syntax.........: _GraphGDIPlus_Plot_Start(ByRef $aGraphArray,$iX,$iY)
; Parameters ....: $aGraphArray - the array returned from _GraphGDIPlus_Create
; $iX - x value to start at
; $iY - y value to start at
; ========================================================================================
Func _GraphGDIPlus_Plot_Start(ByRef $aGraphArray, $iX, $iY)
    If IsArray($aGraphArray) = 0 Then Return
    ;----- MOVE pen to start point -----
    $aGraphArray[18] = _GraphGDIPlus_Reference_Pixel("x", $iX, $aGraphArray[6], $aGraphArray[7], $aGraphArray[4])
    $aGraphArray[19] = _GraphGDIPlus_Reference_Pixel("y", $iY, $aGraphArray[8], $aGraphArray[9], $aGraphArray[5])
EndFunc ;==>_GraphGDIPlus_Plot_Start



; #FUNCTION# =============================================================================
; Name...........: _GraphGDIPlus_Plot_Line
; Description ...: draws straight line to x,y from previous point / starting point
; Syntax.........: _GraphGDIPlus_Plot_Line(ByRef $aGraphArray,$iX,$iY)
; Parameters ....: $aGraphArray - the array returned from _GraphGDIPlus_Create
; $iX - x value to draw to
; $iY - y value to draw to
; ========================================================================================
Func _GraphGDIPlus_Plot_Line(ByRef $aGraphArray, $iX, $iY)
    If IsArray($aGraphArray) = 0 Then Return
    ;----- Draw line from previous point to new point -----
    $iX = _GraphGDIPlus_Reference_Pixel("x", $iX, $aGraphArray[6], $aGraphArray[7], $aGraphArray[4])
    $iY = _GraphGDIPlus_Reference_Pixel("y", $iY, $aGraphArray[8], $aGraphArray[9], $aGraphArray[5])
;~  _GDIPlus_GraphicsDrawLine($aGraphArray[17], $aGraphArray[18], $aGraphArray[19], $iX, $iY, $aGraphArray[20])
    DllCall($__g_hGDIPDll, "int", "GdipDrawLine", "handle", $aGraphArray[17], "handle", $aGraphArray[20], "float", $aGraphArray[18], "float", $aGraphArray[19], "float", $iX, "float", $iY)
    _GraphGDIPlus_RedrawRect($aGraphArray)
    ;----- save current as last coords -----
    $aGraphArray[18] = $iX
    $aGraphArray[19] = $iY
EndFunc ;==>_GraphGDIPlus_Plot_Line



; #FUNCTION# =============================================================================
; Name...........: _GraphGDIPlus_Plot_Point
; Description ...: draws point at coords
; Syntax.........: _GraphGDIPlus_Plot_Point(ByRef $aGraphArray,$iX,$iY)
; Parameters ....: $aGraphArray - the array returned from _GraphGDIPlus_Create
; $iX - x value to draw at
; $iY - y value to draw at
; ========================================================================================
Func _GraphGDIPlus_Plot_Point(ByRef $aGraphArray, $iX, $iY)
    If IsArray($aGraphArray) = 0 Then Return
    ;----- Draw point from previous point to new point -----
    $iX = _GraphGDIPlus_Reference_Pixel("x", $iX, $aGraphArray[6], $aGraphArray[7], $aGraphArray[4])
    $iY = _GraphGDIPlus_Reference_Pixel("y", $iY, $aGraphArray[8], $aGraphArray[9], $aGraphArray[5])
;~  _GDIPlus_GraphicsDrawRect($aGraphArray[17], $iX-1, $iY-1, 2, 2, $aGraphArray[20])
    DllCall($__g_hGDIPDll, "int", "GdipDrawRectangle", "handle", $aGraphArray[17], "handle", $aGraphArray[20], "float", $iX-1, "float", $iY-1,"float", 2, "float", 2)
    _GraphGDIPlus_RedrawRect($aGraphArray)
    ;----- save current as last coords -----
    $aGraphArray[18] = $iX
    $aGraphArray[19] = $iY
EndFunc ;==>_GraphGDIPlus_Plot_Point



; #FUNCTION# =============================================================================
; Name...........: _GraphGDIPlus_Plot_Dot
; Description ...: draws single pixel dot at coords
; Syntax.........: _GraphGDIPlus_Plot_Dot(ByRef $aGraphArray,$iX,$iY)
; Parameters ....: $aGraphArray - the array returned from _GraphGDIPlus_Create
; $iX - x value to draw at
; $iY - y value to draw at
; ========================================================================================
Func _GraphGDIPlus_Plot_Dot(ByRef $aGraphArray, $iX, $iY)
    If IsArray($aGraphArray) = 0 Then Return
    ;----- Draw point from previous point to new point -----
    $iX = _GraphGDIPlus_Reference_Pixel("x", $iX, $aGraphArray[6], $aGraphArray[7], $aGraphArray[4])
    $iY = _GraphGDIPlus_Reference_Pixel("y", $iY, $aGraphArray[8], $aGraphArray[9], $aGraphArray[5])
;~  _GDIPlus_GraphicsDrawRect($aGraphArray[17], $iX, $iY, 1, 1, $aGraphArray[20]) ;draws 2x2 dot ?HOW to get 1x1 pixel?????
    DllCall($__g_hGDIPDll, "int", "GdipDrawRectangle", "handle", $aGraphArray[17], "handle", $aGraphArray[20], "float", $iX, "float", $iY,"float", 1, "float", 1)
    _GraphGDIPlus_RedrawRect($aGraphArray)
    ;----- save current as last coords -----
    $aGraphArray[18] = $iX
    $aGraphArray[19] = $iY
EndFunc ;==>_GraphGDIPlus_Plot_Dot



; #FUNCTION# =============================================================================
; Name...........: _GraphGDIPlus_Set_PenColor
; Description ...: sets the Color for the next drawing
; Syntax.........: _GraphGDIPlus_Set_PenColor(ByRef $aGraphArray,$hColor,$hBkGrdColor = $GUI_GR_NOBKColor)
; Parameters ....: $aGraphArray - the array returned from _GraphGDIPlus_Create
; $hColor - the Color of the next item (ARGB)
; ========================================================================================
Func _GraphGDIPlus_Set_PenColor(ByRef $aGraphArray, $hColor)
    If IsArray($aGraphArray) = 0 Then Return
    ;----- apply pen Color -----
    _GDIPlus_PenSetColor($aGraphArray[20], $hColor)
EndFunc ;==>_GraphGDIPlus_Set_PenColor



; #FUNCTION# =============================================================================
; Name...........: _GraphGDIPlus_Set_PenSize
; Description ...: sets the pen for the next drawing
; Syntax.........: _GraphGDIPlus_Set_PenSize(ByRef $aGraphArray,$iSize = 1)
; Parameters ....: $aGraphArray - the array returned from _GraphGDIPlus_Create
; $iSize - size of pen line
; ========================================================================================
Func _GraphGDIPlus_Set_PenSize(ByRef $aGraphArray, $iSize = 1)
    If IsArray($aGraphArray) = 0 Then Return
    ;----- apply pen size -----
    _GDIPlus_PenSetWidth($aGraphArray[20], $iSize)
EndFunc ;==>_GraphGDIPlus_Set_PenSize



; #FUNCTION# =============================================================================
; Name...........: _GraphGDIPlus_Set_PenDash
; Description ...: sets the pen dash style for the next drawing
; Syntax.........: GraphGDIPlus_Set_PenDash(ByRef $aGraphArray,$iDash = 0)
; Parameters ....: $aGraphArray - the array returned from _GraphGDIPlus_Create
; $iDash - style of dash, where:
;                                       0 = solid line
;                                       1 = simple dashed line
;                                       2 = simple dotted line
;                                       3 = dash dot line
;                                       4 = dash dot dot line
; ========================================================================================
Func _GraphGDIPlus_Set_PenDash(ByRef $aGraphArray, $iDash = 0)
    If IsArray($aGraphArray) = 0 Then Return
    Local $Style
    Switch $iDash
        Case 0 ;solid line _____
            $Style = $GDIP_DASHSTYLESOLID
        Case 1 ;simple dash -----
            $Style = $GDIP_DASHSTYLEDASH
        Case 2 ;simple dotted .....
            $Style = $GDIP_DASHSTYLEDOT
        Case 3 ;dash dot -.-.-
            $Style = $GDIP_DASHSTYLEDASHDOT
        Case 4 ;dash dot dot -..-..-..
            $Style = $GDIP_DASHSTYLEDASHDOTDOT
    EndSwitch
    ;----- apply pen dash -----
    _GDIPlus_PenSetDashStyle($aGraphArray[20], $Style)
EndFunc ;==>_GraphGDIPlus_Set_PenDash



; #FUNCTION# =============================================================================
; Name...........: _GraphGDIPlus_Set_GridX
; Description ...: Adds X gridlines.
; Syntax.........: _GraphGDIPlus_Set_GridX(ByRef $aGraphArray, $Ticks=1, $hColor=0xf0f0f0)
; Parameters ....:  $aGraphArray - the array returned from _GraphGDIPlus_Create
;                   $Ticks - sets line at every nth unit assigned to axis
;               $hColor - [optional] RGB value, defining Color of grid. Default is a light gray
;                   $hColorY0 - [optional] RGB value, defining Color of Y=0 line, Default black
; =======================================================================================
Func _GraphGDIPlus_Set_GridX(ByRef $aGraphArray, $Ticks = 1, $hColor = 0xFFf0f0f0, $hColorY0 = 0xFF000000)
    If IsArray($aGraphArray) = 0 Then Return
    ;----- Set gpen to user color -----
    _GDIPlus_PenSetColor($aGraphArray[23], $hColor)
    ;----- draw grid lines -----
    Select
        Case $Ticks > 0
            For $i = $aGraphArray[6] To $aGraphArray[7] Step $Ticks
                If $i = Number($aGraphArray[6]) Or $i = Number($aGraphArray[7]) Then ContinueLoop
                _GDIPlus_GraphicsDrawLine($aGraphArray[17], _
                        _GraphGDIPlus_Reference_Pixel("x", $i, $aGraphArray[6], $aGraphArray[7], $aGraphArray[4]), _
                        1, _
                        _GraphGDIPlus_Reference_Pixel("x", $i, $aGraphArray[6], $aGraphArray[7], $aGraphArray[4]), _
                        $aGraphArray[5] - 1, _
                        $aGraphArray[23])
            Next
    EndSelect
    ;----- draw y=0 -----
    _GDIPlus_PenSetColor($aGraphArray[23], $hColorY0)
    _GDIPlus_GraphicsDrawLine($aGraphArray[17], _
            _GraphGDIPlus_Reference_Pixel("x", 0, $aGraphArray[6], $aGraphArray[7], $aGraphArray[4]), _
            1, _
            _GraphGDIPlus_Reference_Pixel("x", 0, $aGraphArray[6], $aGraphArray[7], $aGraphArray[4]), _
            $aGraphArray[5] - 1, _
            $aGraphArray[23])
    _GDIPlus_GraphicsDrawLine($aGraphArray[17], _
            1, _
            _GraphGDIPlus_Reference_Pixel("y", 0, $aGraphArray[8], $aGraphArray[9], $aGraphArray[5]), _
            $aGraphArray[4] - 1, _
            _GraphGDIPlus_Reference_Pixel("y", 0, $aGraphArray[8], $aGraphArray[9], $aGraphArray[5]), _
            $aGraphArray[23])

    _GraphGDIPlus_RedrawRect($aGraphArray)
    ;----- re-set to user specs -----
    _GDIPlus_PenSetColor($aGraphArray[23], $hColor) ;set Color back to user def
EndFunc ;==>_GraphGDIPlus_Set_GridX



; #FUNCTION# =============================================================================
; Name...........: _GraphGDIPlus_Set_GridY
; Description ...: Adds Y gridlines.
; Syntax.........: _GraphGDIPlus_Set_GridY(ByRef $aGraphArray, $Ticks=1, $hColor=0xf0f0f0)
; Parameters ....:  $aGraphArray - the array returned from _GraphGDIPlus_Create
;                   $Ticks - sets line at every nth unit assigned to axis
;               $hColor - [optional] RGB value, defining Color of grid. Default is a light gray
;                   $hColorX0 - [optional] RGB value, defining Color of X=0 line, Default black
; =======================================================================================
Func _GraphGDIPlus_Set_GridY(ByRef $aGraphArray, $Ticks = 1, $hColor = 0xFFf0f0f0, $hColorX0 = 0xFF000000)
    If IsArray($aGraphArray) = 0 Then Return
    ;----- Set gpen to user color -----
    _GDIPlus_PenSetColor($aGraphArray[23], $hColor)
    ;----- draw grid lines -----
    Select
        Case $Ticks > 0
            For $i = $aGraphArray[8] To $aGraphArray[9] Step $Ticks
                If $i = Number($aGraphArray[8]) Or $i = Number($aGraphArray[9]) Then ContinueLoop
                _GDIPlus_GraphicsDrawLine($aGraphArray[17], _
                        1, _
                        _GraphGDIPlus_Reference_Pixel("y", $i, $aGraphArray[8], $aGraphArray[9], $aGraphArray[5]), _
                        $aGraphArray[4] - 1, _
                        _GraphGDIPlus_Reference_Pixel("y", $i, $aGraphArray[8], $aGraphArray[9], $aGraphArray[5]), _
                        $aGraphArray[23])
            Next
    EndSelect
    ;----- draw abcissa/ordinate -----
    _GDIPlus_PenSetColor($aGraphArray[23], $hColorX0)
    _GDIPlus_GraphicsDrawLine($aGraphArray[17], _
            _GraphGDIPlus_Reference_Pixel("x", 0, $aGraphArray[6], $aGraphArray[7], $aGraphArray[4]), _
            1, _
            _GraphGDIPlus_Reference_Pixel("x", 0, $aGraphArray[6], $aGraphArray[7], $aGraphArray[4]), _
            $aGraphArray[5] - 1, _
            $aGraphArray[23])
    _GDIPlus_GraphicsDrawLine($aGraphArray[17], _
            1, _
            _GraphGDIPlus_Reference_Pixel("y", 0, $aGraphArray[8], $aGraphArray[9], $aGraphArray[5]), _
            $aGraphArray[4] - 1, _
            _GraphGDIPlus_Reference_Pixel("y", 0, $aGraphArray[8], $aGraphArray[9], $aGraphArray[5]), _
            $aGraphArray[23])

    _GraphGDIPlus_RedrawRect($aGraphArray)
    ;----- re-set to user specs -----
    _GDIPlus_PenSetColor($aGraphArray[23], $hColor) ;set Color back to user def
EndFunc ;==>_GraphGDIPlus_Set_GridY



; #FUNCTION# =============================================================================
; Name...........: _GraphGDIPlus_RedrawRect
; Description ...: INTERNAL FUNCTION - Re-draws the border
; Syntax.........: _GraphGDIPlus_RedrawRect(ByRef $aGraphArray)
; Parameters ....: $aGraphArray - the array returned from _GraphGDIPlus_Create
; Notes..........: This prevents drawing over the border of the graph area
; =========================================================================================
Func _GraphGDIPlus_RedrawRect(ByRef $aGraphArray)
    If IsArray($aGraphArray) = 0 Then Return
    ;----- draw border -----
    _GDIPlus_GraphicsDrawRect($aGraphArray[17], 0, 0, $aGraphArray[4], $aGraphArray[5], $aGraphArray[22]) ;draw border
EndFunc ;==>_GraphGDIPlus_RedrawRect


; #FUNCTION# =============================================================================
; Name...........: _GraphGDIPlus_Reference_Pixel
; Description ...: INTERNAL FUNCTION - performs pixel reference calculations
; Syntax.........: _GraphGDIPlus_Reference_Pixel($iType,$iValue,$iLow,$iHigh,$iTotalPixels)
; Parameters ....: $iType - "x"=x axis pix, "y" = y axis pix, "p"=value from pixels
; $iValue - pixels reference or value
; $iLow - lower limit of axis
; $iHigh - upper limit of axis
; $iTotalPixels - total number of pixels in range (either width or height)
; =========================================================================================
Func _GraphGDIPlus_Reference_Pixel($iType, $iValue, $iLow, $iHigh, $iTotalPixels)
    ;----- perform pixel reference calculations -----
    Switch $iType
        Case "x"
            Return (($iTotalPixels / ($iHigh - $iLow)) * (($iHigh - $iLow) * (($iValue - $iLow) / ($iHigh - $iLow))))
        Case "y"
            Return ($iTotalPixels - (($iTotalPixels / ($iHigh - $iLow)) * (($iHigh - $iLow) * (($iValue - $iLow) / ($iHigh - $iLow)))))
        Case "p"
            Return ($iValue / ($iTotalPixels / ($iHigh - $iLow))) + $iLow
    EndSwitch
EndFunc ;==>_GraphGDIPlus_Reference_Pixel

; #FUNCTION# =============================================================================
; Name...........: _GraphGDIPlus_SaveImage
; Description ...: INTERNAL FUNCTION - save drawn image to file
; Syntax.........: _GraphGDIPlus_SaveImage($aGraphArray, $file)
; Parameters ....: ByRef $aGraphArray - the array returned from _GraphGDIPlus_Create
;                               $file - filename
; Autor .........: UEZ
; =========================================================================================
Func _GraphGDIPlus_SaveImage(ByRef $aGraphArray, $file)
    If IsArray($aGraphArray) = 0 Then Return
    _GDIPlus_ImageSaveToFile($aGraphArray[16], $file)
    If @error Then Return SetError(1, 0, 0)
    Return 1
EndFunc ;==>_GraphGDIPlus_SaveImage

Func _GraphGDIPlus_DrawText(ByRef $aGraphArray, $sString, $iX, $iY, $iBrushColor = 0xFF000000, $sFont = "Arial", $iFontSize = 12, $iStyle = 0)
    If IsArray($aGraphArray) = 0 Then Return
    Local $hBrush = _GDIPlus_BrushCreateSolid($iBrushColor)
    Local $hFormat = _GDIPlus_StringFormatCreate()
Local $hFamily = _GDIPlus_FontFamilyCreate($sFont)
Local $hFont = _GDIPlus_FontCreate($hFamily, $iFontSize, $iStyle)
    Local $tLayout = _GDIPlus_RectFCreate($iX, $iY, 0, 0)
    _GDIPlus_GraphicsDrawStringEx($aGraphArray[17], $sString, $hFont, $tLayout, $hFormat, $hBrush)
_GDIPlus_FontDispose($hFont)
_GDIPlus_FontFamilyDispose($hFamily)
_GDIPlus_StringFormatDispose($hFormat)
_GDIPlus_BrushDispose($hBrush)
    Return 1
EndFunc ;==>_GraphGDIPlus_DrawText

 

Link to comment
Share on other sites

  • 1 month later...
  • 1 year later...
  • 7 months later...
On 2/20/2010 at 9:49 AM, andybiochem said:

 

I'm having an issue where I took your example and increased the Y-axis to 30,000 and now the number gets cut off while drawing so that you can't see the full number (looks like 0000.0) 

When I try to increase the _GraphGDIPlus_Create size it just expands how much space is taken up along the X axis. When trying to increase Padding to move text over more, the right portion of my graph starts moving off screen from window, but left portion with the axis numbering never becomes more visible. I hope that makes sense.... I'm kind of new at this stuff, don't know proper language to use. Easier to see visually.

Seems like there is no way to make larger axis numbers fit into the window space.

 

#include "GraphGDIPlus.au3"
#include <Array.au3>
#include <MsgBoxConstants.au3>

Opt("GUIOnEventMode", 1)

; Create 2D array to display
    Global $aArray_2D[1000][2]
    For $i = 0 To UBound($aArray_2D) - 1
       $aArray_2D[$i][0] = $i
        For $j = 0 To UBound($aArray_2D, 2) - 1
            $aArray_2D[$i][1] = $i^4
        Next

    Next




$aArray_2D[5][1] = 10

    _ArrayDisplay($aArray_2D)

$GUI = GUICreate("",600,600)
GUISetOnEvent(-3,"_Exit")
GUISetState()

;----- Create Graph area -----
$Graph = _GraphGDIPlus_Create($GUI,70,30,510,520,0xFF000000,0xFF88B3DD)


Global $iXRange=50,$iYRange=30000

;----- Set X axis range -----
_GraphGDIPlus_Set_RangeX($Graph,0,$iXRange,10,1,1)
_GraphGDIPlus_Set_RangeY($Graph,0,$iYRange,10,1,1)

;----- Set Y axis range -----
_GraphGDIPlus_Set_GridX($Graph,1,0xFF6993BE)
_GraphGDIPlus_Set_GridY($Graph,1,0xFF6993BE)


_GraphGDIPlus_Set_PenSize($Graph,1)
_GraphGDIPlus_Plot_Start($Graph,-$iXRange,-$iYRange)

_Draw_Graph()

While 1
    Sleep(100)
WEnd


Func _Draw_Graph()
    ;----- Set line color and size -----
    _GraphGDIPlus_Set_PenColor($Graph,0xFF0000FF)
    _GraphGDIPlus_Set_PenSize($Graph,1)

    ;----- draw lines -----
    $First = True
    For $i = 0 to UBound($aArray_2D)-1 Step 1
        If $First = True Then _GraphGDIPlus_Plot_Start($Graph,$aArray_2D[$i][0],$aArray_2D[$i][1])
        $First = False
        _GraphGDIPlus_Plot_Line($Graph,$aArray_2D[$i][0],$aArray_2D[$i][1])
        _GraphGDIPlus_Refresh($Graph)
    Next
EndFunc


Func _Exit()
    ;----- close down GDI+ and clear graphic -----
    _GraphGDIPlus_Delete($GUI,$Graph)
    Exit
EndFunc

 

Edited by AnonymousX
Link to comment
Share on other sites

  • 8 months later...
On 12/8/2013 at 3:51 AM, AZJIO said:

Examples, 14 pieces, 2.75 KB, Ru

GraphGDIPlus_Example.7z

Genius, can you please comment in english, all i get is this;

; Óñòàíàâëèâàåò ñåòêó ïî îñÿì XY
_GraphGDIPlus_Set_GridX($aGraph, 1, 0xFF6C6342)
_GraphGDIPlus_Set_GridY($aGraph, $iMax / 15, 0xFF6C6342)
; Ðèñóåò ãðàôèê
_GraphGDIPlus_Set_PenColor($aGraph, 0xFF00FFFF) ; Çàäà¸ò öâåò ëèíèè ãðàôèêà
For $i = 0 To 30
    _GraphGDIPlus_Plot_Point($aGraph, $i, $i / 3 + 1.5) ; Çàäà¸ò ñëåäóþùóþ òî÷êó
Next

 

Englisch would be very welcome here.

Edited by notsure
Link to comment
Share on other sites

  • 2 years later...

Hello,

i tried the example in the first post, and of course i updated the udf lilke mentioned by @Blaxxun here

and when i close the window i 'm getting this error (i'm using autoit 3.3.14.5 and the function exists) 

image.png.55aeca13e6e3b4a5b4c643d38f85b852.png

Edited by cetipabo
Link to comment
Share on other sites

Well, i have another problem now, with the grids.
in my example below, i'm supposed to have a light grey background with a blue grid, but because of the high amount in Rangex (4000) the backgroung is filled in blue, is it a bug in the UDF or i forgot something ?

#include "GraphGDIPlus.au3"
#include <WinAPISysWin.au3>

Opt("GUIOnEventMode", 1)

$GUI = GUICreate("",900,600)
GUISetOnEvent(-3,"_Exit")
GUISetState()

;----- Create Graph area -----
$Graph = _GraphGDIPlus_Create($GUI,40,30,830,520,0xFF000000,0xFFD6D6D6)

_GraphGDIPlus_Set_RangeX($Graph,0,4000,17,1,0)
_GraphGDIPlus_Set_RangeY($Graph,-100,0,20,1,0)

_GraphGDIPlus_Set_GridX($Graph,1,0xFF6993BE)
_GraphGDIPlus_Set_GridY($Graph,1,0xFF6993BE)

;----- Draw the graph -----
_Draw_Graph()

While 1
    Sleep(100)
WEnd


Func _Draw_Graph()
        _GraphGDIPlus_Refresh($Graph)
EndFunc


Func _Exit()
    ;----- close down GDI+ and clear graphic -----
    _GraphGDIPlus_Delete($GUI,$Graph)
    Exit
EndFunc

 

Edited by cetipabo
Link to comment
Share on other sites

Finaly i found, i just have to increase the value, i thought 1 was only to enable the ticks 😄 but you can set the amount of ticks you want, so i just changed the parameters like this, and now it works, i can see the grid.

_GraphGDIPlus_Set_GridX($Graph,50,0xFF6993BE)
_GraphGDIPlus_Set_GridY($Graph,5,0xFF6993BE)

 

Edited by cetipabo
Link to comment
Share on other sites

  • 1 year later...

Hi

I'm trying to modify the UDF to output colours depending on the Y input data.  I'm modifying the colour using RGB Dim $aColor[3] = [255, 255, 0] and then converting that into Hex using _ColorSetRGB($aColor) function to feed into $hPen = _GDIPlus_PenCreate().  What am I doing wrong?

Func _GraphGDIPlus_Plot_Point(ByRef $aGraphArray, $iX, $iY)
    If IsArray($aGraphArray) = 0 Then Return
    $hexcolour=getRGB($iY,0,$aGraphArray[9])
    $hPen = _GDIPlus_PenCreate($hexcolour, 2)
    ;----- Draw point from previous point to new point -----
    $iX = _GraphGDIPlus_Reference_Pixel("x", $iX, $aGraphArray[6], $aGraphArray[7], $aGraphArray[4])
    $iY = _GraphGDIPlus_Reference_Pixel("y", $iY, $aGraphArray[8], $aGraphArray[9], $aGraphArray[5])

    $hPen1 = _GDIPlus_PenCreate(_StringToHex("000255000"), 2)
    _GDIPlus_GraphicsDrawRect($aGraphArray[17], $iX - 1, $iY - 1, 2, 2, $hPen)
    _GraphGDIPlus_RedrawRect($aGraphArray)
    ;----- save current as last coords -----
    $aGraphArray[18] = $iX
    $aGraphArray[19] = $iY
EndFunc   ;==>_GraphGDIPlus_Plot_Point

Func getRGB($i,$min,$max)
    $i*=100
    $max*=100
    if $i < 0 then $i *= -1
    if $i > $max then Return
    Dim $aColor[3] = [255, 255, 0]
    if $i < $max/2 Then $aColor[0] = Map($i,$min,$max/2,0,255)
    if $i > $max then $aColor[1] = Map($i,$max/2,$max,255,0)
    ConsoleWrite(" Red=" & Hex($aColor[0], 2) & " Green=" & Hex($aColor[1], 2) & " Blue=" & Hex($aColor[2], 2) & "   Color=" & Hex(_ColorSetRGB($aColor)) & @CRLF)
    Return _ColorSetRGB($aColor)
EndFunc   ;==>getRGB

Func Map($iValue, $iFromLow, $iFromHigh, $iToLow, $iToHigh)
    Return round(($iValue - $iFromLow) * ($iToHigh - $iToLow) / ($iFromHigh - $iFromLow) + $iTolow,0)
EndFunc

 

Link to comment
Share on other sites

The color format for _GDIPlus_PenCreate("") should look something like "0xFF00FF00"
your function is creating a number 0000FF00 therefore need you'll need to add the "0x" in front of it.

the 2 bytes after 0x are the alpha channel. If this is set to 00 then there will be no line, as it is set to transparent, so you will need to change the value to "FF"

Moreover, your function is returning the array.

replace the return statement of the getRGB function with this:

Return "0xff" & Hex($aColor[0], 2) & Hex($aColor[1], 2) & Hex($aColor[1], 2)

 

The 

$hPen1 = _GDIPlus_PenCreate(_StringToHex("000255000"), 2)

Does not look right, either.

You should use "0xFF" & Hex("000",2) & Hex("255",2)  &  Hex("000",2) instead of _StringToHex.

 

 

Edited by Dan_555

Some of my script sourcecode

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...