home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 October / Chip_2001-10_cd1.bin / zkuste / delphi / kompon / d123456 / CHEMPLOT.ZIP / TPlot / DBDemo / DBNormal1.pas < prev    next >
Pascal/Delphi Source File  |  2001-06-04  |  9KB  |  363 lines

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