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

  1. unit Ubarzoom;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winprocs, WinTypes, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   TeEngine, Series, ExtCtrls, TeeProcs, Chart, StdCtrls, Buttons;
  8.  
  9. { This example shows customized Bar Series. }
  10.  
  11. { The new Series below are simple Bar Series with a new feature:
  12.  
  13.      The Bars width is automatically resized when zooming.
  14.  
  15.   Also this demo shows how to perform " Animated Scroll " .
  16.  
  17. }
  18.  
  19. type
  20.   { the new Series components... }
  21.   TBarSeriesZoom=class(TBarSeries)
  22.   protected
  23. {    Function InternalCalcBarSize:Longint; override;}
  24.   end;
  25.  
  26.   THorizBarSeriesZoom=class(THorizBarSeries)
  27.   protected
  28. {    Function InternalCalcBarSize:Longint; override;}
  29.   end;
  30.  
  31.   TBarZoomForm = class(TForm)
  32.     Chart1: TChart;
  33.     Panel1: TPanel;
  34.     BitBtn3: TBitBtn;
  35.     BitBtn1: TBitBtn;
  36.     BitBtn2: TBitBtn;
  37.     BitBtn4: TBitBtn;
  38.     ScrollBar1: TScrollBar;
  39.     Label1: TLabel;
  40.     Series1: TBarSeries;
  41.     ComboBox1: TComboBox;
  42.     Label2: TLabel;
  43.     Panel2: TPanel;
  44.     Memo1: TMemo;
  45.     Memo2: TMemo;
  46.     Button1: TButton;
  47.     procedure FormCreate(Sender: TObject);
  48.     procedure BitBtn2Click(Sender: TObject);
  49.     procedure BitBtn1Click(Sender: TObject);
  50.     procedure BitBtn3Click(Sender: TObject);
  51.     procedure BitBtn4Click(Sender: TObject);
  52.     procedure ScrollBar1Change(Sender: TObject);
  53.     procedure ComboBox1Change(Sender: TObject);
  54.     procedure Button1Click(Sender: TObject);
  55.   private
  56.     { Private declarations }
  57.   public
  58.     { Public declarations }
  59.     TheAxis:TChartAxis;
  60.     procedure AnimatedScroll( Const Speed:Double );
  61.     procedure CreateSampleSeries;
  62.   end;
  63.  
  64. var
  65.   BarZoomForm: TBarZoomForm;
  66.  
  67. implementation
  68.  
  69. {$R *.DFM}
  70. uses TeeShape;
  71.  
  72. { This is the calculation necessary to change the Bar width and height 
  73.    when the user zooms....   }
  74. {Function TBarSeriesZoom.InternalCalcBarSize:Longint;
  75. begin
  76.   With ParentChart do
  77.   result:=Round(ChartWidth*(1.0*Count+1)/(1.0+GetHorizAxis.Maximum-GetHorizAxis.Minimum));
  78. end;
  79. }
  80. { This is the calculation necessary to change the Bar width and height
  81.    when the user zooms....   }
  82. {Function THorizBarSeriesZoom.InternalCalcBarSize:Longint;
  83. begin
  84.   With ParentChart do
  85.   result:=Round(ChartHeight*(1.0*Count+1)/(1.0+GetVertAxis.Maximum-GetVertAxis.Minimum));
  86. end;
  87. }
  88. { This function simply adds a Shape Series to the Chart... }
  89. Function AddBanner( AChart:TCustomChart;
  90.                     const AX0,AX1,AY0,AY1:Double;
  91.                     AColor,AFontColor:TColor;
  92.                     Const AText:String):TChartShape;
  93. begin
  94.   result:=TChartShape.Create(AChart);
  95.   With result do
  96.   begin
  97.     X0:=AX0;
  98.     X1:=AX1;
  99.     Y0:=AY0;
  100.     Y1:=AY1;
  101.     Style:=chasRectangle;
  102.     Brush.Color:=AColor;
  103.     Text.Add(AText);
  104.     With Font do
  105.     begin
  106.       Size:=12;
  107.       Style:=[fsBold];
  108.       Name:='Arial';
  109.       Color:=AFontColor;
  110.     end;
  111.     Pen.Width:=2;
  112.     RoundRectangle:=True;
  113.     ParentChart:=AChart;
  114.   end;
  115. end;
  116.  
  117. { This procedure does "Animated Zoom"  BUT ONLY in ONE Axis, 
  118.    instead of the normal behavior ( two axis ).
  119. }
  120. Procedure ZoomAxisPercent(Axis:TChartAxis; Const PercentZoom:Double);
  121.  
  122.   Procedure CalcAxisScale(Axis:TChartAxis; Var tmpA,tmpB:Double);
  123.   Var tmpDelta,AMin,AMax:Double;
  124.   Begin
  125.     AMin:=Axis.Minimum;
  126.     AMax:=Axis.Maximum;
  127.     Axis.CalcMinMax(AMin,AMax);
  128.     tmpDelta:=(AMax-AMin)*(PercentZoom-100.0)/100.0;
  129.     tmpA:=AMin+tmpDelta;
  130.     tmpB:=AMax-tmpDelta;
  131.   end;
  132.  
  133. var A,B:Double;
  134.     t:Longint;
  135. Begin  { zoom a given "percent" }
  136.   CalcAxisScale(Axis,A,B);
  137.   With (Axis.ParentChart as TCustomChart) do
  138.   for t:=1 to AnimatedZoomSteps do
  139.   Begin
  140.     with Axis do
  141.       SetMinMax(Minimum+((A-Minimum)/AnimatedZoomFactor),
  142.                 Maximum-((Maximum-B)/AnimatedZoomFactor));
  143.     Refresh;
  144.   end;
  145. end;
  146.  
  147. { Add some random values to the Chart... }
  148. procedure TBarZoomForm.CreateSampleSeries;
  149. Const MaxPoints=30;
  150. var t,num:Integer;
  151. begin
  152.   for num:=1 to 3 do
  153.   With TBarSeriesZoom.Create(Self) do
  154.   begin
  155.     Marks.Visible:=(num=3);
  156.     Marks.BackColor:=clNavy;
  157.     Marks.Font.Color:=clWhite;
  158.     MultiBar:=mbNone;
  159.     for t:=1 to MaxPoints do Add(Random(1000),'',clTeeColor);
  160.     ParentChart:=Chart1;
  161.   end;
  162. end;
  163.  
  164. procedure TBarZoomForm.FormCreate(Sender: TObject);
  165. begin
  166.   Chart1.FreeAllSeries;
  167.   Chart1.AnimatedZoom:=True;
  168.   Chart1.AnimatedZoomSteps:=3;
  169.   Chart1.Chart3DPercent:=10;
  170.  
  171.   ComboBox1.ItemIndex:=0;
  172.  
  173.   AddBanner( Chart1,  3, 12, 1100, 1700, clBlue,clYellow, 'Winter Sales');
  174.   AddBanner( Chart1, 15, 22, 1100, 1700, clLime,clBlue,'Summer Sales');
  175.  
  176.   TheAxis:=Chart1.BottomAxis;
  177.   CreateSampleSeries;
  178.   TheAxis.SetMinMax(5,12);
  179. end;
  180.  
  181. procedure TBarZoomForm.BitBtn2Click(Sender: TObject);
  182. begin
  183.   ZoomAxisPercent(TheAxis,80);
  184. end;
  185.  
  186. procedure TBarZoomForm.BitBtn1Click(Sender: TObject);
  187. begin
  188.   ZoomAxisPercent(TheAxis,120);
  189. end;
  190.  
  191. { The animated scroll routine... }
  192. procedure TBarZoomForm.AnimatedScroll( Const Speed:Double );
  193. var t:Integer;
  194. begin
  195.   for t:=1 to 5 do   { <-- 5 animation steps... }
  196.   begin
  197.     TheAxis.Scroll(Speed,False);
  198.     Chart1.Refresh;
  199.   end;
  200. end;
  201.  
  202. procedure TBarZoomForm.BitBtn3Click(Sender: TObject);
  203. begin
  204.   AnimatedScroll(0.25);
  205. end;
  206.  
  207. procedure TBarZoomForm.BitBtn4Click(Sender: TObject);
  208. begin
  209.   AnimatedScroll(-0.25);
  210. end;
  211.  
  212. procedure TBarZoomForm.ScrollBar1Change(Sender: TObject);
  213. begin
  214.   Chart1.View3D:=ScrollBar1.Position>0;
  215.   if Chart1.View3D then
  216.      Chart1.Chart3DPercent:=ScrollBar1.Position;
  217. end;
  218.  
  219. procedure TBarZoomForm.ComboBox1Change(Sender: TObject);
  220. var t:Integer;
  221. begin
  222.   With (Chart1[Chart1.SeriesCount-1] as TCustomBarSeries) do
  223.   begin
  224.     Case ComboBox1.ItemIndex of
  225.       0: MultiBar:=mbNone;
  226.       1: MultiBar:=mbSide;
  227.       2: MultiBar:=mbStacked;
  228.     end;
  229.     for t:=0 to Chart1.SeriesCount-1 do
  230.     if Chart1[t] is TChartShape then
  231.        With TChartShape(Chart1[t]) do
  232.        if MultiBar=mbStacked then
  233.        begin
  234.         Y0:=2100;
  235.         Y1:=2700;
  236.        end
  237.        else
  238.        begin
  239.         Y0:=1100;
  240.         Y1:=1700;
  241.        end;
  242.     end
  243. end;
  244.  
  245. procedure TBarZoomForm.Button1Click(Sender: TObject);
  246. begin
  247.   Close;
  248. end;
  249.  
  250. end.
  251.