Recent

Author Topic: Crosshair following Pointer  (Read 964 times)

chucky

  • New Member
  • *
  • Posts: 40
Crosshair following Pointer
« on: February 05, 2025, 09:13:17 pm »
I want to display a simple crosshair on my chart that's always visible even when its not close to a datapoint and also remain fixed to the pointer and not jump from point to point.

Is it possble to use the TDataPointCrosshairTool for that or is their another way to create it in TChart?


wp

  • Hero Member
  • *****
  • Posts: 12696
Re: Crosshair following Pointer
« Reply #1 on: February 06, 2025, 11:21:28 am »
Use a TUserDefinedTool and add this handler to the OnAfterMouseMove event:
Code: Pascal  [Select][+][-]
  1. uses
  2.   Math, TAChartUtils;
  3.  
  4. procedure TForm1.ChartToolset1UserDefinedTool1AfterMouseMove(ATool: TChartTool;
  5.   APoint: TPoint);
  6. const
  7.   SIZE = 24;
  8. var
  9.   ext: TDoubleRect;
  10.   P: TDoublePoint;
  11. begin
  12.   Chart1.Repaint;
  13.   P := Chart1.ImageToGraph(APoint);
  14.   ext := Chart1.CurrentExtent;
  15.   if InRange(P.X, ext.a.X, ext.b.x) and InRange(P.Y, ext.a.Y, ext.b.Y) then
  16.   begin
  17.     Chart1.Canvas.Pen.Color := clRed;
  18.     Chart1.Canvas.Pen.Style := psSolid;
  19.     Chart1.Canvas.Pen.Width := 3;
  20.     Chart1.Canvas.Line(APoint.X, APoint.Y-SIZE, APoint.X, APoint.Y+SIZE);
  21.     Chart1.Canvas.Line(APoint.X-SIZE, APoint.Y, APoint.X+SIZE, APoint.Y);
  22.   end;
  23. end;

chucky

  • New Member
  • *
  • Posts: 40
Re: Crosshair following Pointer
« Reply #2 on: February 11, 2025, 05:11:39 pm »
Thanks so Much!!!

kapibara

  • Hero Member
  • *****
  • Posts: 641
Re: Crosshair following Pointer
« Reply #3 on: February 11, 2025, 09:13:01 pm »
The crosshair is seen when the mouse is moved, and disappears when mouse stops to move. Should it be like that? Is it possible for the crosshair to be visible all the time?
Lazarus trunk / fpc 3.2.2 / Kubuntu 24.04 - 64 bit

chucky

  • New Member
  • *
  • Posts: 40
Re: Crosshair following Pointer
« Reply #4 on: February 11, 2025, 11:41:11 pm »
Yes I noticed that too. WP's code example was really helpful but I realized I didnt ask the question clearly.

I moved it to the Chart object OnMouseMove event and did it this way:

Code: Pascal  [Select][+][-]
  1. procedure TChartForm.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,Y: Integer);
  2.  
  3. var
  4.   ext: TDoubleRect;
  5.   P : TDoublePoint;
  6.   G : TPoint;
  7.  
  8. begin
  9.   If Not Chart1.Enabled then
  10.     Exit;
  11.   Chart1.Repaint;
  12.   G.X := X;
  13.   G.Y := Y;
  14.   P := Chart1.ImageToGraph(G);
  15.   ext := Chart1.CurrentExtent;
  16.   if InRange(P.X, ext.a.X, ext.b.x) and InRange(P.Y, ext.a.Y, ext.b.Y) then
  17.   begin
  18.     Chart1ConstantLine1.Position := P.Y;
  19.     Chart1ConstantLine2.Position := P.X;
  20.     Chart1ConstantLine1.Active := true;
  21.     Chart1ConstantLine2.Active := true;
  22.   end
  23.   Else
  24.     begin
  25.       Chart1ConstantLine1.Active := false;
  26.       Chart1ConstantLine2.Active := false;
  27.     end;
  28. end;
  29.  

Of course you'll have to define 2 ConstantLine series and set Chart1ConstantLine1.LineStyle to lsHorizontal and #2 to lsVertical. Also set the SeriesColor to something other than the chart background

keep the chart inactive until populate your lineseries with the stock price and time. This will draw the crosshairs all the way across the chart area. And only when you move the cursor off of that drawing area will the crosshairs go away.



kapibara

  • Hero Member
  • *****
  • Posts: 641
Re: Crosshair following Pointer
« Reply #5 on: February 14, 2025, 07:47:43 pm »
Thanks for the suggestion, chucky.
Lazarus trunk / fpc 3.2.2 / Kubuntu 24.04 - 64 bit

 

TinyPortal © 2005-2018