Recent

Author Topic: mouse cursor depiction on line graph  (Read 901 times)

Firebird

  • New Member
  • *
  • Posts: 26
mouse cursor depiction on line graph
« on: July 27, 2024, 07:36:09 pm »
Dear all,

Is there a function available from the graph function library for selecting the cursor image on the x-y graph differently from the outside axis definition part of the graph for a line graph (see images 1 and 2)? Thanks for your feedback in advance.

wp

  • Hero Member
  • *****
  • Posts: 12366
Re: mouse cursor depiction on line graph
« Reply #1 on: July 27, 2024, 08:40:49 pm »
Write a handler for the chart's OnMouseMove event with the following code:

Code: Pascal  [Select][+][-]
  1. uses
  2.   TAChartUtils, TAMath;
  3.  
  4. function PointInRect(P: TDoublePoint; R: TDoubleRect): Boolean;
  5. begin
  6.   Result := InRangeWithLimits(P.X, R.A.X, R.B.X) and
  7.             InRangeWithLimits(P.Y, R.A.Y, R.B.Y);
  8. end;
  9.  
  10. procedure TForm1.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
  11.   Y: Integer);
  12. var
  13.   P: TDoublePoint;
  14.   ext: TDoubleRect;
  15. begin
  16.   ext := Chart1.CurrentExtent;
  17.   P := Chart1.ImageToGraph(Point(X, Y));
  18.   if PointInRect(P, ext) then
  19.     Screen.Cursor := crHandPoint
  20.   else
  21.     Screen.Cursor := crDefault;
  22. end;  

Chart1.CurrentExtent is the rectangle (in "real-world"-coordinates) spanned by the axes (well... not always). Chart1.ImageToGraph converts the mouse coordinates to "real-world" coordinates. Finally there is a custom-written function PointInRect which checks whether this transformed mouse point is inside the axis rectangle.

BTW, the cross cursor in your screenshot is not a standard cursor coming with the LCL. If you have it as a CUR file you can link it as a resource and load it from there which gives you the number which you can assign to Screen.Cursor (rather than crHandPoint in my sample code).
« Last Edit: July 27, 2024, 08:43:57 pm by wp »

Firebird

  • New Member
  • *
  • Posts: 26
Re: mouse cursor depiction on line graph
« Reply #2 on: July 28, 2024, 12:49:42 pm »
Thank you wp that really helps, in particular with the zooming function - drawing a rectangle to zoom into close to the graph borders! I am using an older version of Lazarus - the Cross cursor is part of that version, there is no problem with that.

 

TinyPortal © 2005-2018