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

  1. {****************************************}
  2. {    TeeChart. TChart Component          }
  3. { Copyright (c) 1995-98 by David Berneda }
  4. {    All Rights Reserved                 }
  5. {****************************************}
  6. unit umovleg;
  7. {$P-}  { <-- only necessary in Delphi 1 }
  8.  
  9. interface
  10.  
  11. { Example of using GetLegendRect and GetLegendPos events.
  12.  
  13.   The Chart Legend is positioned to the Chart bottom, but
  14.   the Legend is drawn using the same style as when its
  15.   at Left or Right.
  16.  
  17.   This is different than setting the Chart1.Legend.Alignment property.
  18. }
  19. uses
  20.   WinProcs,WinTypes, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  21.   Series, TeEngine, ExtCtrls, TeeProcs, Chart, StdCtrls;
  22.  
  23. type
  24.   TFormMoveLegend = class(TForm)
  25.     Chart1: TChart;
  26.     Series1: TLineSeries;
  27.     Series2: TBarSeries;
  28.     Button1: TButton;
  29.     RadioGroup1: TRadioGroup;
  30.     ScrollBar1: TScrollBar;
  31.     Label1: TLabel;
  32.     ButtonMeta: TButton;
  33.     Button2: TButton;
  34.     SaveDialog1: TSaveDialog;
  35.     CheckBox1: TCheckBox;
  36.     Timer1: TTimer;
  37.     Memo1: TMemo;
  38.     procedure Chart1GetLegendRect(Sender: TCustomChart; var Rect: TRect);
  39.     procedure FormCreate(Sender: TObject);
  40.     procedure Chart1GetLegendPos(Sender: TCustomChart; Index: Longint;
  41.       var X, Y, XColor: Longint);
  42.     procedure Button1Click(Sender: TObject);
  43.     procedure RadioGroup1Click(Sender: TObject);
  44.     procedure ScrollBar1Change(Sender: TObject);
  45.     procedure Button2Click(Sender: TObject);
  46.     procedure ButtonMetaClick(Sender: TObject);
  47.     procedure CheckBox1Click(Sender: TObject);
  48.     procedure Timer1Timer(Sender: TObject);
  49.   private
  50.     { Private declarations }
  51.   public
  52.     { Public declarations }
  53.     Delta:Integer;
  54.   end;
  55.  
  56. implementation
  57.  
  58. {$R *.DFM}
  59.  
  60. Uses UMeta,
  61.      TeePrevi;  { for Chart Print Preview }
  62.  
  63. procedure TFormMoveLegend.Chart1GetLegendRect(Sender: TCustomChart;
  64.   var Rect: TRect);
  65. var tmp:Integer;
  66. begin
  67.   { calculate the Legend Width in pixels, and add 20 pixels
  68.     to allow space for Legend colors }
  69.   tmp:=(Rect.Right-Rect.Left)+20;
  70.  
  71.   { move Legend horizontally to the Chart horizontal center }
  72.   Rect.Left:= Sender.ChartBounds.Left+
  73.               Round(ScrollBar1.Position*(Sender.ChartWidth-tmp)/100.0)+(tmp div 2);
  74.   Rect.Right:=Rect.Left+tmp;
  75.  
  76.   { calculate Legend Height }
  77.   tmp:=(Rect.Bottom-Rect.Top);
  78.  
  79.   if RadioGroup1.ItemIndex=1 then
  80.      { move Legend vertically to the Chart bottom }
  81.      Rect.Top:=Sender.ChartBounds.Bottom-tmp-10
  82.   else
  83.      { move Legend vertically to the Chart Top }
  84.      Rect.Top:=Sender.ChartBounds.Top+10;
  85.  
  86.   Rect.Bottom:=Rect.Top+tmp;
  87. end;
  88.  
  89. procedure TFormMoveLegend.FormCreate(Sender: TObject);
  90. begin
  91.   Delta:=1;  { <-- used for animation }
  92.  
  93.   Chart1.MarginBottom:=20;             { make Chart smaller height }
  94.   Chart1.Legend.ResizeChart:=False;    { free Legend position }
  95.  
  96.   Chart1.Legend.ColorWidth:=40;        { make Legend colors bigger }
  97.  
  98.   { random data }
  99.   Series1.FillSampleValues(8);
  100.   Series2.FillSampleValues(8);
  101. end;
  102.  
  103. procedure TFormMoveLegend.Chart1GetLegendPos(Sender: TCustomChart; Index: Longint;
  104.   var X, Y, XColor: Longint);
  105. begin
  106.   { get the position for each item in the Legend... }
  107.   With Sender.Legend,RectLegend do
  108.      X:=Left+Round(ColorWidth*(Right-Left)/100.0);  { for each Legend text... }
  109. end;
  110.  
  111. procedure TFormMoveLegend.Button1Click(Sender: TObject);
  112. begin
  113.   Close;
  114. end;
  115.  
  116. procedure TFormMoveLegend.RadioGroup1Click(Sender: TObject);
  117. begin
  118.   { make margins small or big depending Top or Bottom position }
  119.   if RadioGroup1.ItemIndex=0 then
  120.   begin
  121.     Chart1.MarginTop:=20;
  122.     Chart1.MarginBottom:=5;
  123.   end
  124.   else
  125.   begin
  126.     Chart1.MarginTop:=5;
  127.     Chart1.MarginBottom:=20;
  128.   end;
  129.   Chart1.Repaint;
  130. end;
  131.  
  132. procedure TFormMoveLegend.ScrollBar1Change(Sender: TObject);
  133. begin
  134.   Chart1.Repaint;  { redraw... }
  135. end;
  136.  
  137. procedure TFormMoveLegend.Button2Click(Sender: TObject);
  138. begin
  139.   ChartPreview(Self,Chart1); { <-- show print preview... }
  140. end;
  141.  
  142. procedure TFormMoveLegend.ButtonMetaClick(Sender: TObject);
  143. var Meta:TMetafile;
  144. begin
  145.   { create a metafile and show it on another form... }
  146.   With TFormMeta.Create(Self) do
  147.   try
  148.     With Chart1.ClientRect do SetBounds(0,0,Right,Bottom);
  149.     Meta:=Chart1.TeeCreateMetafile(True,Chart1.ClientRect);
  150.     try
  151.       Image1.Picture.Assign(Meta);
  152.     finally
  153.       Meta.Free;
  154.     end;
  155.     ShowModal;
  156.   finally
  157.     Free;
  158.   end;
  159. end;
  160.  
  161. procedure TFormMoveLegend.CheckBox1Click(Sender: TObject);
  162. begin
  163.   Timer1.Enabled:=CheckBox1.Checked;
  164. end;
  165.  
  166. procedure TFormMoveLegend.Timer1Timer(Sender: TObject);
  167. begin
  168.   { move the scrollbar to move the legend position... }
  169.   With ScrollBar1 do
  170.   begin
  171.     Position:=Position+Delta;
  172.     if (Position=0) or (Position=100) then Delta:=-Delta;
  173.   end;
  174. end;
  175.  
  176. end.
  177.