{********************************************}
{     TeeChart Pro Charting Library          }
{ Copyright (c) 1995-2020 by Steema Software }
{         All Rights Reserved                }
{********************************************}
unit TeeTransposeTool;
{$I TeeDefs.inc}

interface

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

     {$IFDEF FMX}
     FMX.Types, System.UITypes,
     {$ELSE}
     Graphics, Controls,
     {$ENDIF}

     {$IFDEF FMX}
     FMXTee.Canvas, FMXTee.Engine, FMXTee.Series.Surface
     {$ELSE}
     TeEngine, TeeSurfa, TeCanvas
     {$ENDIF}
     ;

type
  TGridTransposeTool=class(TTeeCustomTool)
  private
    FSeries : TCustom3DGridSeries;
  protected
    class Function GetEditorClass:String; override;
  public
    class Function Description:String; override;
    class Function LongDescription:String; override;
    procedure Transpose;
  published
    property Active;
    property Series:TCustom3DGridSeries read FSeries write FSeries;
  end;

implementation

uses
  {$IFDEF FMX}
  FMXTee.Chart, FMXTee.ProConstants
  {$ELSE}
  Chart, TeeProCo
  {$ENDIF}
  ;

{ TGridTransposeTool }

class function TGridTransposeTool.Description: String;
begin
  result:=TeeMsg_GridTranspose;
end;

class function TGridTransposeTool.GetEditorClass: String;
begin
  result:='TGridTransposeToolEdit';  { the editor dialog class name }
end;

class function TGridTransposeTool.LongDescription: String;
begin
  result:=TeeMsg_GridTranspDesc;
end;

procedure TGridTransposeTool.Transpose;
var t    : Integer;
    tmp  : TCustom3DGridSeries;
    Old  : TCustom3DPalette;
    tmpL : Integer;
begin
  if Active and Assigned(FSeries) then
  begin
    tmp:=TCustom3DGridSeries.Create(nil);
    try
      with FSeries do
      for t:=0 to Count-1 do
          tmp.AddXYZ(ZValues.Value[t],YValues.Value[t],XValues.Value[t]);

      // Backup palette, as it will be cleared when calling "AssignValues"
      tmpL:=Length(FSeries.Palette);
      SetLength(Old,tmpL);
      for t:=0 to tmpL-1 do
          Old[t]:=FSeries.Palette[t];

      try
        FSeries.AssignValues(tmp);

        // Restore palette from backup
        FSeries.Palette:=Old;
      finally
        // Release memory
        Old:=nil;
      end;
      
    finally
      tmp.Free;
    end;
  end;
end;

initialization
  RegisterTeeTools([TGridTransposeTool]);
finalization
  UnRegisterTeeTools([TGridTransposeTool]);
end.

