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

  1. {*********************************************}
  2. { TeeChart Delphi Component Library           }
  3. { Stacked Bar Series Example                  }
  4. { Copyright (c) 1995-1996 by David Berneda    }
  5. { All rights reserved                         }
  6. {*********************************************}
  7. unit Ustack;
  8.  
  9. interface
  10.  
  11. uses
  12.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  13.   Forms, Dialogs, Chart, Series, ExtCtrls, StdCtrls, Teengine, ArrowCha,
  14.   Buttons, teeprocs, TeeComma;
  15.  
  16. type
  17.   TStackedForm = class(TForm)
  18.     Chart1: TChart;
  19.     BarSeries1: TBarSeries;
  20.     BarSeries2: TBarSeries;
  21.     Panel1: TPanel;
  22.     Button1: TButton;
  23.     RadioGroup2: TRadioGroup;
  24.     ComboBox1: TComboBox;
  25.     Label1: TLabel;
  26.     CheckBox1: TCheckBox;
  27.     Timer1: TTimer;
  28.     BarSeries3: TBarSeries;
  29.     Shape1: TShape;
  30.     BitBtn3: TBitBtn;
  31.     CheckBox2: TCheckBox;
  32.     TeeCommander1: TTeeCommander;
  33.     ComboBox2: TComboBox;
  34.     procedure FormCreate(Sender: TObject);
  35.     procedure Button1Click(Sender: TObject);
  36.     procedure ComboBox1Change(Sender: TObject);
  37.     procedure RadioGroup2Click(Sender: TObject);
  38.     procedure CheckBox1Click(Sender: TObject);
  39.     procedure Timer1Timer(Sender: TObject);
  40.     procedure Shape1MouseUp(Sender: TObject; Button: TMouseButton;
  41.       Shift: TShiftState; X, Y: Integer);
  42.     procedure CheckBox2Click(Sender: TObject);
  43.     procedure ComboBox2Change(Sender: TObject);
  44.   private
  45.     { Private declarations }
  46.   public
  47.     { Public declarations }
  48.     Procedure RefreshShape;
  49.   end;
  50.  
  51. implementation
  52.  
  53. {$R *.DFM}
  54. Uses UDemUtil,tecanvas;
  55.  
  56. procedure TStackedForm.FormCreate(Sender: TObject);
  57. var t:Longint;
  58. begin
  59.   { The Chart gradient is visible only with 16k Colors or greater video mode }
  60.   Chart1.Gradient.Visible:=Chart1.IsScreenHighColor;
  61.  
  62.   { Add some random points to the three bar Series }
  63.   Randomize;
  64.   with BarSeries1 do
  65.      for t:=1 to 12 do Add( Random(100),ShortMonthNames[t],clTeeColor);
  66.   with BarSeries2 do
  67.      for t:=1 to 12 do Add( Random(100),ShortMonthNames[t],clTeeColor);
  68.   with BarSeries3 do
  69.      for t:=1 to 12 do Add( Random(100),ShortMonthNames[t],clTeeColor);
  70.  
  71.   { Fill a ComboBox with the three Series titles }
  72.   ComboBox1.Items.Clear;
  73.   for t:=0 to Chart1.SeriesCount-1 do
  74.       ComboBox1.Items.Add(Chart1.Series[t].Name);
  75.   ComboBox1.ItemIndex:=0;
  76.  
  77.   RefreshShape;
  78. end;
  79.  
  80. Procedure TStackedForm.RefreshShape;
  81. Begin
  82.   { This method sets the Shape color to the active Series color in Combobox }
  83.   With Chart1.Series[ComboBox1.ItemIndex] do
  84.   Begin
  85.     Shape1.Visible:=not ColorEachPoint;
  86.     if Shape1.Visible then Shape1.Brush.Color:=SeriesColor;
  87.   end;
  88. end;
  89.  
  90. procedure TStackedForm.Button1Click(Sender: TObject);
  91. begin
  92.   { Scroll two Bar Series }
  93.   With Chart1 do
  94.   Begin
  95.     Series[SeriesCount-1].Marks.Visible:=False;  { hide marks }
  96.     SeriesDown(Series[0]);                       { move series 0 to back }
  97.     Series[SeriesCount-1].Marks.Visible:=True;   { show marks again }
  98.   end;
  99.   ComboBox1Change(Self);
  100. end;
  101.  
  102. procedure TStackedForm.ComboBox1Change(Sender: TObject);
  103. begin
  104.   { Change the active Bar Series }
  105.   With Chart1.Series[ComboBox1.ItemIndex] as TBarSeries do
  106.     RadioGroup2.Itemindex:=Ord(BarStyle);
  107.   RefreshShape;
  108. end;
  109.  
  110. procedure TStackedForm.RadioGroup2Click(Sender: TObject);
  111. begin
  112.   { Change the active Bar Series Style }
  113.   With Chart1.Series[ComboBox1.ItemIndex] as TBarSeries do
  114.        BarStyle:=TBarStyle(RadioGroup2.Itemindex);
  115. end;
  116.  
  117. procedure TStackedForm.CheckBox1Click(Sender: TObject);
  118. begin
  119.   { Start / Stop Animation }
  120.   Timer1.Enabled:=CheckBox1.Checked;
  121.   ComboBox2.Enabled:=not Timer1.Enabled;
  122.   RadioGroup2.Enabled:=not Timer1.Enabled;
  123.   if not Timer1.Enabled then ComboBox1Change(Self);
  124. end;
  125.  
  126. procedure TStackedForm.Timer1Timer(Sender: TObject);
  127.  
  128.   { this function returns a random color from ColorPalette }
  129.   Function RandomColor(CheckBack:Boolean):TColor;
  130.   Begin
  131.     Repeat
  132.       result:=ColorPalette[1+Random(MaxDefaultColors)];
  133.     Until (not CheckBack) or
  134.           ( (result<>Chart1.Color) and (result<>Chart1.BackColor) );
  135.   end;
  136.  
  137.   { this function returns a random angle from 0, 90, 180 or 270 }
  138.   Function RandomAngle:Integer;
  139.   Begin
  140.     Case Random(4) of
  141.       0: result:=0;
  142.       1: result:=90;
  143.       2: result:=180;
  144.     else
  145. {      3: } result:=270;
  146.     end;
  147.   end;
  148.  
  149.   { this function changes randomly the Pen parameter }
  150.   Procedure RandomPen(APen:TChartPen);
  151.   Begin
  152.     With APen do
  153.     if Visible then
  154.     Case Random(3) of
  155.       0: Color:=RandomColor(True);
  156.       1: Style:=TPenStyle(Random(5));
  157.       2: Width:=1+Random(3);
  158.     end;
  159.   end;
  160.  
  161.   { this function changes randomly the Brush parameter }
  162.   Procedure RandomBrush(ABrush:TBrush);
  163.   Begin
  164.     With ABrush do
  165.     Case Random(2) of
  166.       0: Color:=RandomColor(True);
  167.       1: if Random(10)<5 then Style:=TBrushStyle(Random(8));
  168.     end;
  169.   end;
  170.  
  171.   { this function changes randomly the Font parameter }
  172.   Procedure RandomFont(AFont:TFont);
  173.   Begin
  174.     With AFont do
  175.     Case Random(2) of
  176.       0: Color:=RandomColor(True);
  177.       1: if Random(2)=0 then
  178.             Size:=Size+1
  179.          else
  180.          if Size>7 then Size:=Size-1;
  181.     end;
  182.   End;
  183.  
  184. var tmpSeries:TBarSeries;
  185.     t:Longint;
  186. begin
  187.   { This long.... routine.....
  188.     is only a random generator for Chart and Series properties.
  189.   }
  190.   Timer1.Enabled:=False; { stop timer }
  191.  
  192.   { Set all Series.Active }
  193.   for t:=0 to Chart1.SeriesCount-1 do Chart1.Series[t].Active:=True;
  194.  
  195.   { Choose a Series randomly }
  196.   tmpSeries:=Chart1.Series[Random(ComboBox1.Items.Count)] as TBarSeries;
  197.  
  198.   { Then, lets change this chart a little.... }
  199.   Case Random(72) of
  200.     0: ComboBox2.ItemIndex:=Random(ComboBox2.Items.Count);
  201.     1,2,3,4,5: RadioGroup2.ItemIndex:=Random(RadioGroup2.Items.Count);
  202.     6: Button1Click(Self);
  203.     7: tmpSeries.SeriesColor:=RandomColor(False);
  204.     8: Chart1.Chart3dPercent:=5+Random(80);
  205.     9: Chart1.BackColor:=RandomColor(False);
  206.    10: if Chart1.Legend.Alignment=laRight then
  207.           Chart1.Legend.Alignment:=laLeft
  208.        else
  209.           Chart1.Legend.Alignment:=laRight;
  210.    11: RandomFont(Chart1.BottomAxis.LabelsFont);
  211.    12: RandomFont(Chart1.LeftAxis.LabelsFont);
  212.    13: Chart1.View3d:=not Chart1.View3d;
  213.    14: tmpSeries.BarWidthPercent:=50+Random(40);
  214.    15: RandomFont(tmpSeries.Marks.Font);
  215.    16: Chart1.BottomAxis.Grid.Visible:=not Chart1.BottomAxis.Grid.Visible;
  216.    17: Chart1.LeftAxis.Grid.Visible:=not Chart1.LeftAxis.Grid.Visible;
  217.    18: RandomFont(Chart1.LeftAxis.Title.Font);
  218.    19: RandomFont(Chart1.BottomAxis.Title.Font);
  219.    20: tmpSeries.Marks.Style:=TSeriesMarksStyle(Random(9));
  220.    21: With Chart1 do
  221.        Begin
  222.          BevelWidth:=1+Random(10);
  223.          MarginTop   :=TeeDefVerticalMargin+BevelWidth;
  224.          MarginLeft  :=TeeDefHorizMargin+BevelWidth;
  225.          MarginRight :=TeeDefHorizMargin+BevelWidth;
  226.          MarginBottom:=TeeDefVerticalMargin+BevelWidth;
  227.        end;
  228.    22: Chart1.Legend.Visible:=not Chart1.Legend.Visible;
  229.    23: if Random(10)<5 then Chart1.Legend.LegendStyle:=lsSeries
  230.                        else Chart1.Legend.LegendStyle:=lsValues;
  231.    24: tmpSeries.Active:=not tmpSeries.Active;
  232.    25: tmpSeries.Marks.BackColor:=RandomColor(True);
  233.    26: tmpSeries.Marks.Visible:=not tmpSeries.Marks.Visible;
  234.    27: RandomPen(Chart1.BottomAxis.Grid);
  235.    28: RandomPen(Chart1.LeftAxis.Grid);
  236.    29: Chart1.Legend.Frame.Visible:=not Chart1.Legend.Frame.Visible;
  237.    30: RandomPen(Chart1.Legend.Frame);
  238.    31: RandomPen(tmpSeries.BarPen);
  239.    32: tmpSeries.ColorEachPoint:=not tmpSeries.ColorEachPoint;
  240.    33: Chart1.Title.Alignment:=TAlignment(Random(3));
  241.    34: RandomFont(Chart1.Title.Font);
  242.    35: RandomFont(Chart1.Legend.Font);
  243.    36: Chart1.Legend.Color:=RandomColor(False);
  244.    37: Chart1.Legend.TextStyle:=TLegendTextStyle(Random(5));
  245.    38: Chart1.Legend.TopPos:=5+Random(90);
  246.    39: RandomPen(Chart1.BottomAxis.Axis);
  247.    40: RandomPen(Chart1.LeftAxis.Axis);
  248.    41: RandomPen(Chart1.BottomAxis.Ticks);
  249.    42: RandomPen(Chart1.LeftAxis.Ticks);
  250.    43: RandomPen(Chart1.BottomAxis.TicksInner);
  251.    44: RandomPen(Chart1.LeftAxis.TicksInner);
  252.    45: Chart1.BottomAxis.TickLength:=Random(8);
  253.    46: Chart1.LeftAxis.TickLength:=Random(8);
  254.    47: Chart1.BottomAxis.TickInnerLength:=Random(8);
  255.    48: Chart1.LeftAxis.TickInnerLength:=Random(8);
  256.    49: RandomBrush(tmpSeries.BarBrush);
  257.    50: Chart1.Title.Frame.Visible:=not Chart1.Title.Frame.Visible;
  258.    51: RandomPen(Chart1.Title.Frame);
  259.    52: Chart1.Foot.Frame.Visible:=not Chart1.Foot.Frame.Visible;
  260.    53: RandomPen(Chart1.Foot.Frame);
  261.    54: Chart1.Title.AdjustFrame:=not Chart1.Title.AdjustFrame;
  262.    55: Chart1.Foot.AdjustFrame:=not Chart1.Foot.AdjustFrame;
  263.    56: RandomBrush(Chart1.Title.Brush);
  264.    57: RandomBrush(Chart1.Foot.Brush);
  265.    58: Chart1.BottomAxis.MinorTickLength:=Random(8);
  266.    59: Chart1.LeftAxis.MinorTickLength:=Random(8);
  267.    60: RandomPen(Chart1.BottomAxis.MinorTicks);
  268.    61: RandomPen(Chart1.LeftAxis.MinorTicks);
  269.    62: RandomPen(Chart1.LeftWall.Pen);
  270.    63: RandomPen(Chart1.BottomWall.Pen);
  271.    64: Chart1.LeftWall.Color:=RandomColor(False);
  272.    65: Chart1.BottomWall.Color:=RandomColor(False);
  273.    66: Chart1.LeftAxis.MinorTickCount:=1+Random(6);
  274.    67: Chart1.BottomAxis.MinorTickCount:=1+Random(6);
  275.    68: Chart1.LeftAxis.Title.Angle:=RandomAngle;
  276.    69: Chart1.BottomAxis.Title.Angle:=RandomAngle;
  277.    70: Chart1.LeftAxis.LabelsAngle:=RandomAngle;
  278.    71: Chart1.BottomAxis.LabelsAngle:=RandomAngle;
  279.   end;
  280.   { re-start timer }
  281.   Timer1.Enabled:=True;
  282. end;
  283.  
  284. procedure TStackedForm.Shape1MouseUp(Sender: TObject; Button: TMouseButton;
  285.   Shift: TShiftState; X, Y: Integer);
  286. begin
  287.   { Run the Color dialog to change Series color }
  288.   With Chart1.Series[ComboBox1.ItemIndex] do
  289.        SeriesColor:=EditColor(Self,SeriesColor);
  290.   RefreshShape;
  291. end;
  292.  
  293. procedure TStackedForm.CheckBox2Click(Sender: TObject);
  294. begin
  295.   { Turn on / off Chart 3D }
  296.   Chart1.View3D:=CheckBox2.Checked;
  297. end;
  298.  
  299. procedure TStackedForm.ComboBox2Change(Sender: TObject);
  300. begin
  301.   { Four ways to plot bar Series: }
  302.   Case ComboBox2.ItemIndex of
  303.     0: BarSeries1.MultiBar:=mbNone;
  304.     1: BarSeries1.MultiBar:=mbSide;
  305.     2: BarSeries1.MultiBar:=mbStacked;
  306.     3: BarSeries1.MultiBar:=mbStacked100;
  307.   end;
  308. end;
  309.  
  310. end.
  311.