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

  1. {*********************************************}
  2. {  TeeChart Moving Bars example               }
  3. {  Copyright (c) 1995-1998 by David Berneda   }
  4. {  All rights reserved                        }
  5. {*********************************************}
  6. unit UMoveBar;
  7. {$P-}  { <-- this is for Delphi 1.0 compatibility }
  8.  
  9. interface
  10.  
  11. uses
  12.   WinTypes,WinProcs, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  13.   TeEngine, Series, ExtCtrls, TeeProcs, Chart, StdCtrls;
  14.  
  15. { TeeChart Pro.
  16.   Example showing the Bar series BarWidthPercent and CustomBarWidth
  17.   properties.
  18.  
  19.   Also showing how to allow user run-time moving of Bars, both for
  20.   X and Y directions.
  21. }
  22. type
  23.   TFormMoveBars = class(TForm)
  24.     Chart1: TChart;
  25.     Series1: TBarSeries;
  26.     CheckBox1: TCheckBox;
  27.     Label1: TLabel;
  28.     ScrollBar1: TScrollBar;
  29.     Label2: TLabel;
  30.     Label3: TLabel;
  31.     ScrollBar2: TScrollBar;
  32.     Label4: TLabel;
  33.     Series2: TFastLineSeries;
  34.     Button1: TButton;
  35.     CheckBox2: TCheckBox;
  36.     Label5: TLabel;
  37.     Label6: TLabel;
  38.     procedure FormCreate(Sender: TObject);
  39.     procedure CheckBox1Click(Sender: TObject);
  40.     procedure ScrollBar1Change(Sender: TObject);
  41.     procedure Chart1GetAxisLabel(Sender: TChartAxis; Series: TChartSeries;
  42.       ValueIndex: Longint; var LabelText: String);
  43.     procedure ScrollBar2Change(Sender: TObject);
  44.     procedure Button1Click(Sender: TObject);
  45.     procedure CheckBox2Click(Sender: TObject);
  46.     procedure Chart1MouseDown(Sender: TObject; Button: TMouseButton;
  47.       Shift: TShiftState; X, Y: Integer);
  48.     procedure Chart1MouseUp(Sender: TObject; Button: TMouseButton;
  49.       Shift: TShiftState; X, Y: Integer);
  50.     procedure Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
  51.       Y: Integer);
  52.   private
  53.     { Private declarations }
  54.   public
  55.     { Public declarations }
  56.     tmpBar:Integer;
  57.   end;
  58.  
  59. implementation
  60.  
  61. {$R *.DFM}
  62.  
  63. procedure TFormMoveBars.FormCreate(Sender: TObject);
  64. begin
  65.  { fill the Bar series with sample values specifying X coordinates }
  66.   With Series1 do
  67.   begin
  68.     Clear;
  69.     AddXY( 0, 100, '', clTeeColor);
  70.     AddXY( 3, 200, '', clTeeColor);
  71.     AddXY( 7, 300, '', clTeeColor);
  72.     AddXY( 11, 400, '', clTeeColor);
  73.     AddXY( 15, 500, '', clTeeColor);
  74.     AddXY( 22, 600, '', clTeeColor);
  75.     AddXY( 30, 700, '', clTeeColor);
  76.  
  77.     ColorEachPoint:=True;
  78.   end;
  79.  
  80.  { fill the Line series with sample values specifying X coordinates }
  81.   With Series2 do
  82.   begin
  83.     Clear;
  84.     AddXY( 0, 100, '', clTeeColor);
  85.     AddXY( 3, 200, '', clTeeColor);
  86.     AddXY( 7, 300, '', clTeeColor);
  87.     AddXY( 11, 400, '', clTeeColor);
  88.     AddXY( 15, 500, '', clTeeColor);
  89.     AddXY( 22, 600, '', clTeeColor);
  90.     AddXY( 30, 700, '', clTeeColor);
  91.   end;
  92.  
  93.   { change the bottom axis (the X axis) labelling increment }
  94.   With Chart1.BottomAxis do
  95.   begin
  96.     LabelStyle:=talValue;
  97.     Increment:=1;
  98.     LabelsSeparation:=0;
  99.   end;
  100.  
  101.   { initialize the scrollbars }
  102.   ScrollBar1.Position:=Series1.BarWidthPercent;
  103.   ScrollBar2.Position:=Series1.CustomBarWidth;
  104.  
  105.   tmpBar:=-1;
  106. end;
  107.  
  108. procedure TFormMoveBars.CheckBox1Click(Sender: TObject);
  109. begin
  110.   Series1.SideMargins:=CheckBox1.Checked;
  111. end;
  112.  
  113. procedure TFormMoveBars.ScrollBar1Change(Sender: TObject);
  114. begin
  115.   { Call ScrollBar2Change to Set the CustomBarWidth property to Zero }
  116.   ScrollBar2.Position:=0;
  117.   ScrollBar2Change(Self);
  118.  
  119.   { change the "BarWidthPercent" property... }
  120.   { BarWidth percent is allowed only then CustomBarWidth is zero (auto width) }
  121.   Series1.BarWidthPercent:=ScrollBar1.Position;
  122.   Label2.Caption:=IntToStr(ScrollBar1.Position)+'%';
  123. end;
  124.  
  125. procedure TFormMoveBars.Chart1GetAxisLabel(Sender: TChartAxis;
  126.   Series: TChartSeries; ValueIndex: Longint; var LabelText: String);
  127. var t:Integer;
  128. begin
  129.   if Sender=Chart1.BottomAxis then
  130.   With Chart1.BottomAxis do
  131.   begin
  132.     LabelsFont.Style:=[];
  133.  
  134.     { draw some axis labels with special Font... }
  135.     for t:=0 to Series1.Count-1 do
  136.       if LabelText=FormatFloat(AxisValuesFormat,Series1.XValues[t]) then
  137.       begin
  138.         LabelsFont.Style:=[fsBold];
  139.         break;
  140.       end;
  141.   end;
  142. end;
  143.  
  144. procedure TFormMoveBars.ScrollBar2Change(Sender: TObject);
  145. begin
  146.   { change the "CustomBarWidth" property... }
  147.   Series1.CustomBarWidth:=ScrollBar2.Position;
  148.  
  149.   if ScrollBar2.Position=0 then
  150.      Label4.Caption:='(auto)'
  151.   else
  152.      Label4.Caption:=IntToStr(ScrollBar2.Position)+' pixels';
  153. end;
  154.  
  155. procedure TFormMoveBars.Button1Click(Sender: TObject);
  156. begin
  157.   Close;
  158. end;
  159.  
  160. procedure TFormMoveBars.CheckBox2Click(Sender: TObject);
  161. begin
  162.   Chart1.View3D:=CheckBox2.Checked;
  163. end;
  164.  
  165. procedure TFormMoveBars.Chart1MouseDown(Sender: TObject; Button: TMouseButton;
  166.   Shift: TShiftState; X, Y: Integer);
  167. begin
  168.   { calculate if user clicked on a Bar... }
  169.   if Button=mbLeft then tmpBar:=Series1.Clicked(x,y);
  170. end;
  171.  
  172. procedure TFormMoveBars.Chart1MouseUp(Sender: TObject; Button: TMouseButton;
  173.   Shift: TShiftState; X, Y: Integer);
  174. begin
  175.   { when left mouse button is depressed, forget about moving bars... }
  176.   tmpBar:=-1;
  177. end;
  178.  
  179. procedure TFormMoveBars.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
  180.   Y: Integer);
  181. begin
  182.   if tmpBar<>-1 then  { if moving a bar, change X and Y values... }
  183.   With Series1 do
  184.   begin
  185.     XValues[tmpBar]:=XScreenToValue(X);;
  186.     YValues[tmpBar]:=YScreenToValue(Y);
  187.  
  188.     { force repaint to see the bar moving... }
  189.     Repaint;
  190.   end;
  191. end;
  192.  
  193. end.
  194.