TAChart: Add OnDrawLegend event to chart series

git-svn-id: trunk@27555 -
This commit is contained in:
ask 2010-10-04 07:28:23 +00:00
parent 73fda95380
commit 09b0ea83ae
4 changed files with 63 additions and 5 deletions

View File

@ -24,7 +24,7 @@ interface
uses
Classes, Graphics, SysUtils,
TAChartAxis, TAChartUtils, TAGraph, TASources, TAStyles, TATypes;
TAChartAxis, TAChartUtils, TAGraph, TALegend, TASources, TAStyles, TATypes;
const
DEF_AXIS_INDEX = -1;
@ -36,11 +36,15 @@ type
private
FAxisIndexX: Integer;
FAxisIndexY: Integer;
FOnDrawLegend: TLegendItemDrawEvent;
procedure SetAxisIndexX(AValue: Integer);
procedure SetAxisIndexY(AValue: Integer);
procedure SetOnDrawLegend(AValue: TLegendItemDrawEvent);
protected
procedure GetGraphBounds(var ABounds: TDoubleRect); override;
procedure GetLegendItems(AItems: TChartLegendItems); virtual; abstract;
procedure GetLegendItemsBasic(AItems: TChartLegendItems); override;
procedure SetActive(AValue: Boolean); override;
procedure SetDepth(AValue: TChartDistance); override;
procedure SetShowInLegend(AValue: Boolean); override;
@ -72,8 +76,13 @@ type
function GetParentComponent: TComponent; override;
function HasParent: Boolean; override;
property AxisIndexX: Integer read FAxisIndexX write SetAxisIndexX default DEF_AXIS_INDEX;
property AxisIndexY: Integer read FAxisIndexY write SetAxisIndexY default DEF_AXIS_INDEX;
property AxisIndexX: Integer
read FAxisIndexX write SetAxisIndexX default DEF_AXIS_INDEX;
property AxisIndexY: Integer
read FAxisIndexY write SetAxisIndexY default DEF_AXIS_INDEX;
property OnDrawLegend: TLegendItemDrawEvent
read FOnDrawLegend write SetOnDrawLegend;
end;
TChartGetMarkEvent = procedure (
@ -152,6 +161,7 @@ type
property Title;
property ZPosition;
published
property OnDrawLegend;
property OnGetMark: TChartGetMarkEvent read FOnGetMark write SetOnGetMark;
end;
@ -254,6 +264,14 @@ begin
Result := FChart.Series.List.IndexOf(Self);
end;
procedure TCustomChartSeries.GetLegendItemsBasic(AItems: TChartLegendItems);
begin
if Assigned(FOnDrawLegend) then
AItems.Add(TLegendItemUserDrawn.Create(OnDrawLegend, Title))
else
GetLegendItems(AItems);
end;
function TCustomChartSeries.GetParentComponent: TComponent;
begin
Result := FChart;
@ -322,6 +340,13 @@ begin
Move(Index, EnsureRange(AValue, 0, Count - 1));
end;
procedure TCustomChartSeries.SetOnDrawLegend(AValue: TLegendItemDrawEvent);
begin
if FOnDrawLegend = AValue then exit;
FOnDrawLegend := AValue;
UpdateParentChart;
end;
procedure TCustomChartSeries.SetParentComponent(AParent: TComponent);
begin
if not (csLoading in ComponentState) then

View File

@ -59,7 +59,7 @@ type
// Some or all bounds may be left unset, in which case they will be ignored.
procedure GetBounds(var ABounds: TDoubleRect); virtual; abstract;
procedure GetGraphBounds(var ABounds: TDoubleRect); virtual; abstract;
procedure GetLegendItems(AItems: TChartLegendItems); virtual; abstract;
procedure GetLegendItemsBasic(AItems: TChartLegendItems); virtual; abstract;
procedure SetActive(AValue: Boolean); virtual; abstract;
procedure SetDepth(AValue: TChartDistance); virtual; abstract;
procedure SetShowInLegend(AValue: Boolean); virtual; abstract;
@ -510,7 +510,7 @@ begin
for i := 0 to SeriesCount - 1 do
with Series[i] do
if Active and ShowInLegend then
GetLegendItems(ALegendItems);
GetLegendItemsBasic(ALegendItems);
ARect := Legend.Prepare(ACanvas, ALegendItems, FClipRect);
except
FreeAndNil(ALegendItems);

View File

@ -38,6 +38,20 @@ type
procedure Draw(ACanvas: TCanvas; const ARect: TRect); virtual;
end;
TLegendItemDrawEvent =
procedure (ACanvas: TCanvas; const ARect: TRect) of object;
{ TLegendItemUserDrawn }
TLegendItemUserDrawn = class(TLegendItem)
private
FOnDraw: TLegendItemDrawEvent;
public
constructor Create(AOnDraw: TLegendItemDrawEvent; const AText: String);
procedure Draw(ACanvas: TCanvas; const ARect: TRect); override;
property OnDraw: TLegendItemDrawEvent read FOnDraw;
end;
{ TLegendItemLine }
TLegendItemLine = class(TLegendItem)
@ -164,6 +178,22 @@ begin
ACanvas.TextOut(ARect.Right + SYMBOL_TEXT_SPACING, ARect.Top, FText);
end;
{ TLegendItemUserDrawn }
constructor TLegendItemUserDrawn.Create(
AOnDraw: TLegendItemDrawEvent; const AText: String);
begin
inherited Create(AText);
FOnDraw := AOnDraw;
end;
procedure TLegendItemUserDrawn.Draw(ACanvas: TCanvas; const ARect: TRect);
begin
inherited Draw(ACanvas, ARect);
if Assigned(FOnDraw) then
FOnDraw(ACanvas, ARect);
end;
{ TLegendItemLine }
constructor TLegendItemLine.Create(APen: TPen; const AText: String);

View File

@ -248,6 +248,7 @@ type
property Active default true;
property LineStyle: TLineStyle
read FLineStyle write SetLineStyle default lsHorizontal;
property OnDrawLegend;
property Pen: TPen read FPen write SetPen;
property Position: Double read FPosGraph write SetPos;
property SeriesColor: TColor
@ -297,6 +298,7 @@ type
property AxisIndexY;
property Extent: TChartExtent read FExtent write SetExtent;
property OnCalculate: TFuncCalculateEvent read FOnCalculate write SetOnCalculate;
property OnDrawLegend;
property Pen: TChartPen read FPen write SetPen;
property ShowInLegend;
property Step: TFuncSeriesStep read FStep write SetStep default 2;
@ -326,6 +328,7 @@ type
property ZPosition;
published
property OnDraw: TSeriesDrawEvent read FOnDraw write SetOnDraw;
property OnDrawLegend;
property OnGetBounds: TSeriesGetBoundsEvent
read FOnGetBounds write SetOnGetBounds;
end;