home *** CD-ROM | disk | FTP | other *** search
- {****************************************}
- { TeeChart. TChart Component }
- { Copyright (c) 1995-98 by David Berneda }
- { All Rights Reserved }
- {****************************************}
- unit umovleg;
- {$P-} { <-- only necessary in Delphi 1 }
-
- interface
-
- { Example of using GetLegendRect and GetLegendPos events.
-
- The Chart Legend is positioned to the Chart bottom, but
- the Legend is drawn using the same style as when its
- at Left or Right.
-
- This is different than setting the Chart1.Legend.Alignment property.
- }
- uses
- WinProcs,WinTypes, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- Series, TeEngine, ExtCtrls, TeeProcs, Chart, StdCtrls;
-
- type
- TFormMoveLegend = class(TForm)
- Chart1: TChart;
- Series1: TLineSeries;
- Series2: TBarSeries;
- Button1: TButton;
- RadioGroup1: TRadioGroup;
- ScrollBar1: TScrollBar;
- Label1: TLabel;
- ButtonMeta: TButton;
- Button2: TButton;
- SaveDialog1: TSaveDialog;
- CheckBox1: TCheckBox;
- Timer1: TTimer;
- Memo1: TMemo;
- procedure Chart1GetLegendRect(Sender: TCustomChart; var Rect: TRect);
- procedure FormCreate(Sender: TObject);
- procedure Chart1GetLegendPos(Sender: TCustomChart; Index: Longint;
- var X, Y, XColor: Longint);
- procedure Button1Click(Sender: TObject);
- procedure RadioGroup1Click(Sender: TObject);
- procedure ScrollBar1Change(Sender: TObject);
- procedure Button2Click(Sender: TObject);
- procedure ButtonMetaClick(Sender: TObject);
- procedure CheckBox1Click(Sender: TObject);
- procedure Timer1Timer(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- Delta:Integer;
- end;
-
- implementation
-
- {$R *.DFM}
-
- Uses UMeta,
- TeePrevi; { for Chart Print Preview }
-
- procedure TFormMoveLegend.Chart1GetLegendRect(Sender: TCustomChart;
- var Rect: TRect);
- var tmp:Integer;
- begin
- { calculate the Legend Width in pixels, and add 20 pixels
- to allow space for Legend colors }
- tmp:=(Rect.Right-Rect.Left)+20;
-
- { move Legend horizontally to the Chart horizontal center }
- Rect.Left:= Sender.ChartBounds.Left+
- Round(ScrollBar1.Position*(Sender.ChartWidth-tmp)/100.0)+(tmp div 2);
- Rect.Right:=Rect.Left+tmp;
-
- { calculate Legend Height }
- tmp:=(Rect.Bottom-Rect.Top);
-
- if RadioGroup1.ItemIndex=1 then
- { move Legend vertically to the Chart bottom }
- Rect.Top:=Sender.ChartBounds.Bottom-tmp-10
- else
- { move Legend vertically to the Chart Top }
- Rect.Top:=Sender.ChartBounds.Top+10;
-
- Rect.Bottom:=Rect.Top+tmp;
- end;
-
- procedure TFormMoveLegend.FormCreate(Sender: TObject);
- begin
- Delta:=1; { <-- used for animation }
-
- Chart1.MarginBottom:=20; { make Chart smaller height }
- Chart1.Legend.ResizeChart:=False; { free Legend position }
-
- Chart1.Legend.ColorWidth:=40; { make Legend colors bigger }
-
- { random data }
- Series1.FillSampleValues(8);
- Series2.FillSampleValues(8);
- end;
-
- procedure TFormMoveLegend.Chart1GetLegendPos(Sender: TCustomChart; Index: Longint;
- var X, Y, XColor: Longint);
- begin
- { get the position for each item in the Legend... }
- With Sender.Legend,RectLegend do
- X:=Left+Round(ColorWidth*(Right-Left)/100.0); { for each Legend text... }
- end;
-
- procedure TFormMoveLegend.Button1Click(Sender: TObject);
- begin
- Close;
- end;
-
- procedure TFormMoveLegend.RadioGroup1Click(Sender: TObject);
- begin
- { make margins small or big depending Top or Bottom position }
- if RadioGroup1.ItemIndex=0 then
- begin
- Chart1.MarginTop:=20;
- Chart1.MarginBottom:=5;
- end
- else
- begin
- Chart1.MarginTop:=5;
- Chart1.MarginBottom:=20;
- end;
- Chart1.Repaint;
- end;
-
- procedure TFormMoveLegend.ScrollBar1Change(Sender: TObject);
- begin
- Chart1.Repaint; { redraw... }
- end;
-
- procedure TFormMoveLegend.Button2Click(Sender: TObject);
- begin
- ChartPreview(Self,Chart1); { <-- show print preview... }
- end;
-
- procedure TFormMoveLegend.ButtonMetaClick(Sender: TObject);
- var Meta:TMetafile;
- begin
- { create a metafile and show it on another form... }
- With TFormMeta.Create(Self) do
- try
- With Chart1.ClientRect do SetBounds(0,0,Right,Bottom);
- Meta:=Chart1.TeeCreateMetafile(True,Chart1.ClientRect);
- try
- Image1.Picture.Assign(Meta);
- finally
- Meta.Free;
- end;
- ShowModal;
- finally
- Free;
- end;
- end;
-
- procedure TFormMoveLegend.CheckBox1Click(Sender: TObject);
- begin
- Timer1.Enabled:=CheckBox1.Checked;
- end;
-
- procedure TFormMoveLegend.Timer1Timer(Sender: TObject);
- begin
- { move the scrollbar to move the legend position... }
- With ScrollBar1 do
- begin
- Position:=Position+Delta;
- if (Position=0) or (Position=100) then Delta:=-Delta;
- end;
- end;
-
- end.
-