Your example do not show always the X value where the cursor is, only when he mouse is over a point.
I want catch the X value if the mouse is over the whole chart, not only when it is over a serie.
OK, you should have said that. You can add an OnBeforeMouseMove handler to the DatapointCrosshairTool which is fired before the nearest-point search is executed and which should display the current mouse position, e.g. in the statusbar. The event handler has the pixel coordinates as parameters, and you must convert them to the coordinates of the series by calling the chart's ImageToGraph() method which returns a TDoublePoint (you need unit TAChartUtils in "uses" for the compiler to accept this identifier).
uses
TAChartUtils;
procedure TForm1.ChartToolset1DataPointCrosshairTool1BeforeMouseMove(
ATool: TChartTool; APoint: TPoint);
var
DP: TDoublePoint;
begin
DP := Chart1.ImageToGraph(APoint);
Statusbar1.SimpleText := Format('X=%.3f, Y=%.3f', [DP.X, DP.Y]);
end;
This way you have both features: tracking the coordinates underneath the mouse plus snapping to the data points when the mouse comes sufficiently close to a data point.
If you don't want the latter feature (snapping to data points) all the time you should add another chart tool to do the display of the mouse coordinates. Since there is no special requirement for this tool you can use a TUserDefinedTool. The parameter Shift should be empty ([]) so that the tools becomes active whenever you simply move the mouse without pressing any button or key. Add this event handler to OnAfterMouseMove
procedure TForm1.ChartToolset1UserDefinedTool1AfterMouseMove(ATool: TChartTool;
APoint: TPoint);
var
DP: TDoublePoint;
begin
DP := Chart1.ImageToGraph(APoint);
Statusbar1.SimpleText := Format('X=%.3f, Y=%.3f', [DP.X, DP.Y]);
end;
The attached demo corresponds to that of the previous post, but is extended to display the non-data point cursor coordinates when you simply move the mouse across the chart (this is done by the UserDefinedTool). When you press the left mouse button and get close to a data point the cursor snaps to that data point and the statusbar displays the series name, data point index and data point x/y values. When you drag the mouse away from the data point, but hold the left button pressed, the snap is released and the normal mouse coordinates are displayed again. All this is done by the TDatapointCrosshairTool.
For the crosshair cursor to safely disappear when the mouse button is released you must provide an event handler for the crosshairtool's OnAfterMouseUp requesting the tool to hide:
procedure TForm1.ChartToolset1DataPointCrosshairTool1AfterMouseUp(
ATool: TChartTool; APoint: TPoint);
begin
(ATool as TDatapointCrossHairTool).Hide;
end;
When you press the CTRL key and get have the mouse near a data point a popup window with the data point info is displayed - this is done by the TDataPointHintTool. For the popup to disappear when the mouse is moved away from the data point, or the CTRL key is released you must set the "AutoFocus" of the chart to true.