From e6b1840fc4bafcf4be7dc19e2b3a6e05494595eb Mon Sep 17 00:00:00 2001 From: ask Date: Sun, 26 Jun 2011 08:45:35 +0000 Subject: [PATCH] TAChart: Add TChartSeriesLegend.Order property git-svn-id: trunk@31395 - --- components/tachart/demo/legend/main.lfm | 3 ++- components/tachart/tacustomseries.pas | 7 ++++++- components/tachart/tagraph.pas | 7 +++++++ components/tachart/talegend.pas | 27 ++++++++++++++++++++++++- 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/components/tachart/demo/legend/main.lfm b/components/tachart/demo/legend/main.lfm index 1d35ff0111..a64ffbfe3b 100644 --- a/components/tachart/demo/legend/main.lfm +++ b/components/tachart/demo/legend/main.lfm @@ -60,8 +60,9 @@ object Form1: TForm1 Source = ListChartSource2 end object Chart1FuncSeries1: TFuncSeries - Legend.OnDraw = Chart1FuncSeries1DrawLegend + Legend.Order = 0 Legend.UserItemsCount = 2 + Legend.OnDraw = Chart1FuncSeries1DrawLegend Extent.XMax = 5 Extent.UseXMin = True Extent.UseXMax = True diff --git a/components/tachart/tacustomseries.pas b/components/tachart/tacustomseries.pas index f870a9ed7c..e7acae6e92 100644 --- a/components/tachart/tacustomseries.pas +++ b/components/tachart/tacustomseries.pas @@ -311,13 +311,18 @@ end; procedure TCustomChartSeries.GetLegendItemsBasic(AItems: TChartLegendItems); var - i: Integer; + i, oldCount: Integer; begin + oldCount := AItems.Count; if Assigned(Legend.OnDraw) then for i := 0 to Legend.UserItemsCount - 1 do AItems.Add(TLegendItemUserDrawn.Create(i, Legend.OnDraw, Title)) else GetLegendItems(AItems); + for i := oldCount to AItems.Count - 1 do + with AItems[i] do + if Order = LEGEND_ITEM_ORDER_AS_ADDED then + Order := Legend.Order; end; function TCustomChartSeries.GetNearestPoint( diff --git a/components/tachart/tagraph.pas b/components/tachart/tagraph.pas index a83007f80e..b5551940e7 100644 --- a/components/tachart/tagraph.pas +++ b/components/tachart/tagraph.pas @@ -927,6 +927,7 @@ end; function TChart.GetLegendItems(AIncludeHidden: Boolean): TChartLegendItems; var i: Integer; + j: Integer = MaxInt; begin Result := TChartLegendItems.Create; try @@ -934,6 +935,12 @@ begin with Series[i] do if AIncludeHidden or (Active and GetShowInLegend) then GetLegendItemsBasic(Result); + for i := Result.Count - 1 downto 0 do + if Result[i].Order = LEGEND_ITEM_ORDER_AS_ADDED then begin + Result[i].Order := j; + j -= 1; + end; + Result.Sort(@LegendItemCompare); except FreeAndNil(Result); raise; diff --git a/components/tachart/talegend.pas b/components/tachart/talegend.pas index 42c3e47268..9f0320f7a7 100644 --- a/components/tachart/talegend.pas +++ b/components/tachart/talegend.pas @@ -27,6 +27,7 @@ const DEF_LEGEND_SPACING = 4; DEF_LEGEND_MARGIN = 4; DEF_LEGEND_SYMBOL_WIDTH = 20; + LEGEND_ITEM_ORDER_AS_ADDED = -1; type { TLegendItem } @@ -34,12 +35,14 @@ type TLegendItem = class private FColor: TColor; + FOrder: Integer; FText: String; public constructor Create(const AText: String; AColor: TColor = clTAColor); procedure Draw(ADrawer: IChartDrawer; const ARect: TRect); virtual; public property Color: TColor read FColor write FColor; + property Order: Integer read FOrder write FOrder; end; TLegendItemDrawEvent = procedure ( @@ -181,9 +184,11 @@ type private FMultiplicity: TLegendMultiplicity; FOnDraw: TLegendItemDrawEvent; + FOrder: Integer; FUserItemsCount: Integer; procedure SetMultiplicity(AValue: TLegendMultiplicity); procedure SetOnDraw(AValue: TLegendItemDrawEvent); + procedure SetOrder(AValue: Integer); procedure SetUserItemsCount(AValue: Integer); public constructor Create(AOwner: TCustomChart); @@ -192,12 +197,18 @@ type published property Multiplicity: TLegendMultiplicity read FMultiplicity write SetMultiplicity default lmSingle; - property OnDraw: TLegendItemDrawEvent read FOnDraw write SetOnDraw; + property Order: Integer + read FOrder write SetOrder default LEGEND_ITEM_ORDER_AS_ADDED; property UserItemsCount: Integer read FUserItemsCount write SetUserItemsCount default 1; property Visible default true; + + published + property OnDraw: TLegendItemDrawEvent read FOnDraw write SetOnDraw; end; + function LegendItemCompare(AItem1, AItem2: Pointer): Integer; + implementation uses @@ -206,6 +217,11 @@ uses const SYMBOL_TEXT_SPACING = 4; +function LegendItemCompare(AItem1, AItem2: Pointer): Integer; +begin + Result := Sign(TLegendItem(AItem1).Order - TLegendItem(AItem2).Order); +end; + { TChartLegendItems } function TChartLegendItems.GetItem(AIndex: Integer): TLegendItem; @@ -223,6 +239,7 @@ end; constructor TLegendItem.Create(const AText: String; AColor: TColor); begin FColor := AColor; + FOrder := LEGEND_ITEM_ORDER_AS_ADDED; FText := AText; end; @@ -548,6 +565,7 @@ end; constructor TChartSeriesLegend.Create(AOwner: TCustomChart); begin inherited Create(AOwner); + FOrder := LEGEND_ITEM_ORDER_AS_ADDED; FVisible := true; FUserItemsCount := 1; end; @@ -566,6 +584,13 @@ begin StyleChanged(Self); end; +procedure TChartSeriesLegend.SetOrder(AValue: Integer); +begin + if FOrder = AValue then exit; + FOrder := AValue; + StyleChanged(Self); +end; + procedure TChartSeriesLegend.SetUserItemsCount(AValue: Integer); begin if FUserItemsCount = AValue then exit;