home *** CD-ROM | disk | FTP | other *** search
- {**********************************************}
- { TeeChart Pro VCL 4.0 }
- { Using custom Arrays Demo }
- { Copyright (c) 1995-98 by David Berneda }
- {**********************************************}
- unit UArray;
- {$I teedefs.inc}
-
- { This example shows two things:
-
- 1) How to add points to a Fast-Line series and
- make the series to draw every last added point.
-
- 2) How to use "custom" arrays with a Fast-Line series.
-
- Using "2 (arrays)" is much faster and uses less memory than
- "1".
-
- This demo shows how to use the Fast-Line "AutoRepaint" property.
- When "AutoRepaint" is False, new points added to a Fast-Line series
- will be displayed one by one, instead of redrawing the whole chart.
-
- Other properties that might increase speed are:
-
- - Setting the Series "XValues.Order" property to "loNone",
- to disable internal point sorting.
- - Setting the Chart "AutoRepaint" property to False, and then to True
- when we have finished adding points to Series. This is to prevent
- calling the Delphi's and Windows's "Invalidate" method, which might
- be slow to execute.
-
- }
- interface
-
- uses
- WinProcs,WinTypes, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- TeEngine, Series, StdCtrls, ExtCtrls, TeeProcs, Chart, TeeComma;
-
- type
- TArrayForm = class(TForm)
- Chart1: TChart;
- Panel1: TPanel;
- Memo1: TMemo;
- Button1: TButton;
- UseArrays: TCheckBox;
- Button2: TButton;
- LabelTime: TLabel;
- Series1: TFastLineSeries;
- TeeCommander1: TTeeCommander;
- Label1: TLabel;
- CheckBox2: TCheckBox;
- procedure Button1Click(Sender: TObject);
- procedure Button2Click(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure FormDestroy(Sender: TObject);
- procedure CheckBox2Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- Procedure DemoWithArrays;
- Procedure DemoWithoutArrays;
- end;
-
- implementation
-
- {$R *.DFM}
-
- { This example uses a custom Array to store point values }
-
- Const NumPoints={$IFDEF D1}8000{$ELSE}100000{$ENDIF};
-
- type
- TMyValues=Array[0..NumPoints-1] of Double;
- PMyValues=^TMyValues;
-
- Var MyArrayX : PMyValues;
- MyArrayY : PMyValues;
-
- { The following object connects the Array to the Series }
- Type TMyList=class(TChartValueList)
- private
- protected
- Function GetValue(ValueIndex:Longint):Double; override;
- Procedure SetValue(ValueIndex:Longint; Const AValue:Double); override;
- Procedure ClearValues; override;
- Function AddChartValue(Const Value:TChartValue):Longint; override;
- Procedure InsertChartValue(ValueIndex:Longint; Const Value:TChartValue); override;
- public
- MyArray:PMyValues;
- MyCount:Integer;
- Constructor Create(AOwner:TChartSeries; Const AName:String); override;
- Function Count:Longint; override;
- Procedure Delete(ValueIndex:Longint); override;
- Procedure Scroll; override;
- end;
-
- { reset number of points to zero }
- Constructor TMyList.Create(AOwner:TChartSeries; Const AName:String);
- begin
- inherited Create(AOwner,AName);
- MyCount:=0;
- end;
-
- { return a value from the Array }
- Function TMyList.GetValue(ValueIndex:Longint):Double;
- begin
- result:=MyArray^[ValueIndex];
- end;
-
- { set an Array value }
- Procedure TMyList.SetValue(ValueIndex:Longint; Const AValue:Double);
- begin
- MyArray^[ValueIndex]:=AValue;
- end;
-
- { clear all Array values to zero }
- Procedure TMyList.ClearValues;
- var t:Longint;
- begin
- if Assigned(MyArray) then
- for t:=0 to Count-1 do MyArray^[t]:=0;
- end;
-
- { If we use Series1.AddXY or Add methods, fill the Array with the new value }
- Function TMyList.AddChartValue(Const Value:TChartValue):Longint;
- begin
- MyArray^[Count]:=Value;
- Inc(MyCount);
- result:=MyCount-1;
- end;
-
- { Insert a new value in the Array at a specific "ValueIndex" position }
- Procedure TMyList.InsertChartValue(ValueIndex:Longint; Const Value:TChartValue);
- var t:Longint;
- begin
- { scroll all values to make room for the new value }
- for t:=Count-1 downto ValueIndex do MyArray^[t+1]:=MyArray^[t];
- MyArray^[ValueIndex]:=Value;
- Inc(MyCount);
- end;
-
- { return the number of points in the Array }
- Function TMyList.Count:Longint;
- begin
- result:=MyCount;
- end;
-
- { delete an Array value }
- Procedure TMyList.Delete(ValueIndex:Longint);
- var t:Longint;
- begin
- for t:=ValueIndex+1 to Count-1 do MyArray^[t-1]:=MyArray^[t];
- Dec(MyCount);
- end;
-
- { circular scrolling of Array points }
- Procedure TMyList.Scroll;
- var t:Longint;
- tmp:Double;
- begin
- if Count>0 then
- begin
- tmp:=MyArray^[0];
- for t:=1 to Count-1 do MyArray^[t-1]:=MyArray^[t];
- MyArray^[Count-1]:=tmp;
- end;
- end;
-
- { Demo Form }
- procedure TArrayForm.Button1Click(Sender: TObject);
- begin
- Close;
- end;
-
-
- Procedure TArrayForm.DemoWithArrays;
- Var t:Longint;
- begin
- { fill the arrays with random data... }
- MyArrayY^[0]:=1000;
- MyArrayX^[0]:=0;
- for t:=1 to NumPoints-1 do
- begin
- MyArrayX^[t]:=t;
- MyArrayY^[t]:=MyArrayY^[t-1]+Random(51)-25;
- end;
-
- { tell the Series to use our private arrays... }
- With Series1 do
- begin
- { create a new "custom" list for X values }
- ReplaceList(XValues,TMyList.Create(Series1,''));
- With TMyList(XValues) do
- begin
- Order:=loAscending;
- MyCount:=NumPoints; { <-- set number of points }
- MyArray:=MyArrayX; { <-- set our X array }
- end;
- { create a new "custom" list for Y values }
- ReplaceList(YValues,TMyList.Create(Series1,''));
- With TMyList(YValues) do
- begin
- MyCount:=NumPoints; { <-- set number of points }
- MyArray:=MyArrayY; { <-- set our Y array }
- end;
- Repaint; { Show the Array points ! }
- end;
- end;
-
- Procedure TArrayForm.DemoWithoutArrays;
- Var t:Longint;
- begin
- With Series1 do
- begin
- { in case the Series is still using our "custom arrays",
- reset again to the default objects }
- ReplaceList(XValues,TChartValueList.Create(Series1,''));
- ReplaceList(YValues,TChartValueList.Create(Series1,''));
-
- { disable redrawing the Chart for each new added point... }
- AutoRepaint:=False;
- { remove all values in the series... }
- Clear;
- Application.ProcessMessages;
- { disable X ordering }
- XValues.Order:=loNone;
-
- { Disable Chart AutoRepaint to improve speed a little bit }
- ParentChart.AutoRepaint:=False;
-
- { add random values.... }
- Add(1000,'',clTeeColor);
- for t:=1 to NumPoints do Add(YValues.Last+Random(51)-25.0,'',clTeeColor);
-
- { Restore Chart AutoRepaint }
- ParentChart.AutoRepaint:=True;
- end;
- end;
-
- procedure TArrayForm.Button2Click(Sender: TObject);
- Var StartTime,
- EndTime:Longint;
- begin
- StartTime:=GetTickCount; { start Time test }
-
- { do the demo ! }
- if UseArrays.Checked then DemoWithArrays
- else DemoWithoutArrays;
- { finish Time test }
- EndTime:=GetTickCount;
- { show the elapsed time }
- LabelTime.Caption:=IntToStr(EndTime-StartTime);
- end;
-
- procedure TArrayForm.FormCreate(Sender: TObject);
- begin
- { Change the Axis scales to suitable values for this example...
- This is optional but recommended to speed-up display speed.
- }
- Chart1.LeftAxis.SetMinMax(-10000,10000);
- Chart1.BottomAxis.SetMinMax(0,NumPoints+1000);
-
- { create our private Arrays for this example... }
- New(MyArrayX);
- New(MyArrayY);
- end;
-
- procedure TArrayForm.FormDestroy(Sender: TObject);
- begin
- { In case the Series still has our "custom" arrays... }
- if Series1.XValues is TMyList then
- TMyList(Series1.XValues).MyArray:=nil;
- if Series1.YValues is TMyList then
- TMyList(Series1.YValues).MyArray:=nil;
-
- { Deallocate memory and arrays }
- Dispose(MyArrayX);
- Dispose(MyArrayY);
- end;
-
- procedure TArrayForm.CheckBox2Click(Sender: TObject);
- begin
- Chart1.BufferedDisplay:=CheckBox2.Checked;
- end;
-
- end.
-