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

  1. {*********************************************}
  2. { TeeChart Delphi Component Library           }
  3. { Basic Series Types Demo                     }
  4. { Copyright (c) 1995-1996 by David Berneda    }
  5. { All rights reserved                         }
  6. {*********************************************}
  7. unit Basic;
  8.  
  9. interface
  10.  
  11. uses
  12.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  13.   Forms, Dialogs, Chart, Series, ExtCtrls, StdCtrls, Bubble,
  14.   BubbleCh, Teengine, Buttons, teeprocs;
  15.  
  16. type
  17.   TBasicForm = class(TForm)
  18.     Chart1: TChart;
  19.     Panel1: TPanel;
  20.     CheckBox1: TCheckBox;
  21.     Timer1: TTimer;
  22.     BarSeries1: TBarSeries;
  23.     South: TAreaSeries;
  24.     Speaking: TPointSeries;
  25.     Panel2: TPanel;
  26.     Chart2: TChart;
  27.     Label1: TLabel;
  28.     Chart3: TChart;
  29.     Chart4: TChart;
  30.     North: TAreaSeries;
  31.     Writing: TPointSeries;
  32.     Reading: TPointSeries;
  33.     BitBtn1: TBitBtn;
  34.     RadioGroup1: TRadioGroup;
  35.     BitBtn3: TBitBtn;
  36.     CheckBox2: TCheckBox;
  37.     SaveDialog1: TSaveDialog;
  38.     BitBtn2: TBitBtn;
  39.     Winter: TLineSeries;
  40.     Summer: TLineSeries;
  41.     procedure FormCreate(Sender: TObject);
  42.     procedure CheckBox1Click(Sender: TObject);
  43.     procedure Timer1Timer(Sender: TObject);
  44.     procedure FormResize(Sender: TObject);
  45.     procedure FormShow(Sender: TObject);
  46.     procedure BitBtn1Click(Sender: TObject);
  47.     procedure CheckBox2Click(Sender: TObject);
  48.     procedure BitBtn2Click(Sender: TObject);
  49.   private
  50.     { Private declarations }
  51.   public
  52.     { Public declarations }
  53.   end;
  54.  
  55. implementation
  56.  
  57. {$R *.DFM}
  58.  
  59. Uses Printers; 
  60.  
  61. procedure TBasicForm.FormCreate(Sender: TObject);
  62. var t:Longint;
  63. begin
  64.   Summer.FillSampleValues(20); { <-- Some random points }
  65.   Winter.FillSampleValues(20); { <-- Some random points }
  66.  
  67.   { Will need a Bar Series with special random values,
  68.     so we dont call the standard FillSampleValues method }
  69.   With BarSeries1 do
  70.   for t:=1 to 12 do Add(Random(1000),ShortMonthNames[t],GetDefaultColor(t));
  71.  
  72.   South.FillSampleValues(20); { <-- Some random points }
  73.   North.FillSampleValues(20); { <-- Some random points }
  74.  
  75.   Speaking.FillSampleValues(20); { <-- Some random points }
  76.   Reading.FillSampleValues(20); { <-- Some random points }
  77.   Writing.FillSampleValues(20); { <-- Some random points }
  78.  
  79. {  Force to resize the four charts }
  80.   FormResize(Self);
  81. end;
  82.  
  83. procedure TBasicForm.CheckBox1Click(Sender: TObject);
  84. begin
  85.   Timer1.Enabled:=CheckBox1.Checked;
  86. end;
  87.  
  88. procedure TBasicForm.Timer1Timer(Sender: TObject);
  89.  
  90.   Procedure Animate(Series:TChartSeries);
  91.   Begin
  92.     With Series do
  93.     Begin
  94.       Delete(0); { <-- remove the first point }
  95.       { Add a new random point }
  96.       AddXY( XValues.Last+1,
  97.              YValues.Last+(Random(ChartSamplesMax)-(ChartSamplesMax/2)),
  98.              '',clTeeColor);
  99.     end;
  100.   end;
  101.  
  102. Var tmpLabel:String;
  103.     tmpColor:TColor;
  104. begin
  105.   Animate(Summer);
  106.   Animate(Winter);
  107.   { BarSeries1 has special treatment to animate }
  108.   With BarSeries1 do
  109.   Begin
  110.     tmpLabel:=XLabel[0];
  111.     tmpColor:=ValueColor[0];
  112.     Delete(0);
  113.     AddXY(XValues.Last+1,Random(1000),tmpLabel,tmpColor);
  114.  
  115.     { Change Bar Style randomly... }
  116.     if Random(10)<1 then
  117.        BarStyle:=TBarStyle(Random(1+Ord(High(TBarStyle))));
  118.   end;
  119.   Animate(South);
  120.   Animate(North);
  121.   Animate(Speaking);
  122.   Animate(Reading);
  123.   Animate(Writing);
  124.  
  125.   { Change Pointer Style randomly... }
  126.   if Random(10)<1 then
  127.   With Speaking do
  128.    Pointer.Style:=TSeriesPointerStyle(Random(1+Ord(High(TSeriesPointerStyle))));
  129. end;
  130.  
  131. { Realign the four charts }
  132. procedure TBasicForm.FormResize(Sender: TObject);
  133. Var w,h:Longint;
  134. begin
  135.   { Top and bottom Panel positioning }
  136.   w:=ClientWidth-4-Panel1.Width;
  137.   Panel2.Width:=w-4;
  138.   Panel1.Height:=ClientHeight-Panel2.Height-4;
  139.   h:=Panel1.Height;
  140.   { Top Left Chart }
  141.   Chart1.SetBounds(Panel1.Width,Panel2.Height,w div 2,h div 2);
  142.   { Bottom Left Chart }
  143.   Chart2.SetBounds(Panel1.Width,Panel2.Height+Chart1.Height,w div 2,h div 2);
  144.   { Top Right Chart }
  145.   Chart3.SetBounds(Panel1.Width+Chart1.Width+1,Chart1.Top,w div 2,h div 2);
  146.   { Bottom Right Chart }
  147.   Chart4.SetBounds(Chart3.Left,Chart2.Top,w div 2,h div 2);
  148. end;
  149.  
  150. procedure TBasicForm.FormShow(Sender: TObject);
  151. begin
  152.   FormResize(Self); { <-- to align charts }
  153. end;
  154.  
  155. procedure TBasicForm.BitBtn1Click(Sender: TObject);
  156. Var tmpH,tmpW,tmpWMargin,tmpHMargin:Longint; { margins }
  157.     OldOrientation:TPrinterOrientation;
  158. begin
  159.   Screen.Cursor := crHourGlass;
  160.   OldOrientation:=Printer.Orientation;  { <-- save paper orientation }
  161.   Printer.Orientation:=poLandscape; { <-- Force Horizontal paper }
  162.   try
  163.     Printer.BeginDoc;       { <-- start printer job }
  164.     try
  165.       Printer.Title:='TeeChart Printing Demo';
  166.  
  167.       Case RadioGroup1.ItemIndex of
  168.         0: Begin { screen proportional }
  169.              Chart1.PrintResolution:= 0;
  170.              Chart2.PrintResolution:= 0;
  171.              Chart3.PrintResolution:= 0;
  172.              Chart4.PrintResolution:= 0;
  173.            End;
  174.         1: Begin { thin lines and small fonts }
  175.              Chart1.PrintResolution:= -100;
  176.              Chart2.PrintResolution:= -100;
  177.              Chart3.PrintResolution:= -100;
  178.              Chart4.PrintResolution:= -100;
  179.            End;
  180.       end;
  181.  
  182.       { Print the four charts, each one at a different paper position }
  183.  
  184.       { CALCULATE HORIZONTAL MARGIN }
  185.       tmpW:=Printer.PageWidth;
  186.       tmpWMargin:=Round(5.0*tmpW/100.0); { <-- 5% margins }
  187.       tmpW:=tmpW-2*tmpWMargin;  { <-- left and right margins }
  188.       tmpW:=tmpW div 2; { half height for left and right charts }
  189.  
  190.       { CALCULATE VERTICAL MARGIN }
  191.       tmpH:=Printer.PageHeight;
  192.       tmpHMargin:=Round(5.0*tmpH/100.0);  { <-- 5% margins }
  193.       tmpH:=tmpH-2*tmpHMargin;  { <-- bottom and top margins }
  194.       tmpH:=tmpH div 2; { half height for top and bottom charts }
  195.  
  196.       { left / top chart }
  197.       Chart1.PrintPartial( Rect( tmpWMargin,tmpHMargin,
  198.                                  tmpWMargin+tmpW,tmpHMargin+tmpH ) );
  199.  
  200.       { right / top chart }
  201.       Chart3.PrintPartial( Rect( tmpWMargin+tmpW,tmpHMargin,
  202.                                  tmpWMargin+2*tmpW,tmpHMargin+tmpH ) );
  203.  
  204.       { left / bottom chart }
  205.       Chart2.PrintPartial( Rect( tmpWMargin,tmpHMargin+tmpH,
  206.                                  tmpWMargin+tmpW,tmpHMargin+2*tmpH ) );
  207.  
  208.       { right / bottom chart }
  209.       Chart4.PrintPartial( Rect( tmpWMargin+tmpW,tmpHMargin+tmpH,
  210.                                  tmpWMargin+2*tmpW,tmpHMargin+2*tmpH ) );
  211.  
  212.       Printer.EndDoc; { <-- end job and print !! }
  213.     except
  214.       on Exception do  { just in case an error happens... }
  215.       Begin
  216.         Printer.Abort;
  217.         Printer.EndDoc;
  218.         Raise;
  219.       end;
  220.     end;
  221.   finally
  222.     Printer.Orientation:=OldOrientation;  { <-- restore paper orientation }
  223.     Screen.Cursor:=crDefault; { <-- restore cursor }
  224.   end;
  225. end;
  226.  
  227. procedure TBasicForm.CheckBox2Click(Sender: TObject);
  228. begin
  229.   Chart1.View3D:=CheckBox2.Checked;
  230.   Chart2.View3D:=CheckBox2.Checked;
  231.   Chart3.View3D:=CheckBox2.Checked;
  232.   Chart4.View3D:=CheckBox2.Checked;
  233. end;
  234.  
  235. procedure TBasicForm.BitBtn2Click(Sender: TObject);
  236. var tmpH,tmpW:Longint;
  237. begin
  238.  { This code creates and stores a new BITMAP file
  239.    which contains the FOUR charts.
  240.    Asks previously the user the BMP filename.
  241.  }
  242.   with SaveDialog1 do
  243.   begin
  244.     if Execute then
  245.     With TBitmap.Create do
  246.     try
  247.       { calculate bitmap size (2x2) }
  248.       tmpW:=Chart1.Width;
  249.       tmpH:=Chart1.Height;
  250.       Width := 2*tmpW;
  251.       Height:= 2*tmpH;
  252.       { draw chart 1 }
  253.       Chart1.BufferedDisplay:=False;
  254.       Chart1.Draw(Canvas,Rect(0,0,tmpW,tmpH));
  255.       Chart1.BufferedDisplay:=True;
  256.  
  257.       { draw chart 2 }
  258.       Chart2.BufferedDisplay:=False;
  259.       Chart2.Draw(Canvas,Rect(0,tmpH+1,tmpW,2*tmpH));
  260.       Chart2.BufferedDisplay:=True;
  261.  
  262.       { draw chart 3 }
  263.       Chart3.BufferedDisplay:=False;
  264.       Chart3.Draw(Canvas,Rect(tmpW+1,0,2*tmpW,tmpH));
  265.       Chart3.BufferedDisplay:=True;
  266.  
  267.       { draw chart 4 }
  268.       Chart4.BufferedDisplay:=False;
  269.       Chart4.Draw(Canvas,Rect(tmpW+1,tmpH+1,2*tmpW,2*tmpH));
  270.       Chart4.BufferedDisplay:=True;
  271.       SaveToFile(FileName);
  272.     finally
  273.       Free;
  274.     end;
  275.   end;
  276. end;
  277.  
  278. end.
  279.