unit main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, TeeProcs, TeEngine, Chart, TeeShape, Series, ComCtrls, ToolWin,
  ActnList,data, Placemnt;

const
  Vci      : integer = 4;
  Vr       : integer = 12;
  Vco      : integer = 25;
  LegColor : TColor  = clGreen;

type
  TForm1 = class(TForm)
    CoolBar1: TCoolBar;
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    Chart1: TChart;
    ActionList1: TActionList;
    actOpen: TAction;
    OpenDialog: TOpenDialog;
    FormStorage: TFormStorage;
    ToolButton2: TToolButton;
    actClipCopy: TAction;
    ToolButton3: TToolButton;
    actEditChart: TAction;
    ToolButton4: TToolButton;
    actEditData: TAction;
    procedure FormCanResize(Sender: TObject; var NewWidth,
      NewHeight: Integer; var Resize: Boolean);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure actOpenExecute(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure actClipCopyExecute(Sender: TObject);
    procedure actEditChartExecute(Sender: TObject);
    procedure actEditDataExecute(Sender: TObject);
  private
    { Private declarations }
    FTabData : TTabData;
  public
    { Public declarations }
    procedure PaintChart;

  end;

var
  Form1: TForm1;

implementation

uses
  Math,ieditcha, TabDataEd;

{$R *.DFM}



procedure TForm1.FormCanResize(Sender: TObject; var NewWidth,
  NewHeight: Integer; var Resize: Boolean);
begin
//  NewWidth := NewHeight;
end;

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key=VK_INSERT then
  begin
    Chart1.CopyToClipboardMetafile(true);
  end;
end;

procedure TForm1.actOpenExecute(Sender: TObject);
begin
  if OpenDialog.Execute then
  begin
    if Assigned(FTabData) then
       FTabData.Free;
    FTabData := TTabData.Create;
    FTabData.LoadFromFile(OpenDialog.FileName);
    OpenDialog.InitialDir := ExtractFileDir(OpenDialog.FileName);
    PaintChart;
    actEditDAta.Enabled := True;
  end;
end;

procedure TForm1.PaintChart;

type
 TPoint2D    = record
                x,y : real;
               end;
 TPointArray = array of TPoint2d;

 function Rot(P : TPoint2d; ang : real) : TPoint2d;
 begin
   with Result do
   begin
     X:=p.X*Cos(Ang)-p.Y*Sin(Ang);
     Y:=p.Y*Cos(Ang)+p.X*Sin(Ang);
   end;
 end;


 function ConstructLeg(Tab : TTabData; d : integer) : TPointArray;
 const
  W : real = 0.2;
 var
  i   : integer;
  L1  : real;
  L2  : real;
  L3  : real;
  L4  : real;
  ang : real;
 begin
   SetLength(Result,16);
   L1 := 0;  L2 := 0;
   L3 := 0;  L4 := 0;
   with Tab do
   begin
     for i:=0 to Vci-1 do
      if i<=High(PerSectFreq) then
         L1 := L1 + PerSectFreq[i,d];
     for i:=Vci to Vr-1 do
      if i<=High(PerSectFreq) then
         L2 := L2 + PerSectFreq[i,d];
     for i:=Vr to Vco-1 do
      if i<=High(PerSectFreq) then
         L3 := L3 + PerSectFreq[i,d];
     for i:=Vco to High(PerSectFreq) do
         L4 := L4 + PerSectFreq[i,d];
   end;

   L1:=    (L1/1000) * Tab.TTLSectFreq[D];
   L2:= L1+(L2/1000) * Tab.TTLSectFreq[D];
   L3:= L2+(L3/1000) * Tab.TTLSectFreq[D];
   L4:= L3+(L4/1000) * Tab.TTLSectFreq[D];

   Result[0].X := 0;   Result[0].Y := 0;
   Result[1].X := 0;   Result[1].Y := L1;
   Result[2].X :=-W;   Result[2].Y := L1;
   Result[3].X :=-W;   Result[3].Y := L2;
   Result[4].X :=-2*W; Result[4].Y := L2;
   Result[5].X :=-2*W; Result[5].Y := L3;
   Result[6].X :=-3*W; Result[6].Y := L3;
   Result[7].X :=-3*W; Result[7].Y := L4;

   Result[8].X := 3*W; Result[8].Y := L4;
   Result[9].X := 3*W; Result[9].Y := L3;
   Result[10].X:= 2*W; Result[10].Y:= L3;
   Result[11].X:= 2*W; Result[11].Y:= L2;
   Result[12].X:= W;   Result[12].Y:= L2;
   Result[13].X:= W;   Result[13].Y:= L1;
   Result[14].X:= 0;   Result[14].Y:= L1;
   Result[15].X:= 0;   Result[15].Y:= 0;

   Ang:=-D*(2*pi/Tab.SectCount);
   for i:=0 to High(Result) do
     Result[i] := Rot(result[i],ang);
   {
   with Result[i] do
   begin
     oldx := X;
     //Y:=-Y;
     X:=X*Cos(Ang)-Y*Sin(Ang);
     Y:=Y*Cos(Ang)+OldX*Sin(Ang);
   end;
  }

 end;

const
  ofsX   : integer = 500;
  ofsY   : integer = 500;
  scale  : real = 20;
var
 pict : TPicture;
 c    : TCanvas;
 D    : integer;
 r    : real;
 Leg  : TPointArray;
 i    : integer;
 Poly : array of TPoint;
 phi  : real;
 p    : TPoint2d;
 th   ,
 tw   : integer;
begin
  Leg := nil;

  if not Assigned(FTabData) then
     Exit;

  {
  Pict := Chart1.BackImage;
  if Assigned(Pict) then
     Pict.Free;
  }

  pict := TPicture.Create;
  pict.Metafile := TMetaFile.Create;
  Pict.Metafile.Width  := Chart1.Width;
  Pict.MetaFile.Height := Chart1.Height;

  r:= MaxValue(FTabData.ttlSectFreq);

  Scale := Min(Chart1.Width,Chart1.Height)/r/3;

  c := TMetaFileCanvas.Create(pict.Metafile,0);
  ofsX := Chart1.Width div 2; // Chart1.ChartXCenter;
  ofsY := Chart1.Height div 2; //Chart1.ChartYCenter;
  {
  c.Ellipse(ofsx-round(r*Scale*1.05),
            ofsy-round(r*scale*1.05),
            ofsx+round(r*scale*1.05),
            ofsy+Round(r*scale*1.05));
  }
  for D:=0 to FTabData.SectCount-1 do
  begin
    Leg := ConstructLeg(FTabData,d);
    SetLength(Poly,Length(Leg));
    for i:=0 to High(Leg) do
    begin
      Poly[i].X := ofsx+Round(Leg[i].X*Scale);
      Poly[i].Y := ofsy-Round(Leg[i].Y*Scale);
    end;
    c.Brush.Color := legColor;
    c.Polygon(Poly);
  end;
  c.Brush.Style := bsClear;

  phi := (2*pi/12);

  for i:=0 to 11 do
  begin
    tw := c.TextWidth(DirNames[i]); //div 2;
    th := c.TextHeight(DirNames[i]); //div 2;
    p.x := R*Scale+tw/Scale;
    p.x := 1.1 * p.x;
    p.y := th/Scale;
    p.y := 1.1 * p.y;
    p := rot(p,i*phi-pi/2);
    c.TextOut(ofsx+Round(p.x)-tw div 2,ofsy+Round(p.y)-th div 2,DirNames[i]);
  end;
  c.Free;
  
  Chart1.BackImage := pict;
  Chart1.Foot.Text.Clear;
  Chart1.Foot.Text.Add(FTabData.Description);
end;

procedure TForm1.FormResize(Sender: TObject);
begin
  PaintChart;
end;

procedure TForm1.actClipCopyExecute(Sender: TObject);
begin
  Chart1.CopyToClipboardMetafile(True);
end;

procedure TForm1.actEditChartExecute(Sender: TObject);
var
  ef : TChartEditForm;
begin
  ef := TChartEditForm.Create(Application);
  with ef do
  try
    TheChart := Chart1;
    ef.TabSeriesList.TabVisible := False;
    ef.TabSeries.TabVisible := False;
    ef.TheActivePageIndex   := 2;
    ShowModal;
  finally
    Free;
  end;
end;

procedure TForm1.actEditDataExecute(Sender: TObject);
begin
  with TFmTabDataEd.Create(Application) do
  try
    TabData := FTabData ;
    ShowModal;

  finally
    Free;
  end;
end;

end.


