XGraph v1.1.1.0 : Real time data plotting.

Post your working scripts, libraries and tools
User avatar
SKAN
Posts: 231
Joined: 29 Sep 2013, 16:58

XGraph v1.1.1.0 : Real time data plotting.

05 May 2014, 19:14

Easy to use, Light weight, fast, efficient GDI based function library for graphically plotting real time data.
Typical applications include real time usage/performance charts for CPU, HDD, Network etc and to a limited extent, it can also be used for historical charts like Weather, Stock etc.

Simple example follows. ( Another example )

Image

Code: [Select all] [Expand] [Download] (RandomPlot.ahk)GeSHi © Codebox Plus



The Library: XGraph.ahk

Code: [Select all] [Expand] [Download] (XGraph.ahk)GeSHi © Codebox Plus



The functions and parameters:

Image

    XGraph() Create and attach graph. Returns a pointer to Graph data:

  • hCtrl : Hwnd of Static control.
    • The control's dimension ( W x H ) should be present and exactly match the size of the background bitmap.
    • It is preferable to use a Text control instead of Picture control.
    • Do not use any styles/exstyles or associate a variable with this control. To be specific: The text control should not have a non-client area.
  • hBM : This parameter can be either hBitmap ( BPP 16|24 ) or COLORREF ( eg. 0x0000FF = Red ) that will be used as background.
    Note: The background bitmap will be applied to the control with STM_SETIMAGE. This means: The graph line may disappear when the timer is not running or the script is paused etc., But whenever window redraws, it draws the background bitmap and there is a guaranteed placeholder.
    • If hBitmap, the dimension ( W x H ) should exactly match the size of the control.
    • The bitmap can be a Grid returned by XGraph_MakeGrid(), or a vertical linear gradient returned by CreateDIB(). A horizontal linear gradient will produce undesirable result whilst plotting.
    • If this parameter is a COLORREF ( eg. 0x0000FF = Red ), XGraph creates a 16BPP bitmap and fills it with COLORREF passed.
    • Default value is 0x000000 and a black bitmap matching control's dimension is automatically created.
  • ColumnW : Column width for each graph line, in pixels.
    • Total columns is derived from this parameter. eg. For a background bitmap width of 600px with no left/right margins and ColumnW of 3, the Total columns will be 600//3 = 200 Columns.
    • Default value is 2 and valid range is from 1 to 255.
  • LTRB : Margin for Left,Top,Right,Bottom.
    • XGraph derives a virtual client area ( or graph area ) using the margins specified. ( and is the reason why text control should not have styles like WS_BORDER or SS_SUNKEN )
    • Image transfer will occur from Memory bitmap to screen, only for the derived graph area. A background bitmap can be many times large than the graph area and LTRB allows you to specify the client rectangle where it actually needs to be drawn.
    • Default value is 0,2,0,2.
  • PenColor : The line color of graph passed as COLORREF.
    • Default value is 0x808080 and valid range is from 0 to 0xFFFFFF. Note: 24bit color is enforced.
  • PenSize : Thickness of graph line.
    • If 0 is passed, graph line will be short by 1px per column and will result in broken lines. This is a feature.
    • Default value is 1 and valid range is from 0 to 15.
    • Note: GDI lines are not anti-aliased. Line thickness and background color plays an important role in graph's appearance. Pen size of 1 or 2 will be ideal in most situations.
  • SV : Pass True to enable "Set Value" feature. Allocates space for internal array.
    • When SV is true, XGraph allocates memory space of 8 bytes ( data type = double ) for each column of graph.
    • Enabling internal array allows you to store/retrieve values with XGraph_Plot(), XGraph_SetVal(), XGraph_GetVal() and XGraph_GetMean().
    • Default value is False.

Image

    XGraph_Plot() Plots graph line on FIFO basis. ( FIFO = First In, First Out ):
    For a graph with 30 columns, when 31st value is passed, the first value will move out of the graph.
    When called with single parameter, XGraph_Plot( pGraph ) will update screen without any actual plotting.

  • pGraph : Pointer to Graph data.
  • MY2 : The Y axis coordinate.
    • Pass a null value if you want to refresh/redraw the graph ( Draw should be True ).
    • Default value is NULL
  • SetVal : A Column value to store.
    • Updates internal array on FIFO basis.
    • Value will be ignored if MY2 is NULL.
    • Value will be ignored if internal array was not enabled when graph was created with XGraph().
    • Default value is NULL
  • Draw : Transfers the graph from memory to screen.
    • Default value is True.
    • For historical graphs, you can pass False until all columns have been plotted and then call XGraph_Plot( pGraph ) to update the screen.

Image

    XGraph_Detach() Detaches graph from control and releases/frees the resources is use:

  • pGraph : Pointer to Graph data.
    • Passing invalid pointer to XGraph_Plot() etc, will result in appcrash. When successful, XGraph_Detach() returns 0, so it is advised to store the return value from this function to pGraph itself.
    • Correct usage: pGraph := XGraph_Detach( pGraph ). Functions that require pGraph as parameter will safely fail when pGraph equals zero

Image

    XGraph_GetVal() Returns the associated value for a graph column:
    Note: Internal array should have been enabled when creating with XGraph().

  • pGraph : Pointer to Graph data.
  • Column : Valid column number in graph.
    • When invalid column number is passed, the value for the last column will be returned.
    • Default parameter is NULL
    • If this parameter is NULL, and the mouse pointer is over any of the columns, then value for the column under mouse will be returned and ErrorLevel will contain the column number under mouse
    • Suggested usage: Assign a subroutine [ GoSub Label ) to text control and from the subroutine call XGraph_GetVal() and display the return value in Status bar. Well, one would be tempted to use ToolTip, but it will trigger WM_PAINT() which will erase the graph line. The workaround is to call XGraph_Plot( pGraph ) every time WM_PAINT() is triggered.

Image

    XGraph_SetVal() Manually sets a value for any column, Returns pointer to written data :
    Note: Internal array should have been enabled when creating with XGraph().

  • pGraph : Pointer to Graph data.
  • Double : The value.
  • Column : Valid column number in graph. Invalid number will set the value for last column.

Image

    XGraph_GetMean() Returns arithmetic mean for specified no. of columns:
    Note: Internal array should have been enabled when creating with XGraph().
    ErrorLevel will contain the number of columns used for mean calculation.

  • pGraph : Pointer to Graph data.
  • TailCols : Number of columns ( at the tail of internal array ).

Image

    XGraph_MakeGrid() Creates a bitmap and draws a grid on it, Returns hBitmap :

  • CellW : Cell width should be same as Column width parameter of XGraph()
  • CellH : Cell height.
  • Cols : Columns required.
  • Rows : Rows required.
  • GLClr : Color of grid line. Note: 24bit color is enforced.
  • BGClr : Background color. You can pass a hBitmap, which will be re-sized to fit the grid. You may create and pass a vertical linear gradient with CreateDIB()
  • BMPW : ByRef. Width of Bitmap.
  • BMPH : ByRef. Height of Bitmap.

Image

    XGraph_Info() Returns either graph properties or Internal array data

  • pGraph : Pointer to Graph data.
  • FormatFloat:
    • If this parameter is NULL, the function returns graph properties.
    • If this parameter is non-NULL, it must be a valid float format like "0.2" which return all values from internal array

Image
:: end of documentation ::

Sample code posted in following topic should demonstrate most of the functionality:
CPULoad() : CPU usage percentage ( Demos for XGraph )
Screenshots from above topic:

Image

Image

Image

:)
User avatar
joedf
Posts: 5676
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: XGraph v1.1.1.0 : Real time data plotting.

05 May 2014, 21:15

WOW! THIS IS WAY COOL! I HAVE TO TRY THIS! :D
strobo
Posts: 125
Joined: 30 Sep 2013, 15:24

Re: XGraph v1.1.1.0 : Real time data plotting.

07 May 2014, 16:20

Killer -- in the meaning of "looks promising"**2.
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: XGraph v1.1.1.0 : Real time data plotting.

07 May 2014, 22:05

i guess this script could be also used to generate charts, too? i mean static charts like in Excel (with labels and all)? any examples of such charts?
User avatar
Learning one
Posts: 119
Joined: 04 Oct 2013, 13:59
Location: Croatia
Contact:

Re: XGraph v1.1.1.0 : Real time data plotting.

08 May 2014, 00:56

Guest10, look here; BarChart
Nice library SKAN! :)
User avatar
SKAN
Posts: 231
Joined: 29 Sep 2013, 16:58

Re: XGraph v1.1.1.0 : Real time data plotting.

08 May 2014, 05:10

Learning one wrote:Nice library SKAN! :)


Thanks. :)

Guest10 wrote:static charts like in Excel


GDI is very limited. Excel kind of charts requires GDI Plus. Learning one's Lib looks great :).

XGraph was specifically written to plot real time data at high speeds with bare minimum resources.

Here is an example:
When you move your mouse cursor fast enough, it can use 100% of CPU for 1ms which will not be caught by a CPU load monitor running with 1000ms timer as 100% usage for 1ms translates to 0.001% usage for 1000ms. Windows Task Manager's fastest speed is like 250ms. Any faster, the chart would peak as Task manager itself uses lot of CPU time.
You can call XGraph_Plot() several thousands of times without registering 1 second of CPU time... But of course, what you plot does matter. GetSystemTimes() does not use much CPU whereas 'Random' command does.

If not anything else, XGraph() can be a good performance testing tool.
I actually wrote it to monitor the performance of my Mobile dial-up connection ( EDGE ) which is as erratic as Random plotting.
User avatar
SKAN
Posts: 231
Joined: 29 Sep 2013, 16:58

XGraph_GetMean()

08 May 2014, 15:36

Update: Added new function XGraph_GetMean(). This function can return the average value for X number of columns from the end of internal array.

Here follows a demo that shows how to combine XGraph_SetVal() and XGraph_GetMean() to smoothen graph line.

Image

Code: [Select all] [Expand] [Download] (SmoothGraph.ahk)GeSHi © Codebox Plus

User avatar
tank
Posts: 2061
Joined: 28 Sep 2013, 22:15
Facebook: charlie.simmons.7334
GitHub: ttnnkkrr
Location: Louisville KY
Contact:

Re: XGraph v1.1.1.0 : Real time data plotting.

09 Jun 2014, 11:52

I wonder about data points?
dots on the line for each data point?
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
https://www.facebook.com/ahkscript.org
User avatar
SKAN
Posts: 231
Joined: 29 Sep 2013, 16:58

Re: XGraph v1.1.1.0 : Real time data plotting.

09 Jun 2014, 13:41

Hi tank, :)

you wrote:dots on the line for each data point


GDI is fast and efficient for a moving graph..
But since there is no anti-aliasing in GDI, I guess dots would not look good on the graph.
User avatar
evilC
Posts: 3382
Joined: 27 Feb 2014, 12:30

Re: XGraph v1.1.1.0 : Real time data plotting.

11 Jun 2014, 11:16

Hi,
Could this maybe be repurposed for a regular chart?

I would like to create something like the windows joystick test dialog using AHK.

ie, replicate the functionality of the moving "+" in the X/Y chart.
This dialog does not update when it does not have focus, whereas AHK does.

I would like to make a tool for YouTubers that generates graphics to represent state of keyboard / mouse / joystick, for tutorial videos etc.

Image
colt
Posts: 1
Joined: 04 Aug 2014, 23:12

Re: XGraph v1.1.1.0 : Real time data plotting.

05 Aug 2014, 18:38

Thank you so much for posting this! I will be using this library extensively in my future projects.

Are there any plans to support resizing of the graph after it has been created? Currently resizing the text control resizes the background but does not scale the actual graph region.

-Colt

update- a project using xgraph to plot csv data from a file http://www.autohotkey.com/board/topic/145386-csv-data-visualizer-using-xgraphahk/?hl=%2Bxgraph
Last edited by colt on 15 Jul 2015, 00:18, edited 1 time in total.
architech1906

Re: XGraph v1.1.1.0 : Real time data plotting.

26 Oct 2014, 23:06

Can this script allow multiple lines of real time data on the same graph. If so, how? I only see an implementation where one data group per plot. I'm still looking through your code but not sure how it would come together to generate multiple plots on a single graph.
Thanks for any assistance you can provide.
User avatar
Scoox
Posts: 85
Joined: 11 May 2014, 09:12
Location: China

Re: XGraph v1.1.1.0 : Real time data plotting.

13 Nov 2014, 00:34

Looks awesome, thanks for sharing!
ThomasB

Re: XGraph v1.1.1.0 : Real time data plotting.

01 Dec 2014, 03:45

like "architech1906" I tried to use it with multiple graphs using the same axis, but for me it seems at the moment it is not possible.

Any idea?
User avatar
moefr01
Posts: 28
Joined: 25 Nov 2015, 09:01
Location: Germany

Re: XGraph v1.1.1.0 : Real time data plotting.

17 Jul 2016, 04:53

Really great lib, SKAN... thank you for sharing! :clap:

any idea how to implement an filled area underneath the dataplot, with different (fading) colors and transparency for the negative values?
(see attached)

It would incredibly increase the readability from a far distance if you've to monitor different machines at the same time by showing each plot.

cheers
moefr01
Attachments
win10-graph-performance1.jpg
win10-graph-performance1.jpg (18.65 KiB) Viewed 4112 times
win10-graph-ressources.jpg
win10-graph-ressources.jpg (27.71 KiB) Viewed 4112 times

Return to “Scripts and Functions”

Who is online

Users browsing this forum: kczx3, Spikea and 13 guests