amibroker

HomeKnowledge Base

How to convert from bar-value to pixel co-ordinates

NOTE: This article describes old method of using bar/value coordinates. New code should use GfxSetCoordsMode which allows you to use bar/value without any extra calculations.

Sometimes when using low-level graphics functions it is needed to convert from bar number to pixel X co-ordinate and from price level to pixel Y co-ordinate. Converting between them needs knowing visible bar range, Y-axis value range and pixel dimensions of drawing area. Once these params are known it is just a matter of performing simple scale transformation. The code example below shows how to do that.

function GetVisibleBarCount()
{
 
lvb Status("lastvisiblebar");
 
fvb Status("firstvisiblebar"); 

 return 
MinLvb fvbBarCount fvb );


function 
GfxConvertBarToPixelXbar )
{
 
lvb Status("lastvisiblebar");
 
fvb Status("firstvisiblebar");
 
pxchartleft Status("pxchartleft");
 
pxchartwidth Status("pxchartwidth"); 

 return 
pxchartleft bar  pxchartwidth / ( Lvb fvb );


function 
GfxConvertValueToPixelYValue )
{
 
local MinyMaxypxchartbottompxchartheight

 
Miny Status("axisminy");
 
Maxy Status("axismaxy"); 

 
pxchartbottom Status("pxchartbottom");
 
pxchartheight Status("pxchartheight"); 

 return 
pxchartbottom floor0.5 + ( Value Miny ) * pxchartheight/ ( Maxy Miny ) );


Plot(C"Price"colorBlackstyleHistogram ); 

GfxSetOverlayMode(0);
GfxSelectSolidBrushcolorRed );
GfxSelectPencolorRed ); 

AllVisibleBars GetVisibleBarCount();
fvb Status("firstvisiblebar"); 

for( 
0AllVisibleBars i++ ) 

  
GfxConvertBarToPixelX); 
  
GfxConvertValueToPixelYCfvb ] ); 

  
GfxRectanglex-1y-12y+); 


RequestTimedRefresh(1); // ensure 1 sec refres

Note that when chart scale changes, it will usually require one extra refresh to get low-level graphics alignment to new scale. That’s why we added RequestTimedRefresh call at the end.