Page 1 of 1

CalcXPos and XScreenToValue

Posted: Tue Oct 11, 2005 8:20 pm
by 8572663
What is the difference between CalcXPos and XScreenToValue?

I am using DrawZoomRectangle to draw a boxes on the screen (TColorGridSereis) and want to remember where the box is so that when the screen is resized (or Zoomed), I can redraw it in the correct position.

As an example, in the Chart1MouseDown and Up events, I have the following code to grab the Top, Left, Bottom and Right points of the box...

// Calculate the left x index.
ind := Chart1.Series[0].XScreenToValue(X);
fIERRecList[Len].iX0 := ind;
fIERRecList[Len].Left := -1;
for i := 0 to High(DblImageData[0]) do
if Abs(xScaleOff + (xScaleMul * i)) -
Abs(ind) < 0.00000001 then
begin
fIERRecList[Len].Left := Max(i-1, 0);
break;
end;

// Similar code as above for Right, Top and Bottom


Then when the form is resized, I have code like this...

// Calculate X and Y positions on the new resized screen.
fIERRecList.zX0 :=
Chart1.Series[0].CalcXPosValue(fIERRecList.iX0);
fIERRecList.zX1 :=
Chart1.Series[0].CalcXPosValue(fIERRecList.iX1);
fIERRecList.zY0 :=
Chart1.Series[0].CalcYPosValue(fIERRecList.iY0);
fIERRecList.zY1 :=
Chart1.Series[0].CalcYPosValue(fIERRecList.iY1);

// Calculate the Left x index.
for j := 0 to High(DblImageData[0]) do
if Abs(xScaleOff + (xScaleMul * j)) -
Abs(fIERRecList.iX0) < 0.00000001 then
begin
fIERRecList.Left := Max(j-1, 0);
break;
end;

// Similar code as above for Right, Top and Bottom

// Now I can redraw the box.

What I am doing here is working, but seems very inefficient and slow. Since I use an axis offset and multiplier, I do not know how to calculate the information I need directly without the For Loop. Is there I better way to do this? Is my explanation clear?

Posted: Thu Oct 13, 2005 8:58 am
by narcis
Hi leaf,
What is the difference between CalcXPos and XScreenToValue?
CalcXPos returns the pixel Screen Horizontal coordinate of the ValueIndex Series value.

XScreenToValue returns the numeric Value that corresponds to the specified Horizontal Screen coordinate. The resulting Value is based on the Series.GetHorizAxis.

So, XScreenToValue could return the bottom axis value based on the horizontal pixel screen coordinate returned by CalcXPos.
What I am doing here is working, but seems very inefficient and slow. Since I use an axis offset and multiplier, I do not know how to calculate the information I need directly without the For Loop. Is there I better way to do this? Is my explanation clear?


You could try using Locate method, its definition according to TeeChart help files is:
function Locate(Const AValue: TChartValue): Integer;

This new function returns the corresponding point index which has the specified “Value”. You can use it to calculate X co-ordinates based on Y values or vice-versa:

tmp:=LineSeries1.XValues.Locate(EncodeDate(1996,1,1));

Posted: Fri Oct 28, 2005 1:37 am
by 8572663
I do not seem to be able to get this to work. The Locate method seems to always return a -1. Even if I put a number I know is in the XValues array.

Are you sure this works with TColorGridSeries?

Posted: Fri Oct 28, 2005 10:44 am
by narcis
Hi leaf,

Yes, Locate works fine in TColorGridSeries, try:

Code: Select all

procedure TForm1.FormCreate(Sender: TObject);
var x,z: Integer;
begin
  for x:=0 to 10 do
    for z:=0 to 10 do
      Series1.AddXYZ(x,x+z,z);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Chart1.Title.Text[0]:=IntToStr(Series1.XValues.Locate(6));
end;
If you are still having problems, could you please send us an example we can run "as-is" to reproduce the problem here? You can post your files at [url]news://www.steema.net/steema.public.attachments[/url] newsgroup.