March 21, 2008
Using a GFX Include file
Important note: The AmiBroker 5.09.0 Beta introduced the following new GFX functions:
Status(“pxchartleft”) – returns x-coordinate of top-left corner of chart area
Status(“pxcharttop”) – returns y-coordinate of top-left corner of chart area
Status(“pxchartright”) – returns x-coordinate of bottom-right corner of chart area
Status(“pxchartbottom”) – returns y-coordinate of bottom-right corner of chart area
Status(“pxchartwidth”) – returns width chart area (right-left)
Status(“pxchartheight”) – returns width chart area (bottom-top)
Since this release appeared after this post was published these functions are not used in the examples below. This post has been left unchanged for educational purposes. For examples using the new functions please see the 5.09.0 Read Me file.
=====
While the post Creating GFX Chart-Overlays (v2) may have clarified a few of the more important aspects of using GFX functions, it doesn’t really give you a “quick Start” template to get started. Using a GFXInclude file can remove some of the burden of having to define pixel and charting parameters. The Include file at the bottom of this post contains most definitions as well as these common functions that you may want to call from your GFX application:
GetVisualBarIndex( ); // Returns array containing index for visible bars gfxPlotHLine( YPixels, Color ); // Plots horizontal line at level YPixels gfxPlotVLine( XPixels, Color ); // plots vertical line at level XPixels GetYPixels( Y ); // Convert a vertical price number to the pixel equivalent GetXPixels( X ); // Convert a horizontal DateTime number value to the pixel equivalent
Of course you can, and should, add additional functions of your own. Here is an example of how to call the above functions to draw a GFX cross-hair cursor (Red in the capture):
Here is the code that produced the above image:
GraphXSpace = 5; // See the AmiBroker help on how to init these variables GfxSetBkMode( bkmode = 2 ); GfxSetOverlayMode( mode = 0 ); GfxSelectPen( colorRed ); Plot( C, "", colorBlack, styleLine ); // To define miny and maxy #include <GFXInclude-001.afl> // Located in your default Include folder // Example to draw cross-hair cursor Yprice = GetCursorYPosition(0); XIndex = SelectedValue(GetVisualBarIndex( )); gfxPlotHLine( GetYPixels( YPrice ), colorRed ); gfxPlotVLine( GetXPixels( XIndex ), colorRed );
The include file listed below defines the following variables:
// pxwidth, pxheight, Miny, MinX, YRange, VisBarIndex, NumBarsVisible, pxPaneWidth, pxPaneheight, PixelsPerBar, PixelsPerPrice
You may want to copy the above comment line below the #include statement in your code to refresh your memory. You should copy the Include file to your default AmiBroker Include folder.
// GFXInclude-001.afl copy to default include folder function gfxPlotHLine( YPixels, Color ) { global pxwidth; GfxSelectPen( Color ) ; GfxMoveTo( 0, YPixels ); GfxLineTo( pxwidth, YPixels ); } function gfxPlotVLine( XPixels, Color ) { global pxheight; GfxSelectPen( Color ) ; GfxMoveTo( XPixels, 0 ); GfxLineTo( XPixels, pxheight ); } function GetVisualBarIndex( ) { lvb = Status( "lastvisiblebar" ); fvb = Status( "firstvisiblebar" ); bi = BarIndex(); StaticVarSet( "NumberbarsVisible", Lvb - fvb + 1 ); return bi - bi[ 0 ] - fvb; } function GetYPixels( Y ) { global PixelsPerPrice, pxTopArea, MaxY; return (MaxY - Y) * PixelsPerPrice + pxTopArea; } function GetXPixels( X ) { global PixelsPerBar, pxLeftArea; return X * PixelsPerBar + pxLeftArea; } _SECTION_BEGIN("GFX INITIALIZATION"); // These Parameters will change depending on screen layout/fonts pxRightArea = Param( "Right Axis Area", 93, 0, 200, 1 ); // Depends on font pxDateArea = Param( "Date Axis Area", 11, 0, 100, 1 ); // Depends on font DateaxisOn = ParamToggle( "Date Axis", "HIDE|SHOW", 1 ); pxLeftArea = 5; pxTopArea = 5; pxBottomArea = 5; if ( DateaxisOn ) { pxBottomArea = pxDateArea + pxBottomArea; SetChartOptions( 2, chartShowDates ); } else SetChartOptions( 3, chartShowDates ); pxwidth = Status( "pxwidth" ); pxheight = Status( "pxheight" ); // clalculate charting area width and height Miny = Status( "axisminy" ); Maxy = Status( "axismaxy" ); YRange = MaxY - MinY; VisBarIndex = GetVisualBarIndex( ); NumBarsVisible = StaticVarGet( "NumberbarsVisible" ); // Calculate Pane width and height pxPaneWidth = pxwidth - pxLeftArea - pxRightArea; pxPaneHeight = pxHeight - pxTopArea - pxBottomArea; // calculate conversion factors PixelsPerBar = pxPaneWidth / NumBarsVisible; PixelsPerPrice = pxPaneHeight / YRange; _SECTION_END();
Edited by Al Venosa.
Filed by Herman at 1:07 pm under GFX Programming
Comments Off on Using a GFX Include file