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

  1. {**********************************************}
  2. {   TeeChart Pro VCL 4.0                       }
  3. {   Using custom Arrays Demo                   } 
  4. {   Copyright (c) 1995-98 by David Berneda     }
  5. {**********************************************}
  6. unit UArray;
  7. {$I teedefs.inc}
  8.  
  9. { This example shows two things:
  10.  
  11.   1) How to add points to a Fast-Line series and
  12.      make the series to draw every last added point.
  13.  
  14.   2) How to use "custom" arrays with a Fast-Line series.
  15.  
  16.   Using "2 (arrays)" is much faster and uses less memory than
  17.   "1".
  18.  
  19.   This demo shows how to use the Fast-Line "AutoRepaint" property.
  20.   When "AutoRepaint" is False, new points added to a Fast-Line series
  21.   will be displayed one by one, instead of redrawing the whole chart.
  22.  
  23.   Other properties that might increase speed are:
  24.  
  25.   - Setting the Series "XValues.Order" property to "loNone",
  26.     to disable internal point sorting.
  27.   - Setting the Chart "AutoRepaint" property to False, and then to True
  28.     when we have finished adding points to Series.  This is to prevent
  29.     calling the Delphi's and Windows's "Invalidate" method, which might
  30.     be slow to execute.
  31.  
  32. }
  33. interface
  34.  
  35. uses
  36.   WinProcs,WinTypes, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  37.   TeEngine, Series, StdCtrls, ExtCtrls, TeeProcs, Chart, TeeComma;
  38.  
  39. type
  40.   TArrayForm = class(TForm)
  41.     Chart1: TChart;
  42.     Panel1: TPanel;
  43.     Memo1: TMemo;
  44.     Button1: TButton;
  45.     UseArrays: TCheckBox;
  46.     Button2: TButton;
  47.     LabelTime: TLabel;
  48.     Series1: TFastLineSeries;
  49.     TeeCommander1: TTeeCommander;
  50.     Label1: TLabel;
  51.     CheckBox2: TCheckBox;
  52.     procedure Button1Click(Sender: TObject);
  53.     procedure Button2Click(Sender: TObject);
  54.     procedure FormCreate(Sender: TObject);
  55.     procedure FormDestroy(Sender: TObject);
  56.     procedure CheckBox2Click(Sender: TObject);
  57.   private
  58.     { Private declarations }
  59.   public
  60.     { Public declarations }
  61.     Procedure DemoWithArrays;
  62.     Procedure DemoWithoutArrays;
  63.   end;
  64.  
  65. implementation
  66.  
  67. {$R *.DFM}
  68.  
  69. { This example uses a custom Array to store point values }
  70.  
  71. Const NumPoints={$IFDEF D1}8000{$ELSE}100000{$ENDIF};
  72.  
  73. type
  74.     TMyValues=Array[0..NumPoints-1] of Double;
  75.     PMyValues=^TMyValues;
  76.  
  77. Var MyArrayX : PMyValues;
  78.     MyArrayY : PMyValues;
  79.  
  80. { The following object connects the Array to the Series }
  81. Type TMyList=class(TChartValueList)
  82.      private
  83.      protected
  84.        Function GetValue(ValueIndex:Longint):Double; override;
  85.        Procedure SetValue(ValueIndex:Longint; Const AValue:Double); override;
  86.        Procedure ClearValues; override;
  87.        Function AddChartValue(Const Value:TChartValue):Longint; override;
  88.        Procedure InsertChartValue(ValueIndex:Longint; Const Value:TChartValue); override;
  89.      public
  90.        MyArray:PMyValues;
  91.        MyCount:Integer;
  92.        Constructor Create(AOwner:TChartSeries; Const AName:String); override;
  93.        Function Count:Longint; override;
  94.        Procedure Delete(ValueIndex:Longint); override;
  95.        Procedure Scroll; override;
  96.      end;
  97.  
  98. { reset number of points to zero }
  99. Constructor TMyList.Create(AOwner:TChartSeries; Const AName:String);
  100. begin
  101.   inherited Create(AOwner,AName);
  102.   MyCount:=0;
  103. end;
  104.  
  105. { return a value from the Array }
  106. Function TMyList.GetValue(ValueIndex:Longint):Double;
  107. begin
  108.   result:=MyArray^[ValueIndex];
  109. end;
  110.  
  111. { set an Array value }
  112. Procedure TMyList.SetValue(ValueIndex:Longint; Const AValue:Double);
  113. begin
  114.   MyArray^[ValueIndex]:=AValue;
  115. end;
  116.  
  117. { clear all Array values to zero }
  118. Procedure TMyList.ClearValues;
  119. var t:Longint;
  120. begin
  121.   if Assigned(MyArray) then
  122.      for t:=0 to Count-1 do MyArray^[t]:=0;
  123. end;
  124.  
  125. { If we use Series1.AddXY or Add methods, fill the Array with the new value }
  126. Function TMyList.AddChartValue(Const Value:TChartValue):Longint;
  127. begin
  128.   MyArray^[Count]:=Value;
  129.   Inc(MyCount);
  130.   result:=MyCount-1;
  131. end;
  132.  
  133. { Insert a new value in the Array at a specific "ValueIndex" position }
  134. Procedure TMyList.InsertChartValue(ValueIndex:Longint; Const Value:TChartValue);
  135. var t:Longint;
  136. begin
  137.   { scroll all values to make room for the new value }
  138.   for t:=Count-1 downto ValueIndex do MyArray^[t+1]:=MyArray^[t];
  139.   MyArray^[ValueIndex]:=Value;
  140.   Inc(MyCount);
  141. end;
  142.  
  143. { return the number of points in the Array }
  144. Function TMyList.Count:Longint;
  145. begin
  146.   result:=MyCount;
  147. end;
  148.  
  149. { delete an Array value }
  150. Procedure TMyList.Delete(ValueIndex:Longint);
  151. var t:Longint;
  152. begin
  153.   for t:=ValueIndex+1 to Count-1 do MyArray^[t-1]:=MyArray^[t];
  154.   Dec(MyCount);
  155. end;
  156.  
  157. { circular scrolling of Array points }
  158. Procedure TMyList.Scroll;
  159. var t:Longint;
  160.     tmp:Double;
  161. begin
  162.   if Count>0 then
  163.   begin
  164.     tmp:=MyArray^[0];
  165.     for t:=1 to Count-1 do MyArray^[t-1]:=MyArray^[t];
  166.     MyArray^[Count-1]:=tmp;
  167.   end;
  168. end;
  169.  
  170. { Demo Form }
  171. procedure TArrayForm.Button1Click(Sender: TObject);
  172. begin
  173.   Close;
  174. end;
  175.  
  176.  
  177. Procedure TArrayForm.DemoWithArrays;
  178. Var t:Longint;
  179. begin
  180.   { fill the arrays with random data... }
  181.   MyArrayY^[0]:=1000;
  182.   MyArrayX^[0]:=0;
  183.   for t:=1 to NumPoints-1 do
  184.   begin
  185.     MyArrayX^[t]:=t;
  186.     MyArrayY^[t]:=MyArrayY^[t-1]+Random(51)-25;
  187.   end;
  188.  
  189.   { tell the Series to use our private arrays... }
  190.   With Series1 do
  191.   begin
  192.     { create a new "custom" list for X values }
  193.     ReplaceList(XValues,TMyList.Create(Series1,''));
  194.     With TMyList(XValues) do
  195.     begin
  196.       Order:=loAscending;
  197.       MyCount:=NumPoints;  { <-- set number of points }
  198.       MyArray:=MyArrayX;   { <-- set our X array }
  199.     end;
  200.     { create a new "custom" list for Y values }
  201.     ReplaceList(YValues,TMyList.Create(Series1,''));
  202.     With TMyList(YValues) do
  203.     begin
  204.       MyCount:=NumPoints;  { <-- set number of points }
  205.       MyArray:=MyArrayY;   { <-- set our Y array }
  206.     end;
  207.     Repaint; { Show the Array points !  }
  208.   end;
  209. end;
  210.  
  211. Procedure TArrayForm.DemoWithoutArrays;
  212. Var t:Longint;
  213. begin
  214.   With Series1 do
  215.   begin
  216.     { in case the Series is still using our "custom arrays",
  217.       reset again to the default objects }
  218.     ReplaceList(XValues,TChartValueList.Create(Series1,''));
  219.     ReplaceList(YValues,TChartValueList.Create(Series1,''));
  220.  
  221.     { disable redrawing the Chart for each new added point... }
  222.     AutoRepaint:=False;
  223.     { remove all values in the series... }
  224.     Clear;
  225.     Application.ProcessMessages;
  226.     { disable X ordering }
  227.     XValues.Order:=loNone;
  228.  
  229.     { Disable Chart AutoRepaint to improve speed a little bit }
  230.     ParentChart.AutoRepaint:=False;
  231.  
  232.     { add random values.... }
  233.     Add(1000,'',clTeeColor);
  234.     for t:=1 to NumPoints do Add(YValues.Last+Random(51)-25.0,'',clTeeColor);
  235.  
  236.     { Restore Chart AutoRepaint }
  237.     ParentChart.AutoRepaint:=True;
  238.   end;
  239. end;
  240.  
  241. procedure TArrayForm.Button2Click(Sender: TObject);
  242. Var StartTime,
  243.     EndTime:Longint;
  244. begin
  245.   StartTime:=GetTickCount;  { start Time test }
  246.  
  247.   { do the demo ! }
  248.   if UseArrays.Checked then DemoWithArrays
  249.                        else DemoWithoutArrays;
  250.   { finish Time test }
  251.   EndTime:=GetTickCount;
  252.   { show the elapsed time }
  253.   LabelTime.Caption:=IntToStr(EndTime-StartTime);
  254. end;
  255.  
  256. procedure TArrayForm.FormCreate(Sender: TObject);
  257. begin
  258.   { Change the Axis scales to suitable values for this example...
  259.     This is optional but recommended to speed-up display speed.
  260.   }
  261.   Chart1.LeftAxis.SetMinMax(-10000,10000);
  262.   Chart1.BottomAxis.SetMinMax(0,NumPoints+1000);
  263.  
  264.   { create our private Arrays for this example... }
  265.   New(MyArrayX);
  266.   New(MyArrayY);
  267. end;
  268.  
  269. procedure TArrayForm.FormDestroy(Sender: TObject);
  270. begin
  271.   { In case the Series still has our "custom" arrays... }
  272.   if Series1.XValues is TMyList then
  273.      TMyList(Series1.XValues).MyArray:=nil;
  274.   if Series1.YValues is TMyList then
  275.      TMyList(Series1.YValues).MyArray:=nil;
  276.  
  277.   { Deallocate memory and arrays }
  278.   Dispose(MyArrayX);
  279.   Dispose(MyArrayY);
  280. end;
  281.  
  282. procedure TArrayForm.CheckBox2Click(Sender: TObject);
  283. begin
  284.   Chart1.BufferedDisplay:=CheckBox2.Checked;
  285. end;
  286.  
  287. end.
  288.