Get X and Y values under mouse pointer

2019-07-26 16:09发布

问题:

I have a simple chart using only TLineSeries with a single Y axis. As the mouse pointer moves over the chart, I’d like to get the X and Y values associated with the pointer’s position independent of any series.

I can handle the MouseMove event and get the screen X and Y coordinates, but the only way I’ve found to convert them is via the Series->XValues->Locate and Series->YValue->Locate methods.

There are two problems with this:

1 - The value returned from Series->YValue->Locate is always -1 regardless of whether the pointer is over a series line or not.

2 – The value returned from Series->XValue->Locate is -1 unless the pointer is over a part of the chart containing a series line.

Why does Series->YValue->Locate always return -1?

More importantly, how can I get the values regardless of whether the pointer is over a part of the chart with series lines or not?

I’m using the version of TeeChart that ships with Rad Studio XE3.

回答1:

Why does Series->YValue->Locate always return -1?

That's because Locate uses a series value and returns its point index in the series. OnMouseMove provides screen pixel coordinates, not series values.

More importantly, how can I get the values regardless of whether the pointer is over a part of the chart with series lines or not?

You can use axes as a reference instead of series, for example:

procedure TForm2.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var
  XVal: double;
  YVal: double;
begin
  XVal:=Chart1.Axes.Bottom.CalcPosPoint(X);
  YVal:=Chart1.Axes.Left.CalcPosPoint(Y);

  Chart1.Title.Text[0]:=FormatFloat('#.##', XVal) + ' - ' + FormatFloat('#.##', YVal);
end;


标签: teechart