From 09b0ea83ae76025acd5eecb676cda5d656486c29 Mon Sep 17 00:00:00 2001 From: ask Date: Mon, 4 Oct 2010 07:28:23 +0000 Subject: [PATCH] TAChart: Add OnDrawLegend event to chart series git-svn-id: trunk@27555 - --- components/tachart/tacustomseries.pas | 31 ++++++++++++++++++++++++--- components/tachart/tagraph.pas | 4 ++-- components/tachart/talegend.pas | 30 ++++++++++++++++++++++++++ components/tachart/taseries.pas | 3 +++ 4 files changed, 63 insertions(+), 5 deletions(-) diff --git a/components/tachart/tacustomseries.pas b/components/tachart/tacustomseries.pas index ce0a6d128d..b794dcde57 100644 --- a/components/tachart/tacustomseries.pas +++ b/components/tachart/tacustomseries.pas @@ -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 diff --git a/components/tachart/tagraph.pas b/components/tachart/tagraph.pas index fff07cc98b..f84096784e 100644 --- a/components/tachart/tagraph.pas +++ b/components/tachart/tagraph.pas @@ -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); diff --git a/components/tachart/talegend.pas b/components/tachart/talegend.pas index 6320e4d562..329689d5c3 100644 --- a/components/tachart/talegend.pas +++ b/components/tachart/talegend.pas @@ -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); diff --git a/components/tachart/taseries.pas b/components/tachart/taseries.pas index 60ab3dfc20..ccad37910f 100644 --- a/components/tachart/taseries.pas +++ b/components/tachart/taseries.pas @@ -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;