home *** CD-ROM | disk | FTP | other *** search
/ Delphi 5 for Professionals / DELPHI5.iso / AddOns / Components / TEECHART / Src Code / TEEROSE.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-10-24  |  8.8 KB  |  324 lines

  1. {**********************************************}
  2. {   TWindRoseSeries                            }
  3. {   Copyright (c) 1998 by David Berneda        }
  4. {      Series Developer Kit Example            }
  5. {**********************************************}
  6. {$I teedefs.inc}
  7. unit TeeRose;
  8.  
  9. interface
  10.  
  11. Uses WinTypes,WinProcs,SysUtils,Classes,Graphics,Teengine,Chart,
  12.      TeCanvas, Series, TeePolar, ExtCtrls;
  13.  
  14. {  This unit contains two Series components:
  15.  
  16.    TWindRoseSeries --  A Polar Series displaying Wind directions.
  17.    TClockSeries    --  A Polar Series showing a watch.
  18. }
  19.  
  20. Type TWindRoseSeries=class(TCustomPolarSeries)
  21.      protected
  22.        Function GetCircleLabel(Const AngleOrIndex:Double):String; override;
  23.      public
  24.        Constructor Create(AOwner: TComponent); override;
  25.        Procedure PrepareForGallery(IsEnabled:Boolean); override;
  26.      published
  27.        property AngleIncrement;
  28.        property AngleValues;
  29.        property Brush;
  30.        property CircleBackColor;
  31.        property CircleLabels default True;
  32.        property CircleLabelsFont;
  33.        property CircleLabelsRotated;
  34.        property CirclePen;
  35.        property CloseCircle;
  36.        property Pen;
  37.        property Pointer;
  38.        property RadiusIncrement;
  39.        property RadiusValues;
  40.        property RotationAngle default 90;
  41.      end;
  42.  
  43.      TClockSeries=class;
  44.  
  45.      TClockSeriesStyle=(cssDecimal,cssRoman);
  46.  
  47.      TClockSeriesGetTimeEvent=procedure(Sender:TClockSeries; Var ATime:TDateTime) of object;
  48.  
  49.      { This series draws a "Clock" using a Polar series }
  50.      TClockSeries=class(TCustomPolarSeries)
  51.      private
  52.        FOnGetTime  : TClockSeriesGetTimeEvent;
  53.        FPenHours   : TChartPen;
  54.        FPenMinutes : TChartPen;
  55.        FPenSeconds : TChartPen;
  56.        FStyle      : TClockSeriesStyle;
  57.  
  58.        ITimer      : TTimer;
  59.        Procedure SetPenHours(Value:TChartPen);
  60.        Procedure SetPenMinutes(Value:TChartPen);
  61.        Procedure SetPenSeconds(Value:TChartPen);
  62.        Procedure SetStyle(Value:TClockSeriesStyle);
  63.        Procedure OnTimerExpired(Sender:TObject);
  64.      protected
  65.        Procedure DrawAllValues; override;
  66.        Function GetCircleLabel(Const AngleOrIndex:Double):String; override;
  67.        Procedure SetParentChart(Value:TCustomAxisPanel); override;
  68.      public
  69.        Constructor Create(AOwner: TComponent); override;
  70.        Destructor Destroy; override;
  71.  
  72.        Procedure Assign(Source:TPersistent); override;
  73.        Function NumSampleValues:Longint; override;
  74.        Procedure PrepareForGallery(IsEnabled:Boolean); override;
  75.      published
  76.        property Brush;
  77.        property CircleBackColor;
  78.        property Circled default True;
  79.        property CircleLabels default True;
  80.        property CircleLabelsFont;
  81.        property CircleLabelsRotated;
  82.        property CirclePen;
  83.        property PenHours:TChartPen read FPenHours write SetPenHours;
  84.        property PenMinutes:TChartPen read FPenMinutes write SetPenMinutes;
  85.        property PenSeconds:TChartPen read FPenSeconds write SetPenSeconds;
  86.        property RotationAngle default 90;
  87.        property Style:TClockSeriesStyle read FStyle write SetStyle
  88.                                         default cssRoman;
  89.        { events }
  90.        property OnGetTime  : TClockSeriesGetTimeEvent read FOnGetTime
  91.                                                       write FOnGetTime;
  92.      end;
  93.  
  94. implementation
  95.  
  96. Uses TeeProco;  { <-- needed because the "Samples" constant }
  97.  
  98. { TWindRoseSeries }
  99. Constructor TWindRoseSeries.Create(AOwner: TComponent);
  100. begin
  101.   inherited Create(AOwner);
  102.   CircleLabels:=True;
  103.   RotationAngle:=90;
  104.   {$IFDEF TEETRIAL}
  105.   TeeTrial(ComponentState);
  106.   {$ENDIF}
  107. end;
  108.  
  109. Procedure TWindRoseSeries.PrepareForGallery(IsEnabled:Boolean);
  110. begin
  111.   inherited PrepareForGallery(IsEnabled);
  112.   CircleLabelsFont.Size:=6;
  113.   Pointer.HorizSize:=2;
  114.   Pointer.VertSize:=2;
  115. end;
  116.  
  117. { Return the string corresponding to the "Angle" degree parameter }
  118. Function TWindRoseSeries.GetCircleLabel(Const AngleOrIndex:Double):String;
  119. begin
  120.   Case Round(AngleOrIndex) of
  121.       0:  result:='N';
  122.      45:  result:='NW';
  123.      90:  result:='W';
  124.     135:  result:='SW';
  125.     180:  result:='S';
  126.     225:  result:='SE';
  127.     270:  result:='E';
  128.     315:  result:='NE';
  129.   else result:='';
  130.   end;
  131. end;
  132.  
  133. { TClockSeries }
  134. Constructor TClockSeries.Create(AOwner: TComponent);
  135. begin
  136.   inherited Create(AOwner);
  137.   Pointer.Visible:=False;
  138.   ShowInLegend:=False;
  139.   FStyle:=cssRoman;
  140.   Brush.Style:=bsSolid;
  141.   CircleLabels:=True;
  142.   RotationAngle:=90;
  143.   Circled:=True;
  144.   Add(0 {$IFNDEF D4},'',clTeeColor{$ENDIF});
  145.   FPenHours:=CreateChartPen;
  146.   FPenMinutes:=CreateChartPen;
  147.   FPenSeconds:=CreateChartPen;
  148.   ITimer:=TTimer.Create(Self);
  149.   With ITimer do
  150.   begin
  151.     Interval:=1000;
  152.     Enabled:=True;
  153.     OnTimer:=OnTimerExpired;
  154.   end;
  155.   {$IFDEF TEETRIAL}
  156.   TeeTrial(ComponentState);
  157.   {$ENDIF}
  158. end;
  159.  
  160. Destructor TClockSeries.Destroy;
  161. begin
  162.   FPenHours.Free;
  163.   FPenMinutes.Free;
  164.   FPenSeconds.Free;
  165.   ITimer.Free;
  166.   inherited Destroy;
  167. end;
  168.  
  169. Procedure TClockSeries.OnTimerExpired(Sender:TObject);
  170. begin
  171.   ITimer.Enabled:=False;
  172.   Repaint;
  173.   ITimer.Enabled:=True;
  174. end;
  175.  
  176. Procedure TClockSeries.Assign(Source:TPersistent);
  177. begin
  178.   if Source is TClockSeries then
  179.   With TClockSeries(Source) do
  180.   begin
  181.     Self.FPenHours.Assign(FPenHours);
  182.     Self.FPenMinutes.Assign(FPenMinutes);
  183.     Self.FPenSeconds.Assign(FPenSeconds);
  184.     Self.FStyle:=FStyle;
  185.   end;
  186.   inherited Assign(Source);
  187. end;
  188.  
  189. Const PiDegree=Pi/180.0;
  190.  
  191. Procedure TClockSeries.DrawAllValues;
  192. Var X  : {$IFDEF D2C1}Longint{$ELSE}Integer{$ENDIF};
  193.     Y  : {$IFDEF D2C1}Longint{$ELSE}Integer{$ENDIF};
  194.  
  195.     Procedure CalcPos(Const AAngle,ASize:Double);
  196.     begin
  197.       AngleToPos(AAngle*PiDegree,ASize*XRadius/2.0,ASize*YRadius/2.0,X,Y);
  198.     end;
  199.  
  200. Var H  : Word;
  201.     M  : Word;
  202.     S  : Word;
  203.     Ms : Word;
  204.     tmpColor:TColor;
  205.     tmp : TDateTime;
  206. begin
  207.   With ParentChart.Canvas do
  208.   begin
  209.     tmp:=Now;
  210.     if Assigned(FOnGetTime) then FOnGetTime(Self,tmp);
  211.     DecodeTime(tmp,H,M,S,Ms);
  212.     Brush.Assign(Self.Brush);
  213.  
  214.     if FPenHours.Visible then
  215.     begin
  216.       Pen.Assign(FPenHours);
  217.       CalcPos(360.0-(360.0*(60.0*H+M)/(12.0*60.0)),1.3);
  218.       Arrow(True,Point(CircleXCenter,CircleYCenter),Point(X,Y),14,20,EndZ);
  219.     end;
  220.  
  221.     if FPenMinutes.Visible then
  222.     begin
  223.       Pen.Assign(FPenMinutes);
  224.       CalcPos(360.0-(360.0*M/60.0),1.7);
  225.       Arrow(True,Point(CircleXCenter,CircleYCenter),Point(X,Y),10,16,EndZ);
  226.     end;
  227.  
  228.     CalcPos(360.0-(360.0*S/60.0),1.8);
  229.     if FPenSeconds.Visible then
  230.     begin
  231.       Pen.Assign(FPenSeconds);
  232.       MoveTo3D(CircleXCenter,CircleYCenter,EndZ);
  233.       LineTo3D(X,Y,EndZ);
  234.     end;
  235.  
  236.     With Pointer do
  237.     if Visible then
  238.     begin
  239.       tmpColor:=Brush.Color;
  240.       PrepareCanvas(tmpColor);
  241.       Draw(X,Y,tmpColor,Style);
  242.     end;
  243.   end;
  244. end;
  245.  
  246. Procedure TClockSeries.SetParentChart(Value:TCustomAxisPanel);
  247. begin
  248.   inherited SetParentChart(Value);
  249.   if ParentChart<>nil then
  250.   With ParentChart do
  251.   begin
  252.     LeftAxis.Visible:=False;
  253.     TopAxis.Visible:=False;
  254.     RightAxis.Visible:=False;
  255.     BottomAxis.Visible:=False;
  256.     AngleIncrement:=30;
  257.   end;
  258. end;
  259.  
  260. Procedure TClockSeries.PrepareForGallery(IsEnabled:Boolean);
  261. begin
  262.   inherited PrepareForGallery(IsEnabled);
  263.   CircleLabelsFont.Size:=6;
  264.   Pointer.HorizSize:=2;
  265.   Pointer.VertSize:=2;
  266. end;
  267.  
  268. { Return the string corresponding to the "Angle" degree parameter }
  269. Function TClockSeries.GetCircleLabel(Const AngleOrIndex:Double):String;
  270. Const RomanNumber:Array[1..12] of String=
  271.   ('I','II','III','IV','V','VI','VII','VIII','IX','X','XI','XII');
  272. var tmpAngleIndex:Integer;
  273. begin
  274.   tmpAngleIndex:=Round((360.0-AngleOrIndex)/30.0);
  275.   if FStyle=cssDecimal then result:=IntToStr(tmpAngleIndex)
  276.                        else result:=RomanNumber[tmpAngleIndex];
  277. end;
  278.  
  279. Procedure TClockSeries.SetPenHours(Value:TChartPen);
  280. begin
  281.   FPenHours.Assign(Value);
  282. end;
  283.  
  284. Procedure TClockSeries.SetPenMinutes(Value:TChartPen);
  285. begin
  286.   FPenMinutes.Assign(Value);
  287. end;
  288.  
  289. Procedure TClockSeries.SetPenSeconds(Value:TChartPen);
  290. begin
  291.   FPenSeconds.Assign(Value);
  292. end;
  293.  
  294. Procedure TClockSeries.SetStyle(Value:TClockSeriesStyle);
  295. begin
  296.   if FStyle<>Value then
  297.   begin
  298.     FStyle:=Value;
  299.     Repaint;
  300.   end;
  301. end;
  302.  
  303. Function TClockSeries.NumSampleValues:Longint;
  304. begin
  305.   result:=1;
  306. end;
  307.  
  308. { Series/Functions Registration/Un-Registration }
  309. Procedure WindRoseExitProc; far;
  310. begin
  311.   UnRegisterTeeSeries([ TWindRoseSeries, TClockSeries ]);
  312. end;
  313.  
  314. initialization
  315.   RegisterTeeSeries( TWindRoseSeries, 'Wind Rose', TeeMsg_GallerySamples, 1 );
  316.   RegisterTeeSeries( TClockSeries, 'Clock', TeeMsg_GallerySamples, 1 );
  317. {$IFDEF D1}
  318.   AddExitProc(WindRoseExitProc);
  319. {$ELSE}
  320. finalization
  321.   WindRoseExitProc;
  322. {$ENDIF}
  323. end.
  324.