{"id":78,"date":"2009-03-30T04:45:12","date_gmt":"2009-03-30T09:45:12","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/?p=78"},"modified":"2019-04-17T17:46:34","modified_gmt":"2019-04-17T16:46:34","slug":"how-to-convert-from-bar-value-to-pixel-co-ordinates","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2009\/03\/30\/how-to-convert-from-bar-value-to-pixel-co-ordinates\/","title":{"rendered":"How to convert from bar-value to pixel co-ordinates"},"content":{"rendered":"

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

Sometimes when using low-level graphics functions<\/a> 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.<\/p><\/span>function <\/span>GetVisibleBarCount<\/span>()
{
 <\/span>lvb <\/span>= <\/span>Status<\/span>(<\/span>"lastvisiblebar"<\/span>);
 <\/span>fvb <\/span>= <\/span>Status<\/span>(<\/span>"firstvisiblebar"<\/span>); 

 return <\/span>Min<\/span>( <\/span>Lvb <\/span>- <\/span>fvb<\/span>, <\/span>BarCount <\/span>- <\/span>fvb <\/span>);


function <\/span>GfxConvertBarToPixelX<\/span>( <\/span>bar <\/span>)
{
 <\/span>lvb <\/span>= <\/span>Status<\/span>(<\/span>"lastvisiblebar"<\/span>);
 <\/span>fvb <\/span>= <\/span>Status<\/span>(<\/span>"firstvisiblebar"<\/span>);
 <\/span>pxchartleft <\/span>= <\/span>Status<\/span>(<\/span>"pxchartleft"<\/span>);
 <\/span>pxchartwidth <\/span>= <\/span>Status<\/span>(<\/span>"pxchartwidth"<\/span>); 

 return <\/span>pxchartleft <\/span>+ <\/span>bar  <\/span>* <\/span>pxchartwidth <\/span>\/ ( <\/span>Lvb <\/span>- <\/span>fvb <\/span>+ <\/span>1 <\/span>);


function <\/span>GfxConvertValueToPixelY<\/span>( <\/span>Value <\/span>)
{
 <\/span>local Miny<\/span>, <\/span>Maxy<\/span>, <\/span>pxchartbottom<\/span>, <\/span>pxchartheight<\/span>

 <\/span>Miny <\/span>= <\/span>Status<\/span>(<\/span>"axisminy"<\/span>);
 <\/span>Maxy <\/span>= <\/span>Status<\/span>(<\/span>"axismaxy"<\/span>); 

 <\/span>pxchartbottom <\/span>= <\/span>Status<\/span>(<\/span>"pxchartbottom"<\/span>);
 <\/span>pxchartheight <\/span>= <\/span>Status<\/span>(<\/span>"pxchartheight"<\/span>); 

 return <\/span>pxchartbottom <\/span>- <\/span>floor<\/span>( <\/span>0.5 <\/span>+ ( <\/span>Value <\/span>- <\/span>Miny <\/span>) * <\/span>pxchartheight<\/span>\/ ( <\/span>Maxy <\/span>- <\/span>Miny <\/span>) );


<\/span>Plot<\/span>(<\/span>C<\/span>, <\/span>"Price"<\/span>, <\/span>colorBlack<\/span>, <\/span>styleHistogram <\/span>); 

<\/span>GfxSetOverlayMode<\/span>(<\/span>0<\/span>);
<\/span>GfxSelectSolidBrush<\/span>( <\/span>colorRed <\/span>);
<\/span>GfxSelectPen<\/span>( <\/span>colorRed <\/span>); 

<\/span>AllVisibleBars <\/span>= <\/span>GetVisibleBarCount<\/span>();
<\/span>fvb <\/span>= <\/span>Status<\/span>(<\/span>"firstvisiblebar"<\/span>); 

for( <\/span>i <\/span>= <\/span>0<\/span>; <\/span>i <\/span>< <\/span>AllVisibleBars <\/span>; <\/span>i<\/span>++ ) 

  <\/span>x <\/span>= <\/span>GfxConvertBarToPixelX<\/span>( <\/span>i <\/span>); 
  <\/span>y <\/span>= <\/span>GfxConvertValueToPixelY<\/span>( <\/span>C<\/span>[ <\/span>i <\/span>+ <\/span>fvb <\/span>] ); 

  <\/span>GfxRectangle<\/span>( <\/span>x<\/span>-<\/span>1<\/span>, <\/span>y<\/span>-<\/span>1<\/span>, <\/span>x <\/span>+ <\/span>2<\/span>, <\/span>y<\/span>+<\/span>1 <\/span>); 


<\/span>RequestTimedRefresh<\/span>(<\/span>1<\/span>); <\/span>\/\/ ensure 1 sec refres<\/code>

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.<\/p>","protected":false},"excerpt":{"rendered":"

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