home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 October / Chip_2001-10_cd1.bin / zkuste / delphi / kompon / d123456 / CHEMPLOT.ZIP / TPlot / Demo / Normal1.pas < prev    next >
Pascal/Delphi Source File  |  2001-07-26  |  15KB  |  510 lines

  1. unit Normal1;
  2.  
  3. {$I Chemware.inc}
  4.  
  5. {.$ DEFINE JUST_THE_GRAPH}
  6.  
  7. interface
  8.  
  9. uses
  10.   SysUtils, Classes, TypInfo,
  11. {$IFDEF WIN32}
  12.   Windows, Messages, Graphics, Controls, Forms, Dialogs,
  13.   ImgList, Menus, Grids, ExtCtrls, StdCtrls,
  14.   Buttons, ToolWin, ComCtrls,
  15. {$ENDIF}
  16. {$IFDEF LINUX}
  17.   Types, //Untranslated,
  18.   Qt, QTypes,
  19.   QGraphics, QControls, QForms, QDialogs,
  20.   QImgList, QMenus, QGrids, QExtCtrls, QStdCtrls,
  21.   QButtons, QComCtrls,
  22. {$ENDIF}
  23.  
  24.   Plot, Plotdefs, Plotmenu, Plotimagelist, Data, Plottoolbar, Nedit, Misc;
  25.  
  26. type
  27.   TMainForm = class(TForm)
  28.     Panel1: TPanel;
  29.     StringGrid1: TStringGrid;
  30.     MinNEdit: TNEdit;
  31.     Label1: TLabel;
  32.     Label2: TLabel;
  33.     MaxNEdit: TNEdit;
  34.     Label3: TLabel;
  35.     StepSizeNEdit: TNEdit;
  36.     Label4: TLabel;
  37.     MeanNEdit: TNEdit;
  38.     Label5: TLabel;
  39.     StdDevNEdit: TNEdit;
  40.     GoBitBtn: TBitBtn;
  41.     GoCrazyBitBtn: TBitBtn;
  42.     ClearAllBitBtn: TBitBtn;
  43.     NoisyBitBtn: TBitBtn;
  44.     TraceBitBtn: TBitBtn;
  45.     TypeBitBtn: TBitBtn;
  46.     StatusBar1: TStatusBar;
  47.     procedure ClearAllBitBtnClick(Sender: TObject);
  48.     procedure GoBitBtnClick(Sender: TObject);
  49.     procedure FormCreate(Sender: TObject);
  50.     procedure PlotMenu1ExitMenuItemClick(Sender: TObject);
  51.     procedure NoisyBitBtnClick(Sender: TObject);
  52.     procedure GoCrazyBitBtnClick(Sender: TObject);
  53.     procedure CrazyTimerTimer(Sender: TObject);
  54.     procedure MyPlotFileOpen(Sender: TObject; TheFile: String);
  55.     procedure TraceBitBtnClick(Sender: TObject);
  56.     procedure TypeBitBtnClick(Sender: TObject);
  57.     procedure Plot1AfterPaint(Sender: TObject; ACanvas: TCanvas);
  58.     procedure MyPlotBeforeDraw(Sender: TObject; ACanvas: TCanvas);
  59.   private
  60.     MyPlot: TPlot;
  61. {$IFNDEF JUST_THE_GRAPH}
  62.     MyPlotMenu: TPlotMenu;
  63.     MyPlotImageList: TPlotImageList;
  64.     MyPlotToolBar: TPlotToolBar;
  65. {$ENDIF}
  66.     Revolutions,
  67.     StartWidth,
  68.     StartHeight: Integer;
  69.     Angle,
  70.     AngleInc: Single;
  71.     StartTime: TDateTime;
  72.     TypeClicked: Boolean;
  73.   public
  74.     { Public declarations }
  75.   end;
  76.  
  77. var
  78.   MainForm: TMainForm;
  79.  
  80. implementation
  81.  
  82. {$R *.dfm}
  83. {$R Penguin.res}
  84.  
  85. {Kylix seems to have a versioninfo resource already.
  86.  Unfortunately, it is empty.}
  87. {.$ R version.res}
  88.  
  89. const
  90.   RADIUS = 50.0;
  91. {$IFDEF WIN32}
  92.   MYCAPTION = 'TPlot demo for Delphi';
  93. {$ENDIF}
  94. {$IFDEF LINUX}
  95.   MYCAPTION = 'TPlot demo for Kylix';
  96. {$ENDIF}
  97.  
  98. procedure TMainForm.FormCreate(Sender: TObject);
  99. begin
  100.   TypeClicked := FALSE;
  101.   Height := 516;
  102.   Width := 712;
  103.  
  104.   MyPlot := TPlot.Create(Self);
  105.   MyPlot.Parent := Self;
  106.   MyPlot.Align := alClient;
  107.   MyPlot.PlotType := ptXY;
  108.   MyPlot.NoSeries := 2;
  109.   MyPlot.MakeDummyData(20);
  110.  
  111. {$IFNDEF JUST_THE_GRAPH}
  112.   MyPlotImageList := TPlotImageList.Create(Self);
  113.   MyPlot.Images := MyPlotImageList;
  114.  
  115.   MyPlotMenu := TPlotMenu.Create(Self);
  116.   MyPlotMenu.Images := MyPlotImageList;
  117.   MyPlotMenu.Plot := MyPlot;
  118.   MyPlotMenu.SetUpOnClicks;
  119.  
  120.   MyPlotToolBar := TPlotToolBar.Create(Self);
  121.   MyPlotToolBar.Parent := Self;
  122.   MyPlotToolBar.Images := MyPlotImageList;
  123.   MyPlotToolBar.Plot := MyPlot;
  124. {$ENDIF}
  125.   MyPlot.OnFileOpen := MyPlotFileOpen;
  126.   MyPlot.OnBeforeDraw := MyPlotBeforeDraw;
  127.   MyPlot.Instructions.Add('');
  128.   MyPlot.Instructions.Add('Click on the "Type" button to see different Plot Types.');
  129.  
  130.   StringGrid1.Cells[0, 0] := 'Test:';
  131.   StringGrid1.Cells[0, 1] := 'Score:';
  132.   StringGrid1.ColWidths[0] := 60;
  133.  
  134.   Self.Caption := MYCAPTION;
  135. end;
  136.  
  137. procedure TMainForm.ClearAllBitBtnClick(Sender: TObject);
  138. var
  139.   i: Integer;
  140. begin
  141.   MyPlot.SeriesList.ClearSeries;
  142.   for i := 1 to StringGrid1.ColCount-1 do
  143.   begin
  144.     StringGrid1.Cells[i, 0] := '';
  145.     StringGrid1.Cells[i, 1] := '';
  146.   end;
  147. end;
  148.  
  149. procedure TMainForm.GoBitBtnClick(Sender: TObject);
  150. var
  151.   X, Y: Single;
  152.   Mean: Single;
  153.   StdDev: Single;
  154.   Min: Single;
  155.   Max: Single;
  156.   StepSize: Single;
  157. begin
  158.   MyPlot.NoSeries := 1;
  159.   MyPlot.Series[0].DelData;
  160.  
  161.   Screen.Cursor := crHourGlass;
  162.  
  163.   Mean := MeanNEdit.AsReal;
  164.   StdDev := StdDevNEdit.AsReal;
  165.   Min := MinNEdit.AsReal;
  166.   Max := MaxNEdit.AsReal;
  167.   StepSize := StepSizeNEdit.AsReal;
  168.  
  169. {Set the axes:}
  170.   MyPlot.XAxis.Max := Max;
  171.   MyPlot.XAxis.Min := Min;
  172.   MyPlot.YAxis.Min := 0;
  173.   MyPlot.XAxis.Intercept := 0;
  174.  
  175.   X := Min;
  176.   while (X <= Max) do
  177.   begin
  178.     Y := Exp(-Sqr((X-Mean)/(2*StdDev))) /
  179.       Sqrt(2*Pi*StdDev);
  180. {Don't fire any events, and don't adjust axes:}
  181.     MyPlot[0].AddPoint(X, Y, FALSE, FALSE);
  182.     X := X + StepSize;
  183.   end;
  184.   MyPlot[0].Visible := TRUE;
  185.  
  186.   {MyPlot.YAxis.Min := MyPlot.Series[0].YMin;}
  187.   MyPlot.YAxis.Max := MyPlot.Series[0].YMax;
  188.  
  189.   {for i := 1 to StringGrid1.ColCount-1 do
  190.   begin
  191.     if (Length(StringGrid1.Cells[i, 0]) > 0) then
  192.     begin
  193.       if (Length(StringGrid1.Cells[i, 1]) > 0) then
  194.       begin
  195.         try
  196.           TheScore := StrToFloat(StringGrid1.Cells[i, 1]);
  197.           TheSeries := MyPlot.Add(-1);
  198.           MyPlot[TheSeries].Name := StringGrid1.Cells[i, 0];
  199.           MyPlot[TheSeries].AddPoint(TheScore, MyPlot.YAxis.Min, TRUE, TRUE);
  200.           MyPlot[TheSeries].AddPoint(TheScore, MyPlot.YAxis.Max, TRUE, TRUE);
  201.           MyPlot[TheSeries].Visible := TRUE;
  202.           MyPlot[TheSeries].Symbol := TSymbol(i mod (1+Ord(sDownTriangle)));
  203.         finally
  204.         end;
  205.       end;
  206.     end;
  207.   end;}
  208. {get the finishing time:}
  209.  
  210.   Screen.Cursor := crDefault;
  211.   Self.Caption := Format(MYCAPTION + ' - %d points', [MyPlot.Series[0].NoPts]);
  212. end;
  213.  
  214. procedure TMainForm.PlotMenu1ExitMenuItemClick(Sender: TObject);
  215. begin
  216.   Close;
  217. end;
  218.  
  219. procedure TMainForm.NoisyBitBtnClick(Sender: TObject);
  220. begin
  221.   MyPlot.MakeDummyData(100);
  222. end;
  223.  
  224. procedure TMainForm.GoCrazyBitBtnClick(Sender: TObject);
  225. begin
  226.   if (Assigned(MyPlot.OnAfterPaint)) then
  227.   begin
  228.     GoCrazyBitBtn.Caption := 'Go Crazy';
  229.     TraceBitBtn.Enabled := TRUE;
  230.     MyPlot.OnAfterPaint := nil;
  231.   end
  232.   else
  233.   begin
  234.     Revolutions := 0;
  235.     StartTime := Now;
  236.     StartWidth := Width;
  237.     StartHeight := Height;
  238.     Angle := 0;
  239.     AngleInc := 4 * Pi / 180;
  240.     GoCrazyBitBtn.Caption := 'Enough !';
  241.     TraceBitBtn.Enabled := FALSE;
  242.     MyPlot.OnAfterPaint := Plot1AfterPaint;
  243.     MyPlot.Invalidate;
  244.   end;
  245. end;
  246.  
  247. procedure TMainForm.CrazyTimerTimer(Sender: TObject);
  248. var
  249.   fpm: Single;
  250.   ElapsedTime: TDateTime;
  251. begin
  252.   if (MyPlot.PlotType >= pt3DContour) then
  253.   begin
  254.     MyPlot.ZAngle := MyPlot.ZAngle + 1;
  255.   end
  256.   else
  257.   begin
  258.     Width := StartWidth + Round(RADIUS * Sin(Angle));
  259.     Height := StartHeight + Round(RADIUS * Cos(Angle));
  260.     Angle := Angle + AngleInc;
  261.   end;
  262.   Inc(Revolutions);
  263.   ElapsedTime := Now - StartTime;
  264.   fpm := Revolutions / ((24 * 3600)*ElapsedTime);
  265.   StatusBar1.SimpleText := Format(
  266.     '%d frames, %8.2f frames per second',
  267.     [Revolutions, fpm]);
  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.  ThePlotType: Integer;
  289.  TheXStringData: TStringList;
  290.  
  291.   procedure DoXY;
  292.   begin
  293.     MyPlot.SetInstructionText('Please wait while I add some symbols ...');
  294.     Wait(2000, TRUE);
  295.     MyPlot.Series[0].Symbol := sySquare;
  296.     Wait(2000, TRUE);
  297.     MyPlot.Series[1].Symbol := syCircle;
  298.     MyPlot.SetInstructionText('Done !');
  299.   end;
  300.  
  301. begin
  302.   if (not TypeClicked) then
  303.   begin
  304.     DoXY;
  305.     TypeClicked := TRUE;
  306.     exit;
  307.   end;
  308.  
  309.  
  310.   ThePlotType := Ord(MyPlot.PlotType);
  311.   ThePlotType := (ThePlotType+1) mod (Ord(High(TPlotType))+1);
  312. {Initialise properties that can change:}
  313.   MyPlot.SeriesList.ClearSeries;
  314.   MyPlot.YAxis.LimitsVisible := FALSE;
  315.   MyPlot.ContourInterval := 1;
  316.   MyPlot.ContourDetail := cdLow;
  317.   MyPlot.ContourWireFrame := FALSE;
  318.   MyPlot.YAxis.TickDirection := orLeft;
  319.   MyPlot.YAxis.Title.Orientation := orLeft;
  320.   MyPlot.YAxis.Min := 0;
  321.   
  322.   MyPlot.PlotType := TPlotType(ThePlotType);
  323.   
  324.   MyPlot.Title.Caption := 'TPlot - ' + #10 +
  325.     MyPlot.GetPlotTypeAsString;
  326.   case MyPlot.PlotType of
  327.     ptXY:
  328.       begin
  329.         MyPlot.MakeDummyData(100);
  330.         MyPlot.Border.Left := 70;
  331.         MyPlot.Border.BottomGap := 80;
  332.         DoXY;
  333.       end;
  334.     ptError:
  335.       begin
  336.         MyPlot.MakeDummyData(20);
  337.         MyPlot.Series[0].Symbol := syCircle;
  338.         MyPlot.SetInstructionText('This is boring !');
  339.       end;
  340.     ptMultiple:
  341.       begin
  342.         MyPlot.NoSeries := 4;
  343.         MyPlot.Multiplicity := 4;
  344.         MyPlot.MakeDummyData(20);
  345.         MyPlot.Series[0].Pen.Width := 0;
  346.         MyPlot.Series[1].Pen.Width := 0;
  347.         MyPlot.Series[2].Pen.Width := 0;
  348.         MyPlot.Series[3].Pen.Width := 0;
  349.         MyPlot.Series[0].Name := 'High';
  350.         MyPlot.Series[1].Name := 'Low';
  351.         MyPlot.Series[2].Name := 'Open';
  352.         MyPlot.Series[3].Name := 'Close';
  353.         MyPlot.Series[0].Symbol := syLeftDash;
  354.         MyPlot.Series[1].Symbol := syRightDash;
  355.         MyPlot.MultiJoin := '2,3';
  356.         MyPlot.SetInstructionText('Please wait while I add some Limits ...');
  357.         Misc.Wait(2000, FALSE);
  358.         MyPlot.YAxis.LimitLower := 2;
  359.         MyPlot.YAxis.LimitUpper := 9;
  360.         MyPlot.YAxis.LimitsVisible := TRUE;
  361.         MyPlot.Series[3].ShadeLimits := TRUE;
  362.         MyPlot.SetInstructionText('Done !');
  363.       end;
  364.     ptBubble:
  365.       begin
  366.         MyPlot.MakeDummyData(20);
  367.         MyPlot.SetInstructionText('This is boring !');
  368.       end;
  369.     ptColumn, ptStack, ptNormStack:
  370.       begin
  371.         MyPlot.Grid := gtNone;
  372.         MyPlot.MakeDummyData(10);
  373.         MyPlot.SetInstructionText('This is boring !');
  374.       end;
  375.     ptPie:
  376.       begin
  377.         MyPlot.MakeDummyData(10);
  378.         TheXStringData := TStringList.Create;
  379.         TheXStringData.Add('Alpha');
  380.         TheXStringData.Add('Bravo');
  381.         TheXStringData.Add('Charlie');
  382.         TheXStringData.Add('Delta');
  383.         TheXStringData.Add('Echo');
  384.         TheXStringData.Add('Foxtrot');
  385.         TheXStringData.Add('Golf');
  386.         TheXStringData.Add('Hotel');
  387.         TheXStringData.Add('India');
  388.         TheXStringData.Add('Juliet');
  389.         TheXStringData.Add('Kilo');
  390.         MyPlot.Series[0].XStringData := TheXStringData;
  391.         MyPlot.Series[1].XStringData := TheXStringData;
  392.         TheXStringData.Free;
  393.         MyPlot.SetInstructionText('This is very boring !');
  394.       end;
  395.     ptPolar:
  396.       begin
  397.         MyPlot.XAxis.Min := -10;
  398.         MyPlot.XAxis.Max := 10;
  399.         MyPlot.YAxis.Min := -10;
  400.         MyPlot.YAxis.Max := 10;
  401.         MyPlot.MakeDummyData(20);
  402.         MyPlot.XAxis.Intercept := 0;
  403.         MyPlot.YAxis.Intercept := 0;
  404.         MyPlot.SetInstructionText('This is extremely boring !');
  405.       end;
  406.     ptLineContour, ptContour, pt3DContour, pt3DWire, pt3DColumn:
  407.       begin
  408.         if (MyPlot.PlotType > ptContour) then
  409.         begin
  410.           MyPlot.Border.Left := 120;
  411.           MyPlot.Border.BottomGap := 130;
  412.         end;
  413.         MyPlot.XAxis.Min := 0;
  414.         MyPlot.XAxis.Max := 10;
  415.         MyPlot.YAxis.Min := 0;
  416.         MyPlot.YAxis.Max := 10;
  417.         MyPlot.NoSeries := 10;
  418.         MyPlot.MakeDummyData(20);
  419.       end;
  420.   else ;
  421.   end;
  422.  
  423.   case MyPlot.PlotType of
  424.     ptLineContour:
  425.       begin
  426.         MyPlot.SetInstructionText('Please wait while I increase the ContourDetail ...');
  427.         Misc.Wait(2000, FALSE);
  428.         MyPlot.ContourDetail := cdMedium;
  429.         MyPlot.ContourInterval := MyPlot.ContourInterval / 2;
  430.         Misc.Wait(2000, FALSE);
  431.         MyPlot.ContourDetail := cdHigh;
  432.         MyPlot.SetInstructionText('Done !');
  433.       end;
  434.     ptContour:
  435.       begin
  436.         MyPlot.SetInstructionText('Please wait while I increase the ContourDetail ...');
  437.         Misc.Wait(2000, FALSE);
  438.         MyPlot.ContourDetail := cdMedium;
  439.         Misc.Wait(2000, FALSE);
  440.         MyPlot.ContourDetail := cdHigh;
  441.         MyPlot.SetInstructionText('Done !');
  442.       end;
  443.     pt3DContour:
  444.       begin
  445.         MyPlot.SetInstructionText('Please wait while I increase the ContourDetail ...');
  446.         Misc.Wait(2000, FALSE);
  447.         MyPlot.ContourDetail := cdMedium;
  448.         Misc.Wait(2000, FALSE);
  449.         MyPlot.ContourDetail := cdHigh;
  450.         Misc.Wait(2000, FALSE);
  451.         MyPlot.SetInstructionText('... add a wireframe ...');
  452.         Misc.Wait(2000, FALSE);
  453.         MyPlot.ContourWireFrame := TRUE;
  454.         MyPlot.SetInstructionText('... add some walls and grids ...');
  455.         Misc.Wait(2000, FALSE);
  456.         MyPlot.Grid := gtBoth;
  457.         MyPlot.SetInstructionText('... move the axes to render them more visible ...');
  458.         Misc.Wait(2000, FALSE);
  459.         MyPlot.XAxis.Intercept := MyPlot.YAxis.Max;
  460.         Misc.Wait(2000, FALSE);
  461.         MyPlot.YAxis.Intercept := MyPlot.XAxis.Max;
  462.         MyPlot.SetInstructionText('... re-arrange the Y Axis Title and Labels');
  463.         Misc.Wait(2000, FALSE);
  464.         MyPlot.YAxis.TickDirection := orRight;
  465.         MyPlot.YAxis.Title.Orientation := orRight;
  466.         MyPlot.SetInstructionText('... move the Z Axis ...');
  467.         Misc.Wait(2000, FALSE);
  468.         MyPlot.ZAxis.ZInterceptY := MyPlot.YAxis.Max;
  469.         MyPlot.SetInstructionText('Done !');
  470.         MyPlot.Refresh;
  471.       end;
  472.     pt3DWire, pt3DColumn:
  473.       begin
  474.         MyPlot.SetInstructionText('Please wait while I move the axes to render them more visible ...');
  475.         Misc.Wait(2000, FALSE);
  476.         MyPlot.XAxis.Intercept := MyPlot.YAxis.Max;
  477.         MyPlot.YAxis.Intercept := MyPlot.XAxis.Max;
  478.         MyPlot.YAxis.TickDirection := orRight;
  479.         MyPlot.YAxis.Title.Orientation := orRight;
  480.         MyPlot.SetInstructionText('Done !');
  481.       end;
  482.   end;
  483. end;
  484.  
  485. procedure TMainForm.Plot1AfterPaint(Sender: TObject; ACanvas: TCanvas);
  486. begin
  487.   Application.ProcessMessages;
  488.   CrazyTimerTimer(Sender);
  489. end;
  490.  
  491. procedure TMainForm.MyPlotBeforeDraw(Sender: TObject; ACanvas: TCanvas);
  492. var
  493.   ABitmap: TBitmap;
  494.   ARect: TRect;
  495. begin
  496.   if (MyPlot.PlotType = ptLineContour) then
  497.   begin
  498.     ABitmap := TBitmap.Create;
  499.     ABitmap.LoadFromResourceName(hInstance, 'PENGUIN');
  500.     ARect.Left := MyPlot.Border.Left;
  501.     ARect.Top := MyPlot.Border.Top;
  502.     ARect.Right := MyPlot.Border.Right;
  503.     ARect.Bottom := MyPlot.Border.Bottom;
  504.     ACanvas.StretchDraw(ARect, ABitmap);
  505.     ABitmap.Free;
  506.   end;
  507. end;
  508.  
  509. end.
  510.