{**********************************************}
{   TCumulative Function Component             }
{   Copyright (c) 1997-2020 by Steema Software }
{**********************************************}
unit TeeCumu;
{$I TeeDefs.inc}

interface

{ The TCumulative function sums the Series values
  starting from the first point.

  Example:

  Given these values               :  1 2 3

  The TCumulative function returns :  1 3 6  ( 1=1, 1+2=3 and 1+2+3=6 )
}

uses Classes,
     {$IFDEF FMX}
     FMXTee.Engine, FMXTee.Chart
     {$ELSE}
     TeEngine, Chart
     {$ENDIF};

type
  TCumulative = class(TTeeFunction)
  protected
    procedure PrepareForGallery(const Chart:TCustomAxisPanel); override;
  public
    { Public declarations }
    Constructor Create(AOwner: TComponent); override;

    Function Calculate(const Series:TChartSeries; First,Last:Integer):Double; override;
    Function CalculateMany(const SeriesList:TDataSourcesList; ValueIndex:Integer):Double;  override;
  published
    { Published declarations }
    property Period;
  end;

implementation

Uses
  {$IFDEF FMX}
  FMXTee.Constants, FMXTee.ProConstants
  {$ELSE}
  TeeProCo, TeeConst
  {$ENDIF};

{ TCumulative }
Constructor TCumulative.Create(AOwner: TComponent);
Begin
  inherited;
  InternalSetPeriod(1);
end;

Function TCumulative.Calculate(const Series:TChartSeries; First,Last:Integer):Double;
  Begin
    if First>0 then result:=ParentSeries.MandatoryValueList.Last
               else result:=0;

    if First>=0 then
       result:=result+ValueList(Series).Value[First];
end;

{ Returns the sum( ) of the current ValueIndex point of all Series PLUS the
  accumulated calculation of the previous ValueIndex point.
}
Function TCumulative.CalculateMany(const SeriesList:TDataSourcesList; ValueIndex:Integer):Double;
var t       : Integer;
    tmpList : TChartValueList;
begin
  if ValueIndex=0 then result:=0
                  else result:=ParentSeries.MandatoryValueList.Value[ValueIndex-1];

  for t:=0 to SeriesList.Count-1 do
  begin
    tmpList:=ValueList(TChartSeries(SeriesList[t]));

    if tmpList.Count>ValueIndex then
       result:=result+tmpList.Value[ValueIndex];
  end;
end;

procedure TCumulative.PrepareForGallery(const Chart: TCustomAxisPanel);
begin
  inherited;
  ParentSeries.VertAxis:=aRightAxis;
end;

initialization
  RegisterTeeFunction( TCumulative, @TeeMsg_FunctionCumulative,
                                    @TeeMsg_GalleryExtended, 2 );
finalization
  UnRegisterTeeFunctions([TCumulative]);
end.
