Can a general clipping region be set for the Gfx routines?

In AFL, I wrote a few functions where I can draw "line rulers" on the screen between bars where the distance is measured in either ticks, points or monetary value (based on number of shares/contracts held). The problems occur when horizontally scrolling the chart. The standard clipping region for the chart with the Gfx functions is not set to Status("pxchartright") so all of the Gfx drawn lines bleed through into the Y axis labeling area to the right of Status("pxchartright").

I wrote the clipping code to keep the lines from going past Status("pxchartright"), but I guess my question is directed to Tomasz in asking couldn't all of this extra code I had to write in AFL be avoided by one Gfx AFL call to set the clipping (of the Windows device context) to Status("pxchartright")? It seems like the common case for all drawings made by the user which will need to be horizontally scrolled with the price.

Of course, the left side of the chart is perfect for clipping by default when horizontal scrolling because the clipping region for Status("pxchartleft") coincides with the farthest "left" you can draw on the chart's X axis.

[Guys, yes, I know there is a built-in line ruler drawing function in Amibroker which clips properly, but that's not the point. I could be doing anything with Gfx line drawing besides this one example and I would still have the same clipping area issue when horizontally scrolling the chart]

As part of the AFL clipping code I had to write, it was necessary to convert a bar index into an X pixel location on the chart. There is a formula in the Knowledge Base for this. However, it is incorrect. Here is a formula that works. The commented out return line is the old formula.

Look at mine. The right portion of the formula is calculating the percentage that the bar index value (i.e., bar) is from the leftmost bar index. Then, you convert it to an X pixel location by taking that percentage, multiplying it by the pxchartwidth and then adding it to the starting location of X pixel locations (i.e., pxchartleft).

All of this higher level need to clip Gfx line drawing functions (including this function call below) would be unnecessary if Amibroker could clip the Gfx drawing region at the graphics device context level to be Status("pxchartright"):

function GfxConvertBarToPixelX(bar)
{
	local lvb, fvb, pxchartleft, pxchartwidth;

	lvb = Status("lastvisiblebar");
	fvb = Status("firstvisiblebar");
	pxchartleft = Status("pxchartleft");
	pxchartwidth = Status("pxchartwidth"); 
	
	// return pxchartleft + bar  * pxchartwidth / (lvb - fvb + 1);
	
	return pxchartleft + pxchartwidth * ((bar - fvb + 1) / (lvb - fvb + 1)); 
}

Clipping is only available in Plot() function where you can clip min/max areas to draw clipped cloud charts as shown in the manual. It is made available, because there is no other way to achieve that affect.

But as far low-level graphics (Gfx*) functions are considered, there is no clip low-level gfx function that would do the clipping for you. Clipping at pixel level is expensive. It is much less expensive just NOT to draw when you should not be drawing. Calculate correct coordinates and draw only where you should be drawing.

It is quite simple actually, you don't need any special calculation, just don't draw beyond Status("pxchartwidth"). This code shows you safe area to draw:

GfxFillSolidRect( 0, 0, Status("pxchartwidth"), Status("pxchartheight"), colorRed );

Generally good programming is NOT to do unnecessary work, so in your own code you should avoid painting for example bars that are outside screen, etc. It is faster than painting things that should never be pained and clipping afterwards.

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.