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

interface

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

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

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

type
  TTransposeSeriesTool=class(TTeeCustomTool)
  protected
    class Function GetEditorClass:String; override;
  public
    class Function Description:String; override;
    class Function LongDescription:String; override;

    procedure Transpose;
  end;

implementation

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

{ TTransposeSeriesTool }

class function TTransposeSeriesTool.Description: String;
begin
  result:=TeeMsg_TransposeSeries;
end;

class function TTransposeSeriesTool.LongDescription: String;
begin
  result:=TeeMsg_TransposeSeriesDesc;
end;

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

procedure TTransposeSeriesTool.Transpose;
var tmp : Integer;
    t   : Integer;
    tt  : Integer;
    OldCount : Integer;
    tmpSeries : TChartSeries;
    Series : TChartSeriesList;
begin
  Series:=ParentChart.SeriesList;
  OldCount:=Series.Count;

  if OldCount>0 then
  begin
    // Get the maximum number of points in all series:
    tmp:=Series[0].Count;

    for t:=1 to OldCount-1 do
        tmp:=Max(tmp,Series[t].Count);

    for t:=0 to tmp-1 do
    begin
      tmpSeries:=Series.Owner.AddSeries(TChartSeriesClass(Series[0].ClassType));

      for tt:=0 to OldCount-1 do
          with Series[tt].MandatoryValueList do
          if Count>t then
             if Series[tt].IsNull(t) then
                tmpSeries.AddNull(Value[t])
             else
                tmpSeries.Add(Value[t])
          else
             tmpSeries.AddNull;
    end;

    for t:=1 to OldCount do
        {$IFDEF AUTOREFCOUNT}
        Series[0].DisposeOf;
        {$ELSE}
        Series[0].Free;
        {$ENDIF}
  end;
end;

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

