Calculate size of font, in bar/price coordinate system mode


I am trying to create a proc that will take a text and draw it on the chart, with a rectangular line/border around it.

I prefer to work with the GfxSetOverlayMode(1);

I would like to allow for a small margin between the text and the border. The size of the margin should be about 30% of the fost size. Is there a way to do this?

I did see the conversion functions between the coordinate systems in this page:

Can I avoid using these?
GfxConvertBarToPixelX( bar )
GfxConvertValueToPixelY( Value )

Here is the code I came up. I still need to calculate margin_height and margin_width.

function PrintInCell( string, x1, y1, x2, y2, pformat )
GfxSetCoordsMode( 1 ); // bar/price mode (instead of pixel)
GfxSelectFont("Tahoma", 10 );
margin_height = 0;
margin_width = 0;
GfxRectangle( x1-margin_width, y1+margin_height, x2-margin_width, y2-margin_height ) ;
GfxDrawText( string, x1, y1, x2, y2 , pformat  );

This piece of code is straight from the manual and therefore it works.

CellHeight = 20; 
CellWidth = 100; 
GfxSelectFont( "Tahoma", CellHeight/2 ); 

The rest is an exercise for a veteran :slight_smile:

Font size is usually the distance from an ascender to descender.

1 Like


You are talking about the code in this page:

I am using this command:

GfxSetCoordsMode( 1 ); 

The page you mentioned does not use the "GfxSetCoordsMode" command, therefore the default value applies here:

GfxSetCoordsMode( 0 ); 

Any veteran can help with the values of margin_height and margin_width please?

GfxSetCoordsMode() can be called back and forth.

But what you are asking without conversion means you have to design the scale yourself.

This I presume would be to get MaxY and MinY from status, see how many points that is and then come to a sort of scale.
I don't see any other way around it because if you use fix dollar, its definitely going to mess up.
One scrip can be from $5 to 10, another $100-120 and so on.

Will ponder over it, but I think there is no need to re-invent the wheel.

1 Like

@bobptz in my opinion you usually tend to unnecessarily overcomplicate things. Can't you simply use PlotText or PlotTextSetFont? You can choose the text (and rectangle) size, color and the font in just one line without any hassle. For instance:

x = SelectedValue( BarIndex() ) + 1;
PlotTextSetFont( "  High  ", "Arial Black", 10, x , SelectedValue( H ), colorWhite, colorGreen, 0 );
PlotText( "  Low  ", x, SelectedValue( L ), colorWhite, colorRed, -10 );



hehe, good catch @Milosz

I think I wasn't alone in the observation with @bobptz but there is a tendency that occurs to deviate from the obvious :smiley:
or he probably is thinking too much in C++ that he forgets the simplification brought by AFL.

1 Like

@Milosz's solution is very practical, although I cannot control the alignment. I wanted to RIGHT align the text.

I saw this example ( ) and I wanted to work based on it. But it seems to work only with the default "GfxSetCoordsMode( 0 );"

// formatted text output
      sample via low-level gfx functions 
      CellHeight = 20; 
      CellWidth = 100; 
      GfxSelectFont( "Tahoma",
      CellHeight/2 ); 
      function PrintInCell(
      string, row, Col ) 
      GfxDrawText( string,
      Col * CellWidth, row * CellHeight, (Col + 1 )
      * CellWidth, (row + 1 )
      * CellHeight, 0 ); 
      PrintInCell( "Open", 0, 0 ); 
      PrintInCell( "High", 0, 1 ); 
      PrintInCell( "Low", 0, 2 ); 
      PrintInCell( "Close", 0, 3 ); 
      PrintInCell( "Volume", 0, 4 ); 
      GfxSelectPen( colorBlue ); 
      for( i = 1;
      i < 10 && i < BarCount;
      i++ ) 
PrintInCell( StrFormat("%g", O[
i ] ), i, 0 ); 
PrintInCell( StrFormat("%g", H[
i ] ), i, 1 ); 
PrintInCell( StrFormat("%g", L[
i ] ), i, 2 ); 
PrintInCell( StrFormat("%g", C[
i ] ), i, 3 ); 
PrintInCell( StrFormat("%g", V[
i ] ), i, 4 ); 
GfxMoveTo( 0,
i * CellHeight ); 
GfxLineTo( 5 *
CellWidth, i * CellHeight ); 
GfxMoveTo( 0,
i * CellHeight ); 
GfxLineTo( 5 *
CellWidth, i * CellHeight ); 

for( Col = 1;
Col < 6; Col++ ) 
GfxMoveTo( Col * CellWidth, 0); 
GfxLineTo( Col * CellWidth, 10 *
CellHeight ); 


Anyway, it works good enough. No need to spend more time on the aesthetics of the chart.

Thank you guys.


x = SelectedValue( BarIndex() ) + 1;
PlotTextSetFont( " High         ", "Arial Black", 10, x , SelectedValue( H ), colorWhite, colorGreen, 0 );
PlotText( "           Low ", x, SelectedValue( L ), colorWhite, colorRed, -10 );
1 Like

If you want to Right Align, I presume it is for Floats.
I have this function that you can modify and get appropriate padding with PlotText()

For me this works but you can change it based on other counts also.

function zz( mys )
return  mys = WriteIf( mys > 4999, NumToStr( mys, 10.0, False), 
	      WriteIf( mys > 999,  NumToStr( mys, 10.1, False), 
		                   NumToStr( mys, 10.2, False) ));

You can increase/decrease precision ( or scale ) with the magnitude of value,
so 10023447 with two decimal points isn't of much use.

1 Like

No, I was thinking to right-align to some X2 value, where X2 would be the rightmost X value of the square. (in this case the lowest bar of the swing)

Never mind, too much hasle.

You should have illustrated what you were trying to achieve in your first post, not in the last one...

If it is worth the hassle or not, depends on you.

If you want to output just a few numbers on the chart in corellation with the chart (i.e. label peaks and bottoms) I would stick to very simple and effective solution which I have suggested above.

If you want to output longer text in some cell/rectangle and be able to align it, precisely format and break into lines - like in the example below:


... you need to use GfxDrawText() and GfxSetTextAlign()



Thank you for the threads you pointed to me. They taught me many new things.

1 Like