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

  1. unit Uaxcent;
  2. {$I Teedefs.inc}
  3.  
  4. interface
  5.  
  6. { This example shows how to draw the Axis vertically centered.
  7.   It also shows drag and drop using standard VCL "TDragObject"
  8.   objects, and determining which part of a Chart is under
  9.   the mouse cursor }
  10.  
  11. uses
  12.   Wintypes, WinProcs, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  13.   Dialogs,  TeEngine, Series, ExtCtrls, TeeProcs, Chart, OHLChart, CandleCh,
  14.   StdCtrls;
  15.  
  16. type
  17.   TAxisCenterForm = class(TForm)
  18.     RadioGroup1: TRadioGroup;
  19.     Series1: TLineSeries;
  20.     Chart1: TChart;
  21.     Button1: TButton;
  22.     RadioGroup2: TRadioGroup;
  23.     CheckBox1: TCheckBox;
  24.     procedure FormCreate(Sender: TObject);
  25.     procedure RadioGroup1Click(Sender: TObject);
  26.     procedure Button1Click(Sender: TObject);
  27.     procedure RadioGroup2Click(Sender: TObject);
  28.     procedure CheckBox1Click(Sender: TObject);
  29.     procedure Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
  30.       Y: Integer);
  31.     procedure Chart1ClickAxis(Sender: TCustomChart; Axis: TChartAxis;
  32.       Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  33.     {$IFNDEF D1}
  34.     procedure Chart1StartDrag(Sender: TObject;
  35.       var DragObject: TDragObject);
  36.     {$ENDIF}
  37.     procedure Chart1DragOver(Sender, Source: TObject; X, Y: Integer;
  38.       State: TDragState; var Accept: Boolean);
  39.     procedure Chart1DragDrop(Sender, Source: TObject; X, Y: Integer);
  40.   private
  41.     { Private declarations }
  42.   public
  43.     { Public declarations }
  44.   end;
  45.  
  46. var
  47.   AxisCenterForm: TAxisCenterForm;
  48.  
  49. implementation
  50.  
  51. {$R *.DFM}
  52. Uses TeCanvas;
  53.  
  54. procedure TAxisCenterForm.FormCreate(Sender: TObject);
  55. var t:Integer;
  56. begin
  57.   { adjust scales... }
  58.   Chart1.Frame.Visible:=False;
  59.   Chart1.LeftAxis.SetMinMax(-100,100);
  60.  
  61.   { add sample data... }
  62.   Series1.Clear;
  63.   for t:=1 to 100 do Series1.Add(Random(200)-100.0,'',clTeeColor);
  64.  
  65.   {$IFNDEF D1}
  66.   Chart1.OnStartDrag:=Chart1StartDrag;
  67.   {$ENDIF}
  68. end;
  69.  
  70. { Select where to draw the HORIZONTAL axis }
  71. procedure TAxisCenterForm.RadioGroup1Click(Sender: TObject);
  72. begin
  73.   Case RadioGroup1.ItemIndex of
  74.     0:  Series1.HorizAxis:=aTopAxis; { top }
  75.     1: begin  { center }
  76.          Series1.HorizAxis :=aBottomAxis;
  77.          Series1.GetHorizAxis.PositionPercent:=50;
  78.        end;
  79.     2: begin  { bottom  }
  80.          Series1.HorizAxis :=aBottomAxis;
  81.          Series1.GetHorizAxis.PositionPercent:=0;
  82.        end;
  83.   end;
  84. end;
  85.  
  86. procedure TAxisCenterForm.Button1Click(Sender: TObject);
  87. begin
  88.   Close;
  89. end;
  90.  
  91. { Select where to draw the VERTICAL axis }
  92. procedure TAxisCenterForm.RadioGroup2Click(Sender: TObject);
  93. begin
  94.   Case RadioGroup2.ItemIndex of
  95.     0: Series1.VertAxis:=aLeftAxis; { left }
  96.     1: begin  { center }
  97.          Series1.VertAxis :=aRightAxis;
  98.          Series1.GetVertAxis.PositionPercent:=50;
  99.        end;
  100.     2: begin  { right  }
  101.          Series1.VertAxis :=aRightAxis;
  102.          Series1.GetVertAxis.PositionPercent:=0;
  103.        end;
  104.   end;
  105.  
  106. end;
  107.  
  108. procedure TAxisCenterForm.CheckBox1Click(Sender: TObject);
  109. begin
  110.   TeeDrawAxisBeforeSeries:=CheckBox1.Checked;
  111.   Chart1.Repaint;
  112. end;
  113.  
  114. procedure TAxisCenterForm.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
  115.   Y: Integer);
  116. var t          : Integer;
  117.     tmpChanged : Boolean;
  118. begin
  119.   { Determine if the mouse cursor is over
  120.     an axis... }
  121.   tmpChanged:=False;
  122.   for t:=0 to Chart1.AxesList.Count-1 do
  123.   begin
  124.     if TCustomChartAxis(Chart1.AxesList[t]).Clicked(X,Y) then
  125.     begin
  126.       { if mouse is over, then change the cursor ... }
  127.       Chart1.Cursor:=crTeeHand;
  128.       tmpChanged:=True;
  129.       break;
  130.     end;
  131.   end;
  132.   { if no axis are "clicked", then check the Legend... }
  133.   if not tmpChanged then
  134.      if Chart1.Legend.Clicked(X,Y)<>-1 then
  135.      begin
  136.        Chart1.Cursor:=crTeeHand;
  137.        tmpChanged:=True;
  138.      end;
  139.   { if the Legend is not "clicked", then check the Title... }
  140.   if not tmpChanged then
  141.      if Chart1.Title.Clicked(X,Y) then
  142.      begin
  143.        Chart1.Cursor:=crTeeHand;
  144.        tmpChanged:=True;
  145.      end;
  146.  
  147.   { if nothing is under the mouse, set the cursor to
  148.     default arrow... }
  149.   if not tmpChanged then Chart1.Cursor:=crDefault;
  150.   With Chart1 do OriginalCursor:=Cursor;
  151. end;
  152.  
  153. procedure TAxisCenterForm.Chart1ClickAxis(Sender: TCustomChart; Axis: TChartAxis;
  154.   Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  155. begin
  156. {  DraggedAxis:=Axis;}
  157. end;
  158.  
  159. {$IFNDEF D1}
  160. procedure TAxisCenterForm.Chart1StartDrag(Sender: TObject;
  161.   var DragObject: TDragObject);
  162. var Part:TChartClickedPart;
  163. begin
  164.   Chart1.CalcClickedPart(Chart1.GetCursorPos,Part);
  165.   DragObject:=TTeeDragObject.Create(Part);
  166. end;
  167. {$ENDIF}
  168.  
  169. procedure TAxisCenterForm.Chart1DragOver(Sender, Source: TObject; X, Y: Integer;
  170.   State: TDragState; var Accept: Boolean);
  171. begin
  172.   {$IFNDEF D1}
  173.   if Source is TTeeDragObject then
  174.   begin
  175.     Accept:=False;
  176.     With TTeeDragObject(Source).Part do
  177.     if Part=cpAxis then
  178.     begin
  179.       Accept:=PtInRect(Chart1.ChartRect,Point(X,Y));
  180.     end
  181.     else
  182.     if Part=cpLegend then
  183.        Accept:=PtInRect(Chart1.Legend.RectLegend,Point(X,Y))
  184.     else
  185.     if Part=cpTitle then
  186.        Accept:=PtInRect(Chart1.Title.TitleRect,Point(X,Y))
  187.     else
  188.     if Part=cpFoot then
  189.        Accept:=PtInRect(Chart1.Foot.TitleRect,Point(X,Y));
  190.     if Accept then Chart1DragDrop(Self,Source,X,Y);
  191.   end;
  192.   {$ENDIF}
  193. end;
  194.  
  195. procedure TAxisCenterForm.Chart1DragDrop(Sender, Source: TObject; X, Y: Integer);
  196. {$IFNDEF D1}
  197.  
  198. var tmp,tmp2:Integer;
  199.  
  200.     Procedure DropAxis;
  201.     begin
  202.       With TTeeDragObject(Source).Part do
  203.       if AAxis.Horizontal then
  204.       begin
  205.         With Chart1 do
  206.         begin
  207.           tmp:=(ChartRect.Bottom-ChartRect.Top);
  208.           tmp2:=(Y-ChartRect.Top);
  209.           AAxis.PositionPercent:=100-Round(100.0*tmp2/tmp);
  210.         end;
  211.       end
  212.       else
  213.         With Chart1 do
  214.         begin
  215.           tmp:=(ChartRect.Right-ChartRect.Left);
  216.           tmp2:=(X-ChartRect.Left);
  217.           AAxis.PositionPercent:=(Round(100.0*tmp2/tmp));
  218.         end;
  219.     end;
  220.  
  221.  
  222.     Procedure DropLegend;
  223.     Var tmpP,tmp2P:TLegendAlignment;
  224.     begin
  225.       tmp:=Abs(Chart1.Height-Y);
  226.       tmpP:=laTop;
  227.       if Abs(Chart1.Height-Y)<tmp then
  228.       begin
  229.         tmp:=Abs(Chart1.ChartRect.Bottom-Y);
  230.         tmpP:=laBottom;
  231.       end;
  232.       tmp2:=Abs(X-Chart1.ChartRect.Left);
  233.       tmp2P:=laLeft;
  234.       if Abs(Chart1.ChartRect.Right-X)<tmp2 then
  235.       begin
  236.          tmp2:=Abs(Chart1.ChartRect.Right-X);
  237.          tmp2P:=laRight;
  238.       end;
  239.  
  240.       if tmp2<tmp then Chart1.Legend.Alignment:=tmpP
  241.                   else Chart1.Legend.Alignment:=tmp2P;
  242.       if Chart1.Legend.Alignment=laRight then
  243.       begin
  244.         tmp:=(Chart1.Legend.RectLegend.Bottom-Chart1.Legend.RectLegend.Top);
  245.         tmp2:=Abs(Y-Chart1.Legend.RectLegend.Top);
  246.       end
  247.       else
  248.       begin
  249.         tmp:=(Chart1.Legend.RectLegend.Right-Chart1.Legend.RectLegend.Left);
  250.         tmp2:=Abs(X-Chart1.Legend.RectLegend.Left);
  251.       end;
  252. {       if tmp2>0 then
  253.          Chart1.Legend.TopPos:=Round(100.0*tmp2/tmp)
  254.       else
  255.          Chart1.Legend.TopPos:=Round(100.0*tmp2/tmp)}
  256.    end;
  257.  
  258.   {$ENDIF}
  259. begin
  260.   {$IFNDEF D1}
  261.   if Source is TTeeDragObject then
  262.   With TTeeDragObject(Source).Part do
  263.      if Part=cpAxis then DropAxis      else
  264.      if Part=cpLegend then DropLegend
  265.   {$ENDIF}
  266. end;
  267.  
  268. end.
  269.