{*********************************************}
{  TeeExtraLegend Tool                        }
{  Copyright (c) 2002-2020 by Steema Software }
{  All Rights Reserved                        }
{*********************************************}
unit TeeExtraLegendTool;
{$I TeeDefs.inc}

interface

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

     {$IFDEF FMX}
     FMX.Types,
     FMXTee.Engine, FMXTee.Canvas, FMXTee.Chart, FMXTee.Tools
     {$ELSE}
     Chart, TeEngine, TeeTools, TeCanvas
     {$ENDIF}
     ;

type
  TExtraLegendTool=class(TTeeCustomToolSeries)
  private
    FLegend: TChartLegend;
    function GetLegend:TChartLegend;
    procedure SetLegend(const Value: TChartLegend);
  protected
    procedure ChartEvent(AEvent: TChartToolEvent); override;
    class Function GetEditorClass:String; override;
    procedure SetParentChart(const Value: TCustomAxisPanel); override;
    procedure SetSeries(const Value: TChartSeries); override;
  public
    Destructor Destroy; override;
    class Function Description:String; override;
    class Function LongDescription:String; override;
  published
    property Active;
    property Legend:TChartLegend read GetLegend write SetLegend;
    property Series;
  end;

implementation

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

type
  TLegendAccess=class(TChartLegend);

function TExtraLegendTool.GetLegend:TChartLegend;
begin
  if not Assigned(FLegend) then
  begin
    FLegend:=TChartLegend.Create(ParentChart);
    TLegendAccess(FLegend).DefaultCustom:=True;
    FLegend.CustomPosition:=True;
    FLegend.LegendStyle:=lsValues;
  end;

  result:=FLegend;
end;

Destructor TExtraLegendTool.Destroy;
begin
  FreeAndNil(FLegend);
  inherited;
end;

procedure TExtraLegendTool.SetLegend(const Value: TChartLegend);
begin
  FLegend.Assign(Value);
end;

procedure TExtraLegendTool.SetParentChart(const Value: TCustomAxisPanel);
var tmp : TNotifyEvent;
begin
  inherited;

  {$IFDEF IOS}
  tmp:=nil;
  {$ENDIF}

  if Assigned(FLegend) then
  begin
    if Assigned(Value) then tmp:=Value.CanvasChanged
                       else tmp:=nil;

    FLegend.DividingLines.OnChange:=tmp;
    FLegend.ParentChart:=Value;
  end;
end;

procedure TExtraLegendTool.ChartEvent(AEvent: TChartToolEvent);
begin
  inherited;

  if AEvent=cteAfterDraw then
  begin
    if Assigned(ParentChart) and Assigned(Series) then
    begin
      Legend.Series:=Series; // call getter

      if Legend.Visible then
      begin
        TLegendAccess(Legend).CalcRect;
        Legend.DrawLegend;
      end;
    end;
  end;
end;

class function TExtraLegendTool.GetEditorClass: String;
begin
  result:='TExtraLegendEditor'; // Do not localize
end;

class function TExtraLegendTool.Description: String;
begin
  result:=TeeMsg_ExtraLegendTool;
end;

procedure TExtraLegendTool.SetSeries(const Value: TChartSeries);
begin
  inherited;
  Repaint;
end;

class function TExtraLegendTool.LongDescription: String;
begin
  result:=TeeMsg_ExtraLegendDesc;
end;

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