{**********************************************}
{   TBarSeries Component Editor Dialog         }
{   Copyright (c) 1996-2020 by Steema Software }
{**********************************************}
unit TeeStackBarEdit;
{$I TeeDefs.inc}

interface

uses {$IFNDEF LINUX}
     Windows, Messages,
     {$ENDIF}
     SysUtils, Classes,

     Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, ComCtrls,

     Chart, Series, TeCanvas, TeePenDlg, TeeProcs, TeeGalleryPanel;

type
  TStackBarSeriesEditor = class(TForm)
    Panel1: TPanel;
    CBMultiBar: TComboFlat;
    Label1: TLabel;
    CBYOrigin: TCheckBox;
    EYOrigin: TEdit;
    EGroup: TEdit;
    UDGroup: TUpDown;
    MultiBarGallery: TChartGalleryPanel;
    Timer1: TTimer;
    ESelfStackPos: TEdit;
    CBSelfStackPos: TCheckBox;
    Label2: TLabel;
    procedure FormShow(Sender: TObject);
    procedure CBYOriginClick(Sender: TObject);
    procedure EYOriginChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure EGroupChange(Sender: TObject);
    procedure CBMultiBarChange(Sender: TObject);
    procedure MultiBarGalleryChangeChart(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure ESelfStackPosChange(Sender: TObject);
    procedure CBSelfStackPosClick(Sender: TObject);
  private
    { Private declarations }
    CreatingForm : Boolean;
    Bar : TCustomBarSeries;

    procedure CreateGallery;
    Procedure EnableGroup;
    Procedure EnableSelfStack;
    procedure SelectGallery;
  public
    { Public declarations }
  end;

implementation

{$IFNDEF LCL}
{$R *.DFM}
{$ELSE}
{$R *.lfm}
{$ENDIF}

uses
  TeEngine;

procedure TStackBarSeriesEditor.CreateGallery;
type
  TCustomBarSeriesClass=class of TCustomBarSeries;

  function AddSeries(const AChart:TCustomChart; const AMultiBar:TMultiBar):TCustomBarSeries;
  var tmpClass : TCustomBarSeriesClass;
  begin
    if (not Assigned(Bar)) or Bar.YMandatory then
       tmpClass:=TBarSeries
    else
       tmpClass:=THorizBarSeries;

    result:=AChart.AddSeries(tmpClass) as TCustomBarSeries;
    result.MultiBar:=AMultiBar;
    result.Marks.Hide;
  end;

var t : Integer;
    tmp : TGalleryChart;
begin
  MultiBarGallery.NumCols:=2;
  MultiBarGallery.NumRows:=3;

  for t:=0 to CBMultiBar.Items.Count-1 do
  begin
    tmp:=TGalleryChart.Create(MultiBarGallery);

    AddSeries(tmp,TMultiBar(t)).AddArray([50,80]);
    AddSeries(tmp,TMultiBar(t)).AddArray([20,60]);

    tmp.Hint:=CBMultiBar.Items[t];
    tmp.Title.Caption:=tmp.Hint;
    tmp.ShowHint:=True;

    if Assigned(Bar) and Assigned(Bar.ParentChart) then
       tmp.ColorPaletteIndex:=(Bar.ParentChart as TCustomChart).ColorPaletteIndex;

    MultiBarGallery.Charts.Add(tmp);
    tmp.Parent:=MultiBarGallery;

    MultiBarGallery.FinishAdd(tmp);

    tmp.Axes.Left.Texts.Visible:=False;
    //tmp.Walls.Hide;
    tmp.View3DOptions.Orthogonal:=True;
  end;

  MultiBarGallery.ResizeCharts;
end;

procedure TStackBarSeriesEditor.FormShow(Sender: TObject);
begin
  Bar:=TCustomBarSeries(Tag);

  if Assigned(Bar) then
  With Bar do
  begin
    CBYOrigin.Checked    :=UseYOrigin;
    EYOrigin.Text        :=FloatToStr(YOrigin);
    EYOrigin.Enabled     :=UseYOrigin;
    CBMultiBar.ItemIndex :=Ord(MultiBar);
    UDGroup.Position     :=StackGroup;

    CBSelfStackPos.Checked:=SelfStack.UsePosition;
    ESelfStackPos.Text   :=FloatToStr(SelfStack.Position);

    EnableGroup;
    EnableSelfStack;
  end;

  Timer1.Enabled:=True;

  CreatingForm:=False;
end;

procedure TStackBarSeriesEditor.CBSelfStackPosClick(Sender: TObject);
begin
  if not CreatingForm then
  begin
    Bar.SelfStack.UsePosition:=CBSelfStackPos.Checked;
    ESelfStackPos.Enabled:=Bar.SelfStack.UsePosition;

    if ESelfStackPos.Enabled then
       ESelfStackPos.SetFocus;
  end;
end;

procedure TStackBarSeriesEditor.CBYOriginClick(Sender: TObject);
begin
  if not CreatingForm then
  begin
    Bar.UseYOrigin:=CBYOrigin.Checked;
    EYOrigin.Enabled:=Bar.UseYOrigin;

    if EYOrigin.Enabled then
       EYOrigin.SetFocus;
  end;
end;

procedure TStackBarSeriesEditor.EYOriginChange(Sender: TObject);
begin
  if not CreatingForm then
     with Bar do YOrigin:=StrToFloatDef(EYOrigin.Text,YOrigin);
end;

Procedure TStackBarSeriesEditor.EnableGroup;
begin
  UDGroup.Enabled:=(Bar.MultiBar=mbStacked) or (Bar.MultiBar=mbStacked100);
  EGroup.Enabled:=UDGroup.Enabled;
end;

Procedure TStackBarSeriesEditor.EnableSelfStack;
begin
  CBSelfStackPos.Enabled:=Bar.MultiBar=mbSelfStack;
  ESelfStackPos.Enabled:=(Bar.MultiBar=mbSelfStack) and (Bar.SelfStack.UsePosition);
end;

procedure TStackBarSeriesEditor.ESelfStackPosChange(Sender: TObject);
begin
  if not CreatingForm then
     with Bar do
          SelfStack.Position:=StrToFloatDef(ESelfStackPos.Text,SelfStack.Position);
end;

procedure TStackBarSeriesEditor.FormCreate(Sender: TObject);
begin
  CreatingForm:=True;
  Align:=alClient;
end;

procedure TStackBarSeriesEditor.EGroupChange(Sender: TObject);
begin
  if not CreatingForm then
     Bar.StackGroup:=UDGroup.Position
end;

procedure TStackBarSeriesEditor.CBMultiBarChange(Sender: TObject);
begin
  if not CreatingForm then
  begin
    Bar.MultiBar:=TMultiBar(CBMultiBar.ItemIndex);
    EnableGroup;
    EnableSelfStack;

    SelectGallery;
  end;
end;

procedure TStackBarSeriesEditor.SelectGallery;
begin
  MultiBarGallery.SelectChart(MultiBarGallery[CBMultiBar.ItemIndex]);
end;

procedure TStackBarSeriesEditor.MultiBarGalleryChangeChart(
  Sender: TObject);
begin
  CBMultiBar.ItemIndex:=MultiBarGallery.Charts.IndexOf(MultiBarGallery.SelectedChart);
  CBMultiBarChange(Self);
end;

procedure TStackBarSeriesEditor.Timer1Timer(Sender: TObject);
begin
  Timer1.Enabled:=False;
  CreateGallery;
  SelectGallery;
end;

initialization
  RegisterClass(TStackBarSeriesEditor);
end.
