{"id":1743,"date":"2008-03-21T13:07:03","date_gmt":"2008-03-21T13:07:03","guid":{"rendered":"http:\/\/www.amibroker.org\/userkb\/2008\/03\/21\/using-a-gfx-include-file\/"},"modified":"2008-05-01T18:13:01","modified_gmt":"2008-05-01T18:13:01","slug":"using-a-gfx-include-file","status":"publish","type":"post","link":"http:\/\/www.amibroker.org\/editable_userkb\/2008\/03\/21\/using-a-gfx-include-file\/","title":{"rendered":"Using a GFX Include file"},"content":{"rendered":"

Important note<\/strong>: The AmiBroker 5.09.0 Beta<\/a> introduced the following new GFX functions:<\/p>\n

Status(“pxchartleft”) – returns x-coordinate of top-left corner of chart area
\nStatus(“pxcharttop”) – returns y-coordinate of top-left corner of chart area
\nStatus(“pxchartright”) – returns x-coordinate of bottom-right corner of chart area
\nStatus(“pxchartbottom”) – returns y-coordinate of bottom-right corner of chart area
\nStatus(“pxchartwidth”) – returns width chart area (right-left)
\nStatus(“pxchartheight”) – returns width chart area (bottom-top)<\/p>\n

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<\/a> file.<\/p>\n

=====<\/p>\n

While the post Creating GFX Chart-Overlays (v2)<\/a> may have clarified a few of the more important aspects of using GFX functions, it doesn\u2019t really give you a \u201cquick Start\u201d 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:<\/p>\n

GetVisualBarIndex<\/span>( ); <\/span>\/\/ Returns array containing index for visible bars\r<\/span>gfxPlotHLine<\/span>( <\/span>YPixels<\/span>, <\/span>Color <\/span>); <\/span>\/\/ Plots horizontal line at level YPixels\r<\/span>gfxPlotVLine<\/span>( <\/span>XPixels<\/span>, <\/span>Color <\/span>); <\/span>\/\/ plots vertical line at level XPixels\r<\/span>GetYPixels<\/span>( <\/span>Y <\/span>); <\/span>\/\/ Convert a vertical price number to the pixel equivalent\r<\/span>GetXPixels<\/span>( <\/span>X <\/span>); <\/span>\/\/ Convert a horizontal DateTime number value to the pixel equivalent<\/span><\/pre>\n

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):<\/p>\n

\ngfxcrosshair.jpg<\/a><\/p>\n

Here is the code that produced the above image:<\/p>\n

GraphXSpace <\/span>= <\/span>5<\/span>; <\/span>\/\/ See the AmiBroker help on how to init these variables\r<\/span>GfxSetBkMode<\/span>( <\/span>bkmode <\/span>= <\/span>2 <\/span>);\r<\/span>GfxSetOverlayMode<\/span>( <\/span>mode <\/span>= <\/span>0 <\/span>);\r<\/span>GfxSelectPen<\/span>( <\/span>colorRed <\/span>);\r \r<\/span>Plot<\/span>( <\/span>C<\/span>, <\/span>""<\/span>, <\/span>colorBlack<\/span>, <\/span>styleLine <\/span>); <\/span>\/\/ To define miny and maxy\r#include <GFXInclude-001.afl> \/\/ Located in your default Include folder\r \r\/\/ Example to draw cross-hair cursor\r<\/span>Yprice <\/span>= <\/span>GetCursorYPosition<\/span>(<\/span>0<\/span>);\r<\/span>XIndex <\/span>= <\/span>SelectedValue<\/span>(<\/span>GetVisualBarIndex<\/span>( ));\r<\/span>gfxPlotHLine<\/span>( <\/span>GetYPixels<\/span>( <\/span>YPrice <\/span>), <\/span>colorRed <\/span>);\r<\/span>gfxPlotVLine<\/span>( <\/span>GetXPixels<\/span>( <\/span>XIndex <\/span>), <\/span>colorRed <\/span>);<\/span><\/pre>\n

The include file listed below defines the following variables:<\/p>\n

<\/span>\/\/ pxwidth, pxheight, Miny, MinX, YRange, VisBarIndex, NumBarsVisible, pxPaneWidth, pxPaneheight, PixelsPerBar, PixelsPerPrice<\/span><\/pre>\n

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.<\/p>\n

<\/span>\/\/ GFXInclude-001.afl copy to default include folder\r\r<\/span>function <\/span>gfxPlotHLine<\/span>( <\/span>YPixels<\/span>, <\/span>Color <\/span>)\r{\r    global <\/span>pxwidth<\/span>;\r    <\/span>GfxSelectPen<\/span>( <\/span>Color <\/span>) ;\r    <\/span>GfxMoveTo<\/span>( <\/span>0<\/span>, <\/span>YPixels <\/span>);\r    <\/span>GfxLineTo<\/span>( <\/span>pxwidth<\/span>, <\/span>YPixels <\/span>);\r}\r \rfunction <\/span>gfxPlotVLine<\/span>( <\/span>XPixels<\/span>, <\/span>Color <\/span>)\r{\r    global <\/span>pxheight<\/span>;\r    <\/span>GfxSelectPen<\/span>( <\/span>Color <\/span>) ;\r    <\/span>GfxMoveTo<\/span>( <\/span>XPixels<\/span>, <\/span>0 <\/span>);\r    <\/span>GfxLineTo<\/span>( <\/span>XPixels<\/span>, <\/span>pxheight <\/span>);\r}\r \rfunction <\/span>GetVisualBarIndex<\/span>( )\r{\r    <\/span>lvb <\/span>= <\/span>Status<\/span>( <\/span>"lastvisiblebar" <\/span>);\r    <\/span>fvb <\/span>= <\/span>Status<\/span>( <\/span>"firstvisiblebar" <\/span>);\r    <\/span>bi <\/span>= <\/span>BarIndex<\/span>();\r    <\/span>StaticVarSet<\/span>( <\/span>"NumberbarsVisible"<\/span>, <\/span>Lvb <\/span>- <\/span>fvb <\/span>+ <\/span>1 <\/span>);\r    return <\/span>bi <\/span>- <\/span>bi<\/span>[ <\/span>0 <\/span>] - <\/span>fvb<\/span>;\r}\r\rfunction <\/span>GetYPixels<\/span>( <\/span>Y <\/span>)\r    {\r    global <\/span>PixelsPerPrice<\/span>, <\/span>pxTopArea<\/span>, <\/span>MaxY<\/span>; \r    return (<\/span>MaxY <\/span>- <\/span>Y<\/span>) * <\/span>PixelsPerPrice <\/span>+ <\/span>pxTopArea<\/span>;\r    }\r\rfunction <\/span>GetXPixels<\/span>( <\/span>X <\/span>)\r    {\r    global <\/span>PixelsPerBar<\/span>, <\/span>pxLeftArea<\/span>;\r    return <\/span>X <\/span>* <\/span>PixelsPerBar <\/span>+ <\/span>pxLeftArea<\/span>;\r    }\r\r<\/span>_SECTION_BEGIN<\/span>(<\/span>"GFX INITIALIZATION"<\/span>);\r<\/span>\/\/ These Parameters will change depending on screen layout\/fonts\r<\/span>pxRightArea <\/span>= <\/span>Param<\/span>( <\/span>"Right Axis Area"<\/span>, <\/span>93<\/span>, <\/span>0<\/span>, <\/span>200<\/span>, <\/span>1 <\/span>); <\/span>\/\/ Depends on font\r<\/span>pxDateArea <\/span>= <\/span>Param<\/span>( <\/span>"Date Axis Area"<\/span>, <\/span>11<\/span>, <\/span>0<\/span>, <\/span>100<\/span>, <\/span>1 <\/span>); <\/span>\/\/ Depends on font\r<\/span>DateaxisOn <\/span>= <\/span>ParamToggle<\/span>( <\/span>"Date Axis"<\/span>, <\/span>"HIDE|SHOW"<\/span>, <\/span>1 <\/span>);\r\r<\/span>pxLeftArea <\/span>= <\/span>5<\/span>; \r<\/span>pxTopArea <\/span>= <\/span>5<\/span>; \r<\/span>pxBottomArea <\/span>= <\/span>5<\/span>; \rif ( <\/span>DateaxisOn <\/span>)\r{\r    <\/span>pxBottomArea <\/span>= <\/span>pxDateArea <\/span>+ <\/span>pxBottomArea<\/span>;\r    <\/span>SetChartOptions<\/span>( <\/span>2<\/span>, <\/span>chartShowDates <\/span>);\r}\relse\r    <\/span>SetChartOptions<\/span>( <\/span>3<\/span>, <\/span>chartShowDates <\/span>);\r\r<\/span>pxwidth <\/span>= <\/span>Status<\/span>( <\/span>"pxwidth" <\/span>);\r<\/span>pxheight <\/span>= <\/span>Status<\/span>( <\/span>"pxheight" <\/span>);\r\r<\/span>\/\/ clalculate charting area width and height\r<\/span>Miny <\/span>= <\/span>Status<\/span>( <\/span>"axisminy" <\/span>);\r<\/span>Maxy <\/span>= <\/span>Status<\/span>( <\/span>"axismaxy" <\/span>);\r<\/span>YRange <\/span>= <\/span>MaxY <\/span>- <\/span>MinY<\/span>;\r<\/span>VisBarIndex <\/span>=  <\/span>GetVisualBarIndex<\/span>( );\r<\/span>NumBarsVisible <\/span>= <\/span>StaticVarGet<\/span>( <\/span>"NumberbarsVisible" <\/span>);\r\r<\/span>\/\/ Calculate Pane width and height\r<\/span>pxPaneWidth <\/span>= <\/span>pxwidth <\/span>- <\/span>pxLeftArea <\/span>- <\/span>pxRightArea<\/span>;\r<\/span>pxPaneHeight <\/span>= <\/span>pxHeight <\/span>- <\/span>pxTopArea <\/span>- <\/span>pxBottomArea<\/span>;\r\r<\/span>\/\/ calculate conversion factors\r<\/span>PixelsPerBar     <\/span>= <\/span>pxPaneWidth <\/span>\/ <\/span>NumBarsVisible<\/span>;\r<\/span>PixelsPerPrice <\/span>= <\/span>pxPaneHeight <\/span>\/ <\/span>YRange<\/span>;\r<\/span>_SECTION_END<\/span>();<\/span><\/pre>\n

Edited by Al Venosa.<\/p>\n","protected":false},"excerpt":{"rendered":"

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 […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[138],"tags":[],"_links":{"self":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/1743"}],"collection":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/comments?post=1743"}],"version-history":[{"count":0,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/1743\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/media?parent=1743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/categories?post=1743"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/tags?post=1743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}