Recent

Author Topic: Gettin X value of a serie on mouse move  (Read 5186 times)

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Gettin X value of a serie on mouse move
« on: December 04, 2018, 06:22:09 am »
Hi all;
I have a Tchart with 3 lineseries. When the user mouse the mouse over the char, i want get the X value of where the cursor is, to show some info in a label.
I had tried many examples i found, but i cant find the way.
It is with procedure Chart1MouseMove?
I need a toolset?
My source data is in an array, so i only need the value of the X position.

I hope someone can help me.
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

wp

  • Hero Member
  • *****
  • Posts: 11830
Re: Gettin X value of a serie on mouse move
« Reply #1 on: December 04, 2018, 09:31:33 am »
Yes, a toolset is that what you need. Since you want to display the data point info in a label I'd recommend the TDatapointCrosshairTool which also displays a crosshair cursor. In spite of its name, you can hook into the OnDraw event to display the message. Try attached demo. And read http://wiki.lazarus.freepascal.org/TAChart_Tutorial:_Chart_Tools to learn more about ChartTools.

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Re: Gettin X value of a serie on mouse move
« Reply #2 on: December 05, 2018, 02:21:28 am »
Yes, a toolset is that what you need. Since you want to display the data point info in a label I'd recommend the TDatapointCrosshairTool which also displays a crosshair cursor. In spite of its name, you can hook into the OnDraw event to display the message. Try attached demo. And read http://wiki.lazarus.freepascal.org/TAChart_Tutorial:_Chart_Tools to learn more about ChartTools.
Not sure if your example fits for my needs.
My chart is small, but it have 3 lineseries and a x-axis up to 200 entries. Must i add one panel for every entry?
I want be able to read the X position of the mouse, so i can show the value of every serie in a label.
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

wp

  • Hero Member
  • *****
  • Posts: 11830
Re: Gettin X value of a serie on mouse move
« Reply #3 on: December 05, 2018, 12:10:17 pm »
Panel? What do you mean?

In the attachment there's a modified version of the previously posted demo. It contains now three series and more data points, but still one DataPointcrosshairTool to show that it can handle all series. Look at the statusbar when the mouse is above a data point: the series name is reckognized and displayed.

kinlion

  • Jr. Member
  • **
  • Posts: 82
  • I Love Lazarus

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Re: Gettin X value of a serie on mouse move
« Reply #5 on: December 09, 2018, 08:09:07 am »
Thanks a lot for your help.
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.
I dont know if im enough specific   :'(

I readed the Tchart tutorial before i asked here; it helped me a lot with zooming.

Panel? What do you mean?

In the attachment there's a modified version of the previously posted demo. It contains now three series and more data points, but still one DataPointcrosshairTool to show that it can handle all series. Look at the statusbar when the mouse is above a data point: the series name is reckognized and displayed.
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

wp

  • Hero Member
  • *****
  • Posts: 11830
Re: Gettin X value of a serie on mouse move
« Reply #6 on: December 09, 2018, 11:59:57 am »
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).

Code: Pascal  [Select][+][-]
  1. uses
  2.   TAChartUtils;
  3.  
  4. procedure TForm1.ChartToolset1DataPointCrosshairTool1BeforeMouseMove(
  5.   ATool: TChartTool; APoint: TPoint);
  6. var
  7.   DP: TDoublePoint;
  8. begin
  9.   DP := Chart1.ImageToGraph(APoint);
  10.   Statusbar1.SimpleText := Format('X=%.3f, Y=%.3f', [DP.X, DP.Y]);
  11. 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
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ChartToolset1UserDefinedTool1AfterMouseMove(ATool: TChartTool;
  2.   APoint: TPoint);
  3. var
  4.   DP: TDoublePoint;
  5. begin
  6.   DP := Chart1.ImageToGraph(APoint);
  7.   Statusbar1.SimpleText := Format('X=%.3f, Y=%.3f', [DP.X, DP.Y]);
  8. 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:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ChartToolset1DataPointCrosshairTool1AfterMouseUp(
  2.   ATool: TChartTool; APoint: TPoint);
  3. begin
  4.   (ATool as TDatapointCrossHairTool).Hide;
  5. 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.

« Last Edit: December 09, 2018, 12:03:35 pm by wp »

 

TinyPortal © 2005-2018