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