home *** CD-ROM | disk | FTP | other *** search
/ Delphi 5 for Professionals / DELPHI5.iso / AddOns / Components / TEECHART / Delphi1_And_Delphi2 / EXAMPLES / EXTENDED / UNEAR.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-10-24  |  2.5 KB  |  111 lines

  1. unit unear;
  2.  
  3. interface
  4.  
  5. { This example includes a function to calculate the nearest point
  6.   in a Series to the mouse pointer.
  7. }
  8. uses
  9.   Wintypes,WinProcs, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  10.   ExtCtrls, TeeProcs, TeEngine, Chart, Series, StdCtrls;
  11.  
  12. type
  13.   TClickNearForm = class(TForm)
  14.     Chart1: TChart;
  15.     Series1: TLineSeries;
  16.     LabelX: TLabel;
  17.     LabelY: TLabel;
  18.     LabelNear: TLabel;
  19.     Button1: TButton;
  20.     Label4: TLabel;
  21.     Label5: TLabel;
  22.     Label6: TLabel;
  23.     procedure FormCreate(Sender: TObject);
  24.     procedure Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
  25.       Y: Integer);
  26.     procedure Button1Click(Sender: TObject);
  27.     procedure Chart1AfterDraw(Sender: TObject);
  28.   private
  29.     { Private declarations }
  30.   public
  31.     { Public declarations }
  32.     Mouse:TPoint;
  33.     NearestPoint:Integer;
  34.   end;
  35.  
  36. var
  37.   ClickNearForm: TClickNearForm;
  38.  
  39. implementation
  40.  
  41. {$R *.DFM}
  42.  
  43. Function GetNearestPoint( ASeries:TChartSeries; Mouse:TPoint):Integer;
  44. var t,Dif,Dist,x,y :Integer;
  45. begin
  46.   result:=-1;
  47.   Dif:=10000;
  48.   for t:=0 to ASeries.Count-1 do   { <-- traverse all points in a Series... }
  49.   begin
  50.     { calculate each point xy coordinates relative to mouse }
  51.     x:=Abs( Mouse.X-ASeries.CalcXPos(t) );
  52.     y:=Abs( Mouse.Y-ASeries.CalcYPos(t) );
  53.  
  54.     { calculate distance in pixels... }
  55.     Dist:=Round(sqrt(1.0*x*x+1.0*y*y));
  56.  
  57.     { store if distance is lower... }
  58.     if Dist<Dif then
  59.     begin
  60.       Dif:=Dist;
  61.       result:=t;  { <-- set this point to be the nearest... }
  62.     end;
  63.   end;
  64. end;
  65.  
  66. procedure TClickNearForm.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
  67.   Y: Integer);
  68. begin
  69.   Mouse.x:=x;
  70.   Mouse.y:=y;
  71.   LabelX.Caption:=IntToStr(Mouse.x);
  72.   LabelY.Caption:=IntToStr(Mouse.y);
  73.   NearestPoint:=GetNearestPoint(Series1,Mouse);
  74.   LabelNear.Caption:=IntToStr( NearestPoint );
  75.   Chart1.Repaint;
  76. end;
  77.  
  78. procedure TClickNearForm.FormCreate(Sender: TObject);
  79. begin
  80.   Series1.FillSampleValues(8);
  81. end;
  82.  
  83. procedure TClickNearForm.Button1Click(Sender: TObject);
  84. begin
  85.   Close;
  86. end;
  87.  
  88. procedure TClickNearForm.Chart1AfterDraw(Sender: TObject);
  89. var x,y:Integer;
  90. begin
  91.   if NearestPoint<>-1 then
  92.   With Chart1.Canvas do
  93.   begin
  94.     Pen.Style:=psDot;
  95.     Pen.Color:=clWhite;
  96.     Pen.Width:=1;
  97.  
  98.     x:=Series1.CalcXPos(NearestPoint);
  99.     y:=Series1.CalcYPos(NearestPoint);
  100.  
  101.     Brush.Style:=bsClear;
  102.     Ellipse(x-20,y-20,x+20,y+20);
  103.  
  104.     Pen.Style:=psSolid;
  105.     MoveTo(Mouse.X,Mouse.Y);
  106.     LineTo(x,y);
  107.   end;
  108. end;
  109.  
  110. end.
  111.