Get X and Y values under mouse pointer

二次信任 提交于 2019-12-24 17:20:04

问题


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;


来源:https://stackoverflow.com/questions/15441029/get-x-and-y-values-under-mouse-pointer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!