unit GroupFreqUnit; {$mode objfpc}{$H+} interface uses Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls, Buttons, ExtCtrls, MainUnit, OutPutUnit, FunctionsLib, GraphLib, Globals, DataProcs; type { TGroupFreqForm } TGroupFreqForm = class(TForm) GrpInBtn: TBitBtn; GrpOutBtn: TBitBtn; CancelBtn: TButton; ComputeBtn: TButton; GrpVarEdit: TEdit; Label1: TLabel; Label2: TLabel; Memo1: TMemo; OptionsBox: TRadioGroup; ResetBtn: TButton; ReturnBtn: TButton; VarList: TListBox; procedure ComputeBtnClick(Sender: TObject); procedure GrpInBtnClick(Sender: TObject); procedure GrpOutBtnClick(Sender: TObject); procedure ResetBtnClick(Sender: TObject); private { private declarations } public { public declarations } end; var GroupFreqForm: TGroupFreqForm; implementation { TGroupFreqForm } procedure TGroupFreqForm.ResetBtnClick(Sender: TObject); VAR i : integer; begin VarList.Clear; for i := 1 to NoVariables do VarList.Items.Add(OS3MainFrm.DataGrid.Cells[i,0]); GrpVarEdit.Text := ''; GrpInBtn.Visible := true; GrpOutBtn.Visible := false; end; procedure TGroupFreqForm.GrpInBtnClick(Sender: TObject); VAR index : integer; begin index := VarList.ItemIndex; if index < 0 then exit; GrpVarEdit.Text := VarList.Items.Strings[index]; VarList.Items.Delete(index); GrpInBtn.Visible := false; GrpOutBtn.Visible := true; end; procedure TGroupFreqForm.ComputeBtnClick(Sender: TObject); VAR nogroups, mingrp, maxgrp, grpcol, tempcol, value, minfreq, maxfreq : integer; labelstr : string; results, prompt : boolean; result, intvalue, i, j : integer; dblvalue : double; strvalue : string; freq : IntDyneVec; plottype : integer; begin // get the variable to analyze grpcol := 0; for i := 1 to NoVariables do begin strvalue := Trim(OS3MainFrm.DataGrid.Cells[i,0]); if GrpVarEdit.Text = strvalue then grpcol := i; end; labelstr := GrpVarEdit.Text; mingrp := 1000; maxgrp := -1000; for i := 1 to NoCases do begin if not ValidValue(i,grpcol) then continue; value := round(StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[grpcol,i]))); if value < mingrp then mingrp := value; if value > maxgrp then maxgrp := value; end; nogroups := maxgrp - mingrp + 1; if nogroups < 2 then begin ShowMessage('One or fewer groups found. Returning.'); exit; end; // setup frequency array and count cases in each group SetLength(freq,NoGroups+1); for i := 0 to NoGroups do freq[i] := 0; for i := 1 to NoCases do begin if not ValidValue(i,grpcol) then continue; value := round(StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[grpcol,i]))); value := value - mingrp; freq[value] := freq[value] + 1; end; // get min and max frequencies and check for existence of a range minfreq := 10000; maxfreq := -10000; for i := 0 to NoGroups-1 do begin if freq[i] < minfreq then minfreq := freq[i]; if freq[i] > maxfreq then maxfreq := freq[i]; end; if minfreq = maxfreq then begin ShowMessage('All groups have equal frequencies. Cannot plot.'); freq := nil; exit; end; plottype := OptionsBox.ItemIndex + 1; if plottype = 3 then plottype := 1 else if plottype = 4 then plottype := 2 else if plottype = 1 then plottype := 9 else if plottype = 2 then plottype := 10; // plot the frequencies SetLength(GraphFrm.Xpoints,1,nogroups+1); SetLength(GraphFrm.Ypoints,1,nogroups+1); GraphFrm.nosets := 1; GraphFrm.nbars := nogroups; GraphFrm.Heading := 'Frequency Distribution'; GraphFrm.XTitle := 'Values of ' + labelstr; GraphFrm.YTitle := 'Frequency'; GraphFrm.barwideprop := 0.5; GraphFrm.AutoScale := false; GraphFrm.miny := 0.0; GraphFrm.maxy := maxfreq; GraphFrm.GraphType := plottype; GraphFrm.BackColor := clYellow; GraphFrm.WallColor := clBlack; GraphFrm.FloorColor := clLtGray; GraphFrm.ShowBackWall := true; for i := 0 to nogroups do begin GraphFrm.Ypoints[0,i] := freq[i]; GraphFrm.Xpoints[0,i] := mingrp + i; end; GraphFrm.ShowModal; GraphFrm.Xpoints := nil; GraphFrm.Ypoints := nil; end; procedure TGroupFreqForm.GrpOutBtnClick(Sender: TObject); begin if GrpVarEdit.Text = '' then exit; VarList.Items.Add(GrpVarEdit.Text); GrpVarEdit.Text := ''; GrpInBtn.Visible := true; GrpOutBtn.Visible := false; end; initialization {$I groupfrequnit.lrs} end.