home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 September / Chip_2001-09_cd1.bin / zkuste / delphi / kolekce / d12345 / CHEMPLOT.ZIP / TPlot / Demo / Normal1.pas < prev   
Pascal/Delphi Source File  |  2001-05-30  |  10KB  |  372 lines

  1. unit Normal1;
  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;
  20.  
  21. type
  22.   TMainForm = class(TForm)
  23.     Panel1: TPanel;
  24.     StringGrid1: TStringGrid;
  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.     procedure ClearAllBitBtnClick(Sender: TObject);
  44.     procedure GoBitBtnClick(Sender: TObject);
  45.     procedure FormCreate(Sender: TObject);
  46.     procedure PlotMenu1ExitMenuItemClick(Sender: TObject);
  47.     procedure NoisyBitBtnClick(Sender: TObject);
  48.     procedure GoCrazyBitBtnClick(Sender: TObject);
  49.     procedure CrazyTimerTimer(Sender: TObject);
  50.     procedure MyPlotFileOpen(Sender: TObject; TheFile: String);
  51.     procedure TraceBitBtnClick(Sender: TObject);
  52.     procedure TypeBitBtnClick(Sender: TObject);
  53.   private
  54.     MyPlot: TPlot;
  55.     MyPlotMenu: TPlotMenu;
  56.     MyPlotImageList: TPlotImageList;
  57.     MyPlotToolBar: TPlotToolBar;
  58.     Revolutions,
  59.     StartWidth,
  60.     StartHeight: Integer;
  61.     Angle,
  62.     AngleInc: Single;
  63.   public
  64.     { Public declarations }
  65.   end;
  66.  
  67. var
  68.   MainForm: TMainForm;
  69.  
  70. implementation
  71.  
  72. {$R *.dfm}
  73.  
  74. const
  75.   RADIUS = 50.0;
  76.  
  77. procedure TMainForm.FormCreate(Sender: TObject);
  78. begin
  79.   MyPlotImageList := TPlotImageList.Create(Self);
  80.  
  81.   MyPlot := TPlot.Create(Self);
  82.   MyPlot.Parent := Self;
  83.   MyPlot.Align := alClient;
  84.   MyPlot.Images := MyPlotImageList;
  85.   MyPlot.PlotType := ptXY;
  86.   MyPlot.NoSeries := 2;
  87.   MyPlot.MakeDummyData(20);
  88.  
  89.   MyPlotMenu := TPlotMenu.Create(Self);
  90.   MyPlotMenu.Images := MyPlotImageList;
  91.   MyPlotMenu.Plot := MyPlot;
  92.   MyPlotMenu.SetUpOnClicks;
  93.  
  94.   MyPlotToolBar := TPlotToolBar.Create(Self);
  95.   MyPlotToolBar.Parent := Self;
  96.   MyPlotToolBar.Images := MyPlotImageList;
  97.   MyPlotToolBar.Plot := MyPlot;
  98.  
  99.   StringGrid1.Cells[0, 0] := 'Test:';
  100.   StringGrid1.Cells[0, 1] := 'Score:';
  101.   StringGrid1.ColWidths[0] := 60;
  102.  
  103. {$IFDEF WIN32}
  104.   Self.Caption := 'TPlot demo for Delphi';
  105. {$ENDIF}
  106. {$IFDEF LINUX}
  107.   Self.Caption := 'TPlot demo for Kylix';
  108. {$ENDIF}
  109. end;
  110.  
  111. procedure TMainForm.ClearAllBitBtnClick(Sender: TObject);
  112. var
  113.   i: Integer;
  114. begin
  115.   MyPlot.SeriesList.ClearSeries;
  116.   for i := 1 to StringGrid1.ColCount-1 do
  117.   begin
  118.     StringGrid1.Cells[i, 0] := '';
  119.     StringGrid1.Cells[i, 1] := '';
  120.   end;
  121. end;
  122.  
  123. procedure TMainForm.GoBitBtnClick(Sender: TObject);
  124. var
  125.   X, Y: Single;
  126.   Mean: Single;
  127.   StdDev: Single;
  128.   Min: Single;
  129.   Max: Single;
  130.   StepSize: Single;
  131. {$IFDEF MSWINDOWS}
  132.   FStartTime:          Int64; {TLargeInteger;}
  133.   FFinishTime:         Int64;
  134.   FFrequency:          Int64;
  135. {$ENDIF}
  136. {$IFDEF LINUX}
  137.   FStartTime:          TDateTime;
  138.   FFinishTime:         TDateTime;
  139. {$ENDIF}
  140.   ElapsedTime: Double;
  141. begin
  142.   MyPlot.NoSeries := 1;
  143.   MyPlot.Series[0].DelData;
  144.  
  145.   Screen.Cursor := crHourGlass;
  146. {$IFDEF MSWINDOWS}
  147.   QueryPerformanceFrequency(FFrequency); {counts per second}
  148. {get the starting time:}
  149.   QueryPerformanceCounter(FStartTime); { LARGE_INTEGER}
  150. {$ENDIF}
  151. {$IFDEF LINUX}
  152.   FStartTime := Time;
  153. {$ENDIF}
  154.  
  155.   Mean := MeanNEdit.AsReal;
  156.   StdDev := StdDevNEdit.AsReal;
  157.   Min := MinNEdit.AsReal;
  158.   Max := MaxNEdit.AsReal;
  159.   StepSize := StepSizeNEdit.AsReal;
  160.  
  161. {Set the axes:}
  162.   MyPlot.XAxis.Max := Max;
  163.   MyPlot.XAxis.Min := Min;
  164.   MyPlot.YAxis.Min := 0;
  165.   MyPlot.XAxis.Intercept := 0;
  166.  
  167.   X := Min;
  168.   while (X <= Max) do
  169.   begin
  170.     Y := Exp(-Sqr((X-Mean)/(2*StdDev))) /
  171.       Sqrt(2*Pi*StdDev);
  172. {Don't fire any events, and don't adjust axes:}
  173.     MyPlot[0].AddPoint(X, Y, FALSE, FALSE);
  174.     X := X + StepSize;
  175.   end;
  176.   MyPlot[0].Visible := TRUE;
  177.  
  178.   {MyPlot.YAxis.Min := MyPlot.Series[0].YMin;}
  179.   MyPlot.YAxis.Max := MyPlot.Series[0].YMax;
  180.  
  181.   {for i := 1 to StringGrid1.ColCount-1 do
  182.   begin
  183.     if (Length(StringGrid1.Cells[i, 0]) > 0) then
  184.     begin
  185.       if (Length(StringGrid1.Cells[i, 1]) > 0) then
  186.       begin
  187.         try
  188.           TheScore := StrToFloat(StringGrid1.Cells[i, 1]);
  189.           TheSeries := MyPlot.Add(-1);
  190.           MyPlot[TheSeries].Name := StringGrid1.Cells[i, 0];
  191.           MyPlot[TheSeries].AddPoint(TheScore, MyPlot.YAxis.Min, TRUE, TRUE);
  192.           MyPlot[TheSeries].AddPoint(TheScore, MyPlot.YAxis.Max, TRUE, TRUE);
  193.           MyPlot[TheSeries].Visible := TRUE;
  194.           MyPlot[TheSeries].Symbol := TSymbol(i mod (1+Ord(sDownTriangle)));
  195.         finally
  196.         end;
  197.       end;
  198.     end;
  199.   end;}
  200. {get the finishing time:}
  201. {$IFDEF MSWINDOWS}
  202.   QueryPerformanceCounter(FFinishTime); { LARGE_INTEGER}
  203. {take difference and convert to ms:}
  204.   ElapsedTime := 1000 * (FFinishTime - FStartTime) / FFrequency;
  205. {$ENDIF}
  206. {$IFDEF LINUX}
  207.   FFinishTime := Time;
  208.   ElapsedTime := 1000 * (FFinishTime - FStartTime);
  209. {$ENDIF}
  210.   StatusBar1.SimpleText := Format('Drawing %d points takes %g ms',
  211.     [MyPlot[0].NoPts, ElapsedTime]);
  212.  
  213.   Screen.Cursor := crDefault;
  214. end;
  215.  
  216. procedure TMainForm.PlotMenu1ExitMenuItemClick(Sender: TObject);
  217. begin
  218.   Close;
  219. end;
  220.  
  221. procedure TMainForm.NoisyBitBtnClick(Sender: TObject);
  222. begin
  223.   MyPlot.MakeDummyData(100);
  224. end;
  225.  
  226. procedure TMainForm.GoCrazyBitBtnClick(Sender: TObject);
  227. begin
  228.   if (CrazyTimer.Enabled) then
  229.   begin
  230.     CrazyTimer.Enabled := FALSE;
  231.     GoCrazyBitBtn.Caption := 'Go Crazy';
  232.     TraceBitBtn.Enabled := TRUE;
  233.   end
  234.   else
  235.   begin
  236.     Revolutions := 0;
  237.     StartWidth := Width;
  238.     StartHeight := Height;
  239.     Angle := 0;
  240.     AngleInc := 4 * Pi / 180;
  241.     GoCrazyBitBtn.Caption := 'Enough !';
  242.     CrazyTimer.Enabled := TRUE;
  243.     TraceBitBtn.Enabled := FALSE;
  244.   end;
  245. end;
  246.  
  247. procedure TMainForm.CrazyTimerTimer(Sender: TObject);
  248. begin
  249.   if (MyPlot.PlotType >= pt3DContour) then
  250.   begin
  251.     MyPlot.ZAngle := MyPlot.ZAngle + 1;
  252.   end
  253.   else
  254.   begin
  255.     Width := StartWidth + Round(RADIUS * Sin(Angle));
  256.     Height := StartHeight + Round(RADIUS * Cos(Angle));
  257.     Angle := Angle + AngleInc;
  258.     Inc(Revolutions);
  259.     StatusBar1.SimpleText := IntToStr(Revolutions);
  260.   end;
  261. end;
  262.  
  263. procedure TMainForm.MyPlotFileOpen(Sender: TObject; TheFile: String);
  264. var
  265.   TheTitle: String;
  266. begin
  267.   TheTitle := ExtractFileName(Application.ExeName);
  268.   TheTitle := Copy(TheTitle, 1, Length(TheTitle)-4);
  269.   TheTitle := TheTitle + ' - ' + ExtractFileName(TheFile);
  270.   Application.Title := TheTitle;
  271.   MainForm.Caption := TheTitle;
  272. end;
  273.  
  274. procedure TMainForm.TraceBitBtnClick(Sender: TObject);
  275. begin
  276.   MyPlot.Trace;
  277. end;
  278.  
  279. procedure TMainForm.TypeBitBtnClick(Sender: TObject);
  280. var
  281.  ThePlotType: Integer;
  282.  TheXStringData: TStringList;
  283. begin
  284.   ThePlotType := Ord(MyPlot.PlotType);
  285.   ThePlotType := (ThePlotType+1) mod (Ord(High(TPlotType))+1);
  286.   MyPlot.SeriesList.ClearSeries;
  287.   MyPlot.PlotType := TPlotType(ThePlotType);
  288.   MyPlot.Title.Caption := 'TPlot - ' + #10 +
  289.     Copy(TypInfo.GetEnumName(TypeInfo(TPlotType), ThePlotType), 3, 99);
  290.   case MyPlot.PlotType of
  291.     ptXY:
  292.       begin
  293.         MyPlot.MakeDummyData(100);
  294.         MyPlot.Border.Left := 70;
  295.         MyPlot.Border.BottomGap := 80;
  296.       end;
  297.     ptError:
  298.       begin
  299.         MyPlot.MakeDummyData(20);
  300.         MyPlot.Series[0].Symbol := syCircle;
  301.       end;
  302.     ptMultiple:
  303.       begin
  304.         MyPlot.NoSeries := 4;
  305.         MyPlot.Multiplicity := 4;
  306.         MyPlot.MakeDummyData(20);
  307.         MyPlot.Series[0].Pen.Width := 0;
  308.         MyPlot.Series[1].Pen.Width := 0;
  309.         MyPlot.Series[2].Pen.Width := 0;
  310.         MyPlot.Series[3].Pen.Width := 0;
  311.         MyPlot.Series[0].Name := 'High';
  312.         MyPlot.Series[1].Name := 'Low';
  313.         MyPlot.Series[2].Name := 'Open';
  314.         MyPlot.Series[3].Name := 'Close';
  315.         MyPlot.Series[0].Symbol := syLeftDash;
  316.         MyPlot.Series[1].Symbol := syRightDash;
  317.         MyPlot.MultiJoin := '2,3';
  318.       end;
  319.     ptBubble:
  320.       begin
  321.         MyPlot.MakeDummyData(20);
  322.       end;
  323.     ptColumn, ptStack, ptNormStack: MyPlot.MakeDummyData(10);
  324.     ptPie:
  325.       begin
  326.         MyPlot.MakeDummyData(10);
  327.         TheXStringData := TStringList.Create;
  328.         TheXStringData.Add('Alpha');
  329.         TheXStringData.Add('Bravo');
  330.         TheXStringData.Add('Charlie');
  331.         TheXStringData.Add('Delta');
  332.         TheXStringData.Add('Echo');
  333.         TheXStringData.Add('Foxtrot');
  334.         TheXStringData.Add('Golf');
  335.         TheXStringData.Add('Hotel');
  336.         TheXStringData.Add('India');
  337.         TheXStringData.Add('Juliet');
  338.         TheXStringData.Add('Kilo');
  339.         MyPlot.Series[0].XStringData := TheXStringData;
  340.         MyPlot.Series[1].XStringData := TheXStringData;
  341.         TheXStringData.Free;
  342.       end;
  343.     ptPolar:
  344.       begin
  345.         MyPlot.XAxis.Min := -10;
  346.         MyPlot.XAxis.Max := 10;
  347.         MyPlot.YAxis.Min := -10;
  348.         MyPlot.YAxis.Max := 10;
  349.         MyPlot.MakeDummyData(20);
  350.         MyPlot.XAxis.Intercept := 0;
  351.         MyPlot.YAxis.Intercept := 0;
  352.       end;
  353.     ptContour, pt3DContour, pt3DWire:
  354.       begin
  355.         if (MyPlot.PlotType > ptContour) then
  356.         begin
  357.           MyPlot.Border.Left := 120;
  358.           MyPlot.Border.BottomGap := 130;
  359.         end;
  360.         MyPlot.XAxis.Min := 0;
  361.         MyPlot.XAxis.Max := 10;
  362.         MyPlot.YAxis.Min := 0;
  363.         MyPlot.YAxis.Max := 10;
  364.         MyPlot.NoSeries := 10;
  365.         MyPlot.MakeDummyData(20);
  366.       end;
  367.   else ;
  368.   end;
  369. end;
  370.  
  371. end.
  372.