From 267a457efe0aff1ee69d565ae99117ed47f99043 Mon Sep 17 00:00:00 2001 From: ask Date: Tue, 12 Jul 2011 09:22:52 +0000 Subject: [PATCH] TAChart: Add and use TLegendItemsEnumerator git-svn-id: trunk@31674 - --- components/tachart/demo/legend/main.pas | 4 +-- components/tachart/tachartlistbox.pas | 6 ++--- components/tachart/talegend.pas | 36 ++++++++++++++++++------- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/components/tachart/demo/legend/main.pas b/components/tachart/demo/legend/main.pas index 51d6d1846a..b13d1ada5e 100644 --- a/components/tachart/demo/legend/main.pas +++ b/components/tachart/demo/legend/main.pas @@ -115,13 +115,13 @@ end; procedure TForm1.FormCreate(Sender: TObject); var - i: Integer; + li: TLegendItem; begin // Workaround for issue #19632 Chart1FuncSeries1.Legend.OnCreate := @Chart1FuncSeries1LegendCreate; FItems := Chart1.GetLegendItems; Chart1.Legend.SortItemsByOrder(FItems); - for i := 1 to FItems.Count do + for li in FItems do cbSeries.AddItem('', nil); end; diff --git a/components/tachart/tachartlistbox.pas b/components/tachart/tachartlistbox.pas index 4b0c15b5a5..fcab5bcc32 100644 --- a/components/tachart/tachartlistbox.pas +++ b/components/tachart/tachartlistbox.pas @@ -460,7 +460,7 @@ procedure TChartListbox.Populate; { populates the listbox with all series contained in the chart. Use the event OnPopulate if you don't omit special series from the listbox (RemoveSeries) } var - i: Integer; + li: TLegendItem; begin Items.BeginUpdate; try @@ -469,9 +469,9 @@ begin FreeAndNil(FLegendItems); FLegendItems := CreateLegendItems; Chart.Legend.SortItemsByOrder(FLegendItems); - for i := 0 to FLegendItems.Count - 1 do + for li in FLegendItems do // The caption is owner-drawn, but add it anyway for user convenience. - Items.AddObject(FLegendItems[i].Text, FLegendItems[i]); + Items.AddObject(li.Text, li); if Assigned(OnPopulate) then OnPopulate(Self); finally diff --git a/components/tachart/talegend.pas b/components/tachart/talegend.pas index 8792984438..3dba1c2021 100644 --- a/components/tachart/talegend.pas +++ b/components/tachart/talegend.pas @@ -111,6 +111,12 @@ type procedure Draw(ADrawer: IChartDrawer; const ARect: TRect); override; end; + TLegendItemsEnumerator = class(TListEnumerator) + public + function GetCurrent: TLegendItem; + property Current: TLegendItem read GetCurrent; + end; + { TChartLegendItems } TChartLegendItems = class(TObjectList) @@ -118,6 +124,7 @@ type function GetItem(AIndex: Integer): TLegendItem; procedure SetItem(AIndex: Integer; AValue: TLegendItem); public + function GetEnumerator: TLegendItemsEnumerator; property Items[AIndex: Integer]: TLegendItem read GetItem write SetItem; default; end; @@ -275,8 +282,20 @@ begin Result := Sign(li1.Order - li2.Order); end; +{ TLegendItemsEnumerator } + +function TLegendItemsEnumerator.GetCurrent: TLegendItem; +begin + Result := TLegendItem(inherited GetCurrent); +end; + { TChartLegendItems } +function TChartLegendItems.GetEnumerator: TLegendItemsEnumerator; +begin + Result := TLegendItemsEnumerator.Create(Self); +end; + function TChartLegendItems.GetItem(AIndex: Integer): TLegendItem; begin Result := TLegendItem(inherited GetItem(AIndex)); @@ -517,19 +536,18 @@ end; function TChartLegend.MeasureItem( ADrawer: IChartDrawer; AItems: TChartLegendItems): TPoint; var - i: Integer; p: TPoint; prevFont: TFont = nil; + li: TLegendItem; begin Result := Point(0, 0); - for i := 0 to AItems.Count - 1 do - with AItems[i] do begin - AItems[i].UpdateFont(ADrawer, prevFont); - p := ADrawer.TextExtent(Text); - if HasSymbol then - p.X += SYMBOL_TEXT_SPACING + SymbolWidth; - Result := MaxPoint(p, Result); - end; + for li in AItems do begin + li.UpdateFont(ADrawer, prevFont); + p := ADrawer.TextExtent(li.Text); + if li.HasSymbol then + p.X += SYMBOL_TEXT_SPACING + SymbolWidth; + Result := MaxPoint(p, Result); + end; end; procedure TChartLegend.Prepare(