home *** CD-ROM | disk | FTP | other *** search
- {*********************************************}
- { TeeChart Delphi Component Library }
- { Functions with Range Period Demo }
- { Copyright (c) 1995-1998 by David Berneda }
- { All rights reserved }
- {*********************************************}
- unit UFunRan;
-
- interface
-
- uses
- WinProcs,WinTypes, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls, ExtCtrls, TeEngine, CurvFitt, Series, TeeProcs, Chart, TeeComma;
-
- type
- TFunctionRangeForm = class(TForm)
- Panel1: TPanel;
- Memo1: TMemo;
- Button1: TButton;
- TeeCommander1: TTeeCommander;
- Chart1: TChart;
- Series1: TFastLineSeries;
- Series2: TLineSeries;
- TeeFunction1: TTrendFunction;
- RadioGroup1: TRadioGroup;
- Label1: TLabel;
- ComboBox1: TComboBox;
- CheckboxForecast: TCheckBox;
- procedure Button1Click(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure RadioGroup1Click(Sender: TObject);
- procedure ComboBox1Change(Sender: TObject);
- procedure CheckboxForecastClick(Sender: TObject);
- procedure Chart1AfterDraw(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
-
- implementation
-
- {$R *.DFM}
-
- procedure TFunctionRangeForm.Button1Click(Sender: TObject);
- begin
- Close;
- end;
-
- procedure TFunctionRangeForm.FormCreate(Sender: TObject);
- var t:Longint;
- tmp:Double;
- begin
- { First lets add one year of data to Series1 }
- With Series1 do
- begin
- Clear;
- tmp:=1000;
- for t:=1 to 364 do
- begin
- tmp:=tmp+Random(50)-25.0;
- AddXY( EncodeDate(1998,1,1)+t, tmp,'', clTeeColor);
- end;
- XValues.DateTime:=True;
- end;
-
- { Set the Trend period initially to "no period" (all points) }
- TeeFunction1.Period:=0;
- TeeFunction1.PeriodStyle:=psNumPoints;
-
- { Refresh the Trend }
- Series2.CheckDataSource;
-
- { Set the bottom axis datetime format properly }
- With Chart1.BottomAxis do
- begin
- DateTimeFormat:='mmm dd'; { <-- show axis labels }
- ExactDateTime:=True; { <-- at exact "begin of the month" }
- Increment:=DateTimeStep[dtOneMonth]; { one month each label }
- MinorTickCount:=3; { <-- 3 minor ticks to divide weeks }
- LabelsMultiLine:=True; { <-- two rows per label }
- end;
-
- { Set the initial combobox value }
- ComboBox1.ItemIndex:=1;
-
- { Expand the bottom axis to show a forecast line }
- CheckboxForecastClick(Self);
- end;
-
- procedure TFunctionRangeForm.RadioGroup1Click(Sender: TObject);
- begin
- ComboBox1.Enabled:=RadioGroup1.ItemIndex=2;
-
- { Set the trend function Period... }
- Case RadioGroup1.ItemIndex of
- 0: TeeFunction1.Period:=0; { All points. No period }
- 1: begin
- TeeFunction1.PeriodStyle:=psNumPoints;
- TeeFunction1.Period:=30; { calculate every 30 points }
- end;
- 2: begin
- TeeFunction1.PeriodStyle:=psRange;
- { change period range according to Combobox options }
- ComboBox1Change(Self);
- end;
- end;
- end;
-
- procedure TFunctionRangeForm.ComboBox1Change(Sender: TObject);
- begin
- Case ComboBox1.ItemIndex of
- 0: TeeFunction1.Period:=DateTimeStep[dtOneWeek];
- 1: TeeFunction1.Period:=DateTimeStep[dtOneMonth];
- 2: TeeFunction1.Period:=DateTimeStep[dtTwoMonths];
- 3: TeeFunction1.Period:=DateTimeStep[dtThreeMonths];
- 4: TeeFunction1.Period:=DateTimeStep[dtSixMonths];
- end;
- end;
-
- procedure TFunctionRangeForm.CheckboxForecastClick(Sender: TObject);
- begin
- { Set the bottom axis to be one more month long or automatic }
- With Chart1.BottomAxis do
- if CheckBoxForeCast.Checked then
- begin
- Automatic:=False;
- AutomaticMaximum:=False;
- Maximum:=Series1.XValues.MaxValue+30; { one more month }
- AutomaticMinimum:=False;
- end
- else Automatic:=True;
-
- { Force repaint... }
- Chart1.Repaint;
- end;
-
- procedure TFunctionRangeForm.Chart1AfterDraw(Sender: TObject);
- Var x0,
- x1:Longint;
- m,b:Double;
- begin
- if CheckBoxForeCast.Checked then
- With Chart1.Canvas do
- begin
- { calculate the X position for the last point in the series }
- x0:=Series1.CalcXPos(Series1.Count-1);
- { calculate the X position for the last point in the series + Forecast }
- x1:=Series1.CalcXPosValue(Series1.XValues.Last+30);
-
- { Calculate Trend factors for all points in Series1 }
- TeeFunction1.CalculateTrend(m,b,Series1,0,Series1.Count-1);
-
- { Draw the forecast trend line }
- Pen.Color:=clLime;
- Pen.Style:=psSolid;
- Pen.Width:=2;
- With Series1 do
- begin
- MoveTo3D( x0, CalcYPosValue(m*XValues.Last+b), MiddleZ );
- LineTo3D( x1, CalcYPosValue(m*(XValues.Last+30)+b), MiddleZ );
- end;
- end;
- end;
-
- end.
-