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

  1. {*********************************************}
  2. { TeeChart Delphi Component Library           }
  3. { Functions with Range Period Demo            }
  4. { Copyright (c) 1995-1998 by David Berneda    }
  5. { All rights reserved                         }
  6. {*********************************************}
  7. unit UFunRan;
  8.  
  9. interface
  10.  
  11. uses
  12.   WinProcs,WinTypes, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  13.   StdCtrls, ExtCtrls, TeEngine, CurvFitt, Series, TeeProcs, Chart, TeeComma;
  14.  
  15. type
  16.   TFunctionRangeForm = class(TForm)
  17.     Panel1: TPanel;
  18.     Memo1: TMemo;
  19.     Button1: TButton;
  20.     TeeCommander1: TTeeCommander;
  21.     Chart1: TChart;
  22.     Series1: TFastLineSeries;
  23.     Series2: TLineSeries;
  24.     TeeFunction1: TTrendFunction;
  25.     RadioGroup1: TRadioGroup;
  26.     Label1: TLabel;
  27.     ComboBox1: TComboBox;
  28.     CheckboxForecast: TCheckBox;
  29.     procedure Button1Click(Sender: TObject);
  30.     procedure FormCreate(Sender: TObject);
  31.     procedure RadioGroup1Click(Sender: TObject);
  32.     procedure ComboBox1Change(Sender: TObject);
  33.     procedure CheckboxForecastClick(Sender: TObject);
  34.     procedure Chart1AfterDraw(Sender: TObject);
  35.   private
  36.     { Private declarations }
  37.   public
  38.     { Public declarations }
  39.   end;
  40.  
  41. implementation
  42.  
  43. {$R *.DFM}
  44.  
  45. procedure TFunctionRangeForm.Button1Click(Sender: TObject);
  46. begin
  47.   Close;
  48. end;
  49.  
  50. procedure TFunctionRangeForm.FormCreate(Sender: TObject);
  51. var t:Longint;
  52.     tmp:Double;
  53. begin
  54.  { First lets add one year of data to Series1 }
  55.   With Series1 do
  56.   begin
  57.     Clear;
  58.     tmp:=1000;
  59.     for t:=1 to 364 do
  60.     begin
  61.       tmp:=tmp+Random(50)-25.0;
  62.       AddXY( EncodeDate(1998,1,1)+t, tmp,'', clTeeColor);
  63.     end;
  64.     XValues.DateTime:=True;
  65.   end;
  66.  
  67.   { Set the Trend period initially to "no period" (all points) }
  68.   TeeFunction1.Period:=0;
  69.   TeeFunction1.PeriodStyle:=psNumPoints;
  70.  
  71.   { Refresh the Trend }
  72.   Series2.CheckDataSource;
  73.  
  74.   { Set the bottom axis datetime format properly }
  75.   With Chart1.BottomAxis do
  76.   begin
  77.     DateTimeFormat:='mmm dd';  { <-- show axis labels  }
  78.     ExactDateTime:=True;      { <-- at exact "begin of the month" }
  79.     Increment:=DateTimeStep[dtOneMonth];  { one month each label }
  80.     MinorTickCount:=3;         { <-- 3 minor ticks to divide weeks }
  81.     LabelsMultiLine:=True;     { <-- two rows per label }
  82.   end;
  83.  
  84.   { Set the initial combobox value }
  85.   ComboBox1.ItemIndex:=1;
  86.  
  87.   { Expand the bottom axis to show a forecast line }
  88.   CheckboxForecastClick(Self);
  89. end;
  90.  
  91. procedure TFunctionRangeForm.RadioGroup1Click(Sender: TObject);
  92. begin
  93.   ComboBox1.Enabled:=RadioGroup1.ItemIndex=2;
  94.  
  95.   { Set the trend function Period... }
  96.   Case RadioGroup1.ItemIndex of
  97.      0: TeeFunction1.Period:=0;  { All points. No period }
  98.      1: begin
  99.           TeeFunction1.PeriodStyle:=psNumPoints;
  100.           TeeFunction1.Period:=30; { calculate every 30 points }
  101.         end;
  102.      2: begin
  103.           TeeFunction1.PeriodStyle:=psRange;
  104.           { change period range according to Combobox options }
  105.           ComboBox1Change(Self);
  106.         end;
  107.   end;
  108. end;
  109.  
  110. procedure TFunctionRangeForm.ComboBox1Change(Sender: TObject);
  111. begin
  112.   Case ComboBox1.ItemIndex of
  113.      0: TeeFunction1.Period:=DateTimeStep[dtOneWeek];
  114.      1: TeeFunction1.Period:=DateTimeStep[dtOneMonth];
  115.      2: TeeFunction1.Period:=DateTimeStep[dtTwoMonths];
  116.      3: TeeFunction1.Period:=DateTimeStep[dtThreeMonths];
  117.      4: TeeFunction1.Period:=DateTimeStep[dtSixMonths];
  118.   end;
  119. end;
  120.  
  121. procedure TFunctionRangeForm.CheckboxForecastClick(Sender: TObject);
  122. begin
  123.   { Set the bottom axis to be one more month long or automatic }
  124.   With Chart1.BottomAxis do
  125.   if CheckBoxForeCast.Checked then
  126.   begin
  127.     Automatic:=False;
  128.     AutomaticMaximum:=False;
  129.     Maximum:=Series1.XValues.MaxValue+30; { one more month }
  130.     AutomaticMinimum:=False;
  131.   end
  132.   else Automatic:=True;
  133.  
  134.   { Force repaint... }
  135.   Chart1.Repaint;
  136. end;
  137.  
  138. procedure TFunctionRangeForm.Chart1AfterDraw(Sender: TObject);
  139. Var x0,
  140.     x1:Longint;
  141.     m,b:Double;
  142. begin
  143.   if CheckBoxForeCast.Checked then
  144.   With Chart1.Canvas do
  145.   begin
  146.     { calculate the X position for the last point in the series }
  147.     x0:=Series1.CalcXPos(Series1.Count-1);
  148.     { calculate the X position for the last point in the series + Forecast }
  149.     x1:=Series1.CalcXPosValue(Series1.XValues.Last+30);
  150.  
  151.     { Calculate Trend factors for all points in Series1 }
  152.     TeeFunction1.CalculateTrend(m,b,Series1,0,Series1.Count-1);
  153.  
  154.     { Draw the forecast trend line }
  155.     Pen.Color:=clLime;
  156.     Pen.Style:=psSolid;
  157.     Pen.Width:=2;
  158.     With Series1 do
  159.     begin
  160.       MoveTo3D( x0, CalcYPosValue(m*XValues.Last+b), MiddleZ );
  161.       LineTo3D( x1, CalcYPosValue(m*(XValues.Last+30)+b), MiddleZ );
  162.     end;
  163.   end;
  164. end;
  165.  
  166. end.
  167.