home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 September / Chip_2001-09_cd1.bin / zkuste / delphi / kolekce / d12345 / CHEMPLOT.ZIP / TPlot / DBDemo / DBNormal1.pas < prev    next >
Pascal/Delphi Source File  |  2001-05-29  |  9KB  |  362 lines

  1. unit DBNormal1;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, Classes, TypInfo,
  7. {$IFDEF WIN32}
  8.   Windows, Messages, Graphics, Controls, Forms, Dialogs,
  9.   ImgList, Menus, Grids, ExtCtrls, StdCtrls,
  10.   Buttons, ToolWin, ComCtrls,
  11. {$ENDIF}
  12. {$IFDEF LINUX}
  13.   Qt, QTypes,
  14.   QGraphics, QControls, QForms, QDialogs,
  15.   QImgList, QMenus, QGrids, QExtCtrls, QStdCtrls,
  16.   QButtons, QComCtrls,
  17. {$ENDIF}
  18.  
  19.   Plot, Plotdefs, Plotmenu, Plotimagelist, Data, Plottoolbar, Nedit, Db,
  20.   DBTables, DBPlot, DBCtrls, Mask, ActnList;
  21.  
  22. type
  23.   TMainForm = class(TForm)
  24.     Panel1: TPanel;
  25.     MinNEdit: TNEdit;
  26.     Label1: TLabel;
  27.     Label2: TLabel;
  28.     MaxNEdit: TNEdit;
  29.     Label3: TLabel;
  30.     StepSizeNEdit: TNEdit;
  31.     Label4: TLabel;
  32.     MeanNEdit: TNEdit;
  33.     Label5: TLabel;
  34.     StdDevNEdit: TNEdit;
  35.     GoBitBtn: TBitBtn;
  36.     GoCrazyBitBtn: TBitBtn;
  37.     CrazyTimer: TTimer;
  38.     ClearAllBitBtn: TBitBtn;
  39.     NoisyBitBtn: TBitBtn;
  40.     TraceBitBtn: TBitBtn;
  41.     TypeBitBtn: TBitBtn;
  42.     StatusBar1: TStatusBar;
  43.     Panel2: TPanel;
  44.     DBNavigator1: TDBNavigator;
  45.     Table1: TTable;
  46.     DataSource1: TDataSource;
  47.     TitleDBEdit: TDBEdit;
  48.     DateDBEdit: TDBEdit;
  49.     ActionList1: TActionList;
  50.     GoAction: TAction;
  51.     NoisyAction: TAction;
  52.     ClearAllAction: TAction;
  53.     TypeAction: TAction;
  54.     procedure ClearAllBitBtnClick(Sender: TObject);
  55.     procedure GoBitBtnClick(Sender: TObject);
  56.     procedure FormCreate(Sender: TObject);
  57.     procedure PlotMenu1ExitMenuItemClick(Sender: TObject);
  58.     procedure NoisyBitBtnClick(Sender: TObject);
  59.     procedure GoCrazyBitBtnClick(Sender: TObject);
  60.     procedure CrazyTimerTimer(Sender: TObject);
  61.     procedure MyPlotFileOpen(Sender: TObject; TheFile: String);
  62.     procedure TraceBitBtnClick(Sender: TObject);
  63.     procedure TypeBitBtnClick(Sender: TObject);
  64.     procedure DBNavigator1BeforeAction(Sender: TObject;
  65.       Button: TNavigateBtn);
  66.     procedure GoActionUpdate(Sender: TObject);
  67.   private
  68.     MyPlot: TDBPlot;
  69.     MyPlotMenu: TPlotMenu;
  70.     MyPlotImageList: TPlotImageList;
  71.     MyPlotToolBar: TPlotToolBar;
  72.     Revolutions,
  73.     StartWidth,
  74.     StartHeight: Integer;
  75.     Angle,
  76.     AngleInc: Single;
  77.   public
  78.     { Public declarations }
  79.   end;
  80.  
  81. var
  82.   MainForm: TMainForm;
  83.  
  84. implementation
  85.  
  86. {$R *.dfm}
  87.  
  88. const
  89.   RADIUS = 50.0;
  90.  
  91. procedure TMainForm.FormCreate(Sender: TObject);
  92. begin
  93.   MyPlotImageList := TPlotImageList.Create(Self);
  94.  
  95.   MyPlot := TDBPlot.Create(Self);
  96.   MyPlot.Parent := Self;
  97.   MyPlot.Align := alClient;
  98.   MyPlot.Images := MyPlotImageList;
  99.   MyPlot.DataSource := DataSource1;
  100.   MyPlot.DataField := 'Plot';
  101.   MyPlot.PlotType := ptXY;
  102.  
  103.   MyPlotMenu := TPlotMenu.Create(Self);
  104.   MyPlotMenu.Images := MyPlotImageList;
  105.   MyPlotMenu.Plot := TPlot(MyPlot);
  106.   MyPlotMenu.SetUpOnClicks;
  107.  
  108.   MyPlotToolBar := TPlotToolBar.Create(Self);
  109.   MyPlotToolBar.Parent := Self;
  110.   MyPlotToolBar.Images := MyPlotImageList;
  111.   MyPlotToolBar.Plot := TPlot(MyPlot);
  112.  
  113.   Table1.Active := TRUE;
  114.  
  115. {$IFDEF WIN32}
  116.   Self.Caption := 'TDBPlot demo for Delphi';
  117. {$ENDIF}
  118. {$IFDEF LINUX}
  119.   Self.Caption := 'TDBPlot demo for Kylix';
  120. {$ENDIF}
  121. end;
  122.  
  123. procedure TMainForm.ClearAllBitBtnClick(Sender: TObject);
  124. begin
  125.   MyPlot.Clear(FALSE);
  126. end;
  127.  
  128. procedure TMainForm.GoBitBtnClick(Sender: TObject);
  129. var
  130.   X, Y: Single;
  131.   Mean: Single;
  132.   StdDev: Single;
  133.   Min: Single;
  134.   Max: Single;
  135.   StepSize: Single;
  136. {$IFDEF MSWINDOWS}
  137.   FStartTime:          Int64; {TLargeInteger;}
  138.   FFinishTime:         Int64;
  139.   FFrequency:          Int64;
  140. {$ENDIF}
  141. {$IFDEF LINUX}
  142.   FStartTime:          TDateTime;
  143.   FFinishTime:         TDateTime;
  144. {$ENDIF}
  145.   ElapsedTime: Double;
  146. begin
  147.   MyPlot.NoSeries := 1;
  148.   MyPlot.Series[0].DelData;
  149.  
  150.   Screen.Cursor := crHourGlass;
  151. {$IFDEF MSWINDOWS}
  152.   QueryPerformanceFrequency(FFrequency); {counts per second}
  153. {get the starting time:}
  154.   QueryPerformanceCounter(FStartTime); { LARGE_INTEGER}
  155. {$ENDIF}
  156. {$IFDEF LINUX}
  157.   FStartTime := Time;
  158. {$ENDIF}
  159.  
  160.   Mean := MeanNEdit.AsReal;
  161.   StdDev := StdDevNEdit.AsReal;
  162.   Min := MinNEdit.AsReal;
  163.   Max := MaxNEdit.AsReal;
  164.   StepSize := StepSizeNEdit.AsReal;
  165.  
  166. {Set the axes:}
  167.   MyPlot.XAxis.Max := Max;
  168.   MyPlot.XAxis.Min := Min;
  169.   MyPlot.YAxis.Min := 0;
  170.   MyPlot.XAxis.Intercept := 0;
  171.  
  172.   X := Min;
  173.   while (X <= Max) do
  174.   begin
  175.     Y := Exp(-Sqr((X-Mean)/(2*StdDev))) /
  176.       Sqrt(2*Pi*StdDev);
  177. {Don't fire any events, and don't adjust axes:}
  178.     MyPlot[0].AddPoint(X, Y, FALSE, FALSE);
  179.     X := X + StepSize;
  180.   end;
  181.   MyPlot[0].Visible := TRUE;
  182.  
  183.   {MyPlot.YAxis.Min := MyPlot.Series[0].YMin;}
  184.   MyPlot.YAxis.Max := MyPlot.Series[0].YMax;
  185.  
  186.   {for i := 1 to StringGrid1.ColCount-1 do
  187.   begin
  188.     if (Length(StringGrid1.Cells[i, 0]) > 0) then
  189.     begin
  190.       if (Length(StringGrid1.Cells[i, 1]) > 0) then
  191.       begin
  192.         try
  193.           TheScore := StrToFloat(StringGrid1.Cells[i, 1]);
  194.           TheSeries := MyPlot.Add(-1);
  195.           MyPlot[TheSeries].Name := StringGrid1.Cells[i, 0];
  196.           MyPlot[TheSeries].AddPoint(TheScore, MyPlot.YAxis.Min, TRUE, TRUE);
  197.           MyPlot[TheSeries].AddPoint(TheScore, MyPlot.YAxis.Max, TRUE, TRUE);
  198.           MyPlot[TheSeries].Visible := TRUE;
  199.           MyPlot[TheSeries].Symbol := TSymbol(i mod (1+Ord(sDownTriangle)));
  200.         finally
  201.         end;
  202.       end;
  203.     end;
  204.   end;}
  205. {get the finishing time:}
  206. {$IFDEF MSWINDOWS}
  207.   QueryPerformanceCounter(FFinishTime); { LARGE_INTEGER}
  208. {take difference and convert to ms:}
  209.   ElapsedTime := 1000 * (FFinishTime - FStartTime) / FFrequency;
  210. {$ENDIF}
  211. {$IFDEF LINUX}
  212.   FFinishTime := Time;
  213.   ElapsedTime := 1000 * (FFinishTime - FStartTime);
  214. {$ENDIF}
  215.   StatusBar1.SimpleText := Format('Drawing %d points takes %g ms',
  216.     [MyPlot[0].NoPts, ElapsedTime]);
  217.  
  218.   Screen.Cursor := crDefault;
  219.   
  220. end;
  221.  
  222. procedure TMainForm.PlotMenu1ExitMenuItemClick(Sender: TObject);
  223. begin
  224.   Close;
  225. end;
  226.  
  227. procedure TMainForm.NoisyBitBtnClick(Sender: TObject);
  228. begin
  229.   MyPlot.MakeDummyData(100);
  230. end;
  231.  
  232. procedure TMainForm.GoCrazyBitBtnClick(Sender: TObject);
  233. begin
  234.   if (CrazyTimer.Enabled) then
  235.   begin
  236.     CrazyTimer.Enabled := FALSE;
  237.     GoCrazyBitBtn.Caption := 'Go Crazy';
  238.     TraceBitBtn.Enabled := TRUE;
  239.   end
  240.   else
  241.   begin
  242.     Revolutions := 0;
  243.     StartWidth := Width;
  244.     StartHeight := Height;
  245.     Angle := 0;
  246.     AngleInc := 4 * Pi / 180;
  247.     GoCrazyBitBtn.Caption := 'Enough !';
  248.     CrazyTimer.Enabled := TRUE;
  249.     TraceBitBtn.Enabled := FALSE;
  250.   end;
  251. end;
  252.  
  253. procedure TMainForm.CrazyTimerTimer(Sender: TObject);
  254. begin
  255.   if (MyPlot.PlotType >= pt3DContour) then
  256.   begin
  257.     MyPlot.ZAngle := MyPlot.ZAngle + 1;
  258.   end
  259.   else
  260.   begin
  261.     Width := StartWidth + Round(RADIUS * Sin(Angle));
  262.     Height := StartHeight + Round(RADIUS * Cos(Angle));
  263.     Angle := Angle + AngleInc;
  264.     Inc(Revolutions);
  265.     StatusBar1.SimpleText := IntToStr(Revolutions);
  266.   end;
  267. end;
  268.  
  269. procedure TMainForm.MyPlotFileOpen(Sender: TObject; TheFile: String);
  270. var
  271.   TheTitle: String;
  272. begin
  273.   TheTitle := ExtractFileName(Application.ExeName);
  274.   TheTitle := Copy(TheTitle, 1, Length(TheTitle)-4);
  275.   TheTitle := TheTitle + ' - ' + ExtractFileName(TheFile);
  276.   Application.Title := TheTitle;
  277.   MainForm.Caption := TheTitle;
  278. end;
  279.  
  280. procedure TMainForm.TraceBitBtnClick(Sender: TObject);
  281. begin
  282.   MyPlot.Trace;
  283. end;
  284.  
  285. procedure TMainForm.TypeBitBtnClick(Sender: TObject);
  286. var
  287.  i,
  288.  ThePlotType: Integer;
  289.  TheXStringData: TStringList;
  290. begin
  291.   ThePlotType := Ord(MyPlot.PlotType);
  292.   ThePlotType := (ThePlotType+1) mod (Ord(High(TPlotType))+1);
  293.   MyPlot.PlotType := TPlotType(ThePlotType);
  294.   MyPlot.Title.Caption := 'TPlot - ' + #10 +
  295.     Copy(TypInfo.GetEnumName(TypeInfo(TPlotType), ThePlotType), 3, 99);
  296.   case MyPlot.PlotType of
  297.     ptXY:
  298.       begin
  299.         MyPlot.Border.Left := 70;
  300.         MyPlot.Border.BottomGap := 80;
  301.       end;
  302.     ptError:
  303.       begin
  304.         MyPlot.Series[0].Symbol := syCircle;
  305.       end;
  306.     ptMultiple:
  307.       begin
  308.         for i := 0 to MyPlot.NoSeries-1 do
  309.         begin
  310.           MyPlot.Series[i].Pen.Width := 0;
  311.         end;
  312.       end;
  313.     ptBubble:
  314.       begin
  315.       end;
  316.     ptColumn, ptStack, ptNormStack: ;
  317.     ptPie:
  318.       begin
  319.       end;
  320.     ptPolar:
  321.       begin
  322.         MyPlot.XAxis.Min := -10;
  323.         MyPlot.XAxis.Max := 10;
  324.         MyPlot.YAxis.Min := -10;
  325.         MyPlot.YAxis.Max := 10;
  326.         MyPlot.XAxis.Intercept := 0;
  327.         MyPlot.YAxis.Intercept := 0;
  328.       end;
  329.     ptContour, pt3DContour, pt3DWire:
  330.       begin
  331.         if (MyPlot.PlotType > ptContour) then
  332.         begin
  333.           MyPlot.Border.Left := 120;
  334.           MyPlot.Border.BottomGap := 130;
  335.         end;
  336.         MyPlot.XAxis.Min := 0;
  337.         MyPlot.XAxis.Max := 10;
  338.         MyPlot.YAxis.Min := 0;
  339.         MyPlot.YAxis.Max := 10;
  340.       end;
  341.   else ;
  342.   end;
  343. end;
  344.  
  345. procedure TMainForm.DBNavigator1BeforeAction(Sender: TObject;
  346.   Button: TNavigateBtn);
  347. begin
  348.   if ((Button = nbInsert) or
  349.       (Button = nbEdit)) then
  350.   begin
  351.     DateDBEdit.Text := DateToStr(Now);
  352.     TitleDBEdit.Text := MyPlot.Title.Caption;
  353.   end;
  354. end;
  355.  
  356. procedure TMainForm.GoActionUpdate(Sender: TObject);
  357. begin
  358.   (Sender as TAction).Enabled := not MyPlot.ReadOnly;
  359. end;
  360.  
  361. end.
  362.