{*********************************************}
{  TeeChart Charting Library for FireMonkey   }
{  Copyright (c) 2012-2020 by Steema Software }
{  All Rights Reserved                        }
{*********************************************}
unit FMXTee.Editor.AxisMaxMin;
{$I TeeDefs.inc}

interface

uses
  SysUtils, Types, System.UITypes, Classes, Variants, FMX.Types, FMX.Controls,
  {$IFNDEF UPTOXE3}
  FMX.StdCtrls,
  {$ENDIF}
  FMX.Forms, FMX.Edit, FMX.Dialogs, FMXTee.Engine;

type
  TAxisMaxMin = class(TForm)
    LMax: TLabel;
    LMin: TLabel;
    BitBtn1: TButton;
    BitBtn2: TButton;
    EMaximum: TEdit;
    EMinimum: TEdit;
  private
   { Private declarations }
    Axis : TChartAxis;
    IsMax : Boolean;

    function IsDateTime:Boolean;
    procedure Prepare;
  public
   { Public declarations }
    class function Edit(const AOwner:TComponent; const AAxis:TChartAxis;
                        const ACheckBox: TCheckBox; IsMaximum:Boolean):Boolean;
  end;

implementation

{$R *.fmx}

uses
  FMXTee.Procs;

function TAxisMaxMin.IsDateTime: Boolean;
begin
  result:=Axis.IsDateTime;
end;

procedure TAxisMaxMin.Prepare;
var MaxMin : Double;
begin
  if IsDateTime then
  begin
    if IsMax then
       MaxMin:=Axis.Maximum
    else
       MaxMin:=Axis.Minimum;

    if ((MaxMin>DTMinValue) and (MaxMin<DTMaxValue)) then
       EMaximum.Text:=DateToStr(MaxMin)
    else
       EMaximum.Text:='';

    EMinimum.Text:=TimeToStr(MaxMin);
  end
  else
  begin
    LMax.Text:='Maximum:'; // Do not localize
    LMin.Text:='Minimum:'; // Do not localize

    EMaximum.Text:=FloatToStr(Axis.Maximum);
    EMinimum.Text:=FloatToStr(Axis.Minimum);

    if IsMax then
       ActiveControl:=EMaximum
    else
       ActiveControl:=EMinimum;
  end;

  TeeTranslateControl(Self);
end;

class function TAxisMaxMin.Edit(const AOwner: TComponent; const AAxis: TChartAxis;
                                const ACheckBox: TCheckBox; IsMaximum:Boolean):Boolean;
var MaxMin : Double;
begin
  with TAxisMaxMin.Create(AOwner) do
  try
    Axis:=AAxis;
    IsMax:=IsMaximum;

    Prepare;

    result:=ShowModal=mrOk;

    if result then
    begin
      if IsDateTime then
      begin
        if EMaximum.Text<>'' then
           MaxMin:=StrToDateTime(EMaximum.Text+' '+EMinimum.Text)
        else
           MaxMin:=StrToTime(EMinimum.Text);

        if IsMax then
           Axis.Maximum:=MaxMin
        else
           Axis.Minimum:=MaxMin;
      end
      else
      begin
        Axis.Maximum:=StrToFloat(EMaximum.Text);
        Axis.Minimum:=StrToFloat(EMinimum.Text);
      end;

      ACheckBox.IsChecked:=False;
    end;
  finally
    Free;
  end;
end;

end.
