diff --git a/components/tachart/demo/legend/main.lfm b/components/tachart/demo/legend/main.lfm index 661672ef45..d1ccb57ea6 100644 --- a/components/tachart/demo/legend/main.lfm +++ b/components/tachart/demo/legend/main.lfm @@ -65,7 +65,7 @@ object Form1: TForm1 Source = ListChartSource2 end object Chart1FuncSeries1: TFuncSeries - Legend.Order = 0 + Legend.Order = 1 Legend.UserItemsCount = 2 Legend.OnDraw = Chart1FuncSeries1DrawLegend Extent.XMax = 5 diff --git a/components/tachart/demo/legend/main.pas b/components/tachart/demo/legend/main.pas index 44b16e5216..fa5271f0a4 100644 --- a/components/tachart/demo/legend/main.pas +++ b/components/tachart/demo/legend/main.pas @@ -39,6 +39,8 @@ type procedure Chart1FuncSeries1Calculate(const AX: Double; out AY: Double); procedure Chart1FuncSeries1DrawLegend( ACanvas: TCanvas; const ARect: TRect; AIndex: Integer; var AText: String); + procedure Chart1FuncSeries1LegendCreate( + AItem: TLegendItem; AIndex: Integer); procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure rgAlignmentClick(Sender: TObject); @@ -85,12 +87,19 @@ begin AY := Sin(AX * 2) + 7; end; +procedure TForm1.Chart1FuncSeries1LegendCreate( + AItem: TLegendItem; AIndex: Integer); +begin + AItem.Text := 'Function ' + IntToStr(AIndex); + if AIndex = 1 then + AItem.Order := 0; +end; + procedure TForm1.Chart1FuncSeries1DrawLegend( ACanvas: TCanvas; const ARect: TRect; AIndex: Integer; var AText: String); var x, y0, w: Integer; begin - AText := 'Function ' + IntToStr(AIndex); ACanvas.Pen := Chart1FuncSeries1.Pen; y0 := (ARect.Top + ARect.Bottom) div 2; ACanvas.MoveTo(ARect.Left, y0); @@ -105,6 +114,8 @@ procedure TForm1.FormCreate(Sender: TObject); var i: Integer; begin + // Workaround for issue #19632 + Chart1FuncSeries1.Legend.OnCreate := @Chart1FuncSeries1LegendCreate; FItems := Chart1.GetLegendItems; Chart1.Legend.SortItemsByOrder(FItems); for i := 1 to FItems.Count do diff --git a/components/tachart/tacustomseries.pas b/components/tachart/tacustomseries.pas index ee344430e4..d64c15ad99 100644 --- a/components/tachart/tacustomseries.pas +++ b/components/tachart/tacustomseries.pas @@ -320,14 +320,7 @@ begin else GetLegendItems(AItems); for i := oldCount to AItems.Count - 1 do - with AItems[i] do begin - if Font = nil then - Font := FChart.Legend.Font; - if GroupIndex = LEGEND_ITEM_NO_GROUP then - GroupIndex := Legend.GroupIndex; - if Order = LEGEND_ITEM_ORDER_AS_ADDED then - Order := Legend.Order; - end; + Legend.InitItem(AItems[i], i - oldCount, FChart.Legend); end; function TCustomChartSeries.GetNearestPoint( diff --git a/components/tachart/talegend.pas b/components/tachart/talegend.pas index 50011c7f03..f1629d5b36 100644 --- a/components/tachart/talegend.pas +++ b/components/tachart/talegend.pas @@ -34,7 +34,7 @@ type { TLegendItem } TLegendItem = class - private + strict private FColor: TColor; FFont: TFont; FGroupIndex: Integer; @@ -50,7 +50,7 @@ type property Font: TFont read FFont write FFont; property GroupIndex: Integer read FGroupIndex write FGroupIndex; property Order: Integer read FOrder write FOrder; - property Text: String read FText; + property Text: String read FText write FText; end; { TLegendItemGroupTitle } @@ -202,24 +202,31 @@ type TLegendMultiplicity = (lmSingle, lmPoint); + TLegendItemCreateEvent = procedure ( + AItem: TLegendItem; AIndex: Integer) of object; + { TChartSeriesLegend } TChartSeriesLegend = class(TChartElement) private FGroupIndex: Integer; FMultiplicity: TLegendMultiplicity; + FOnCreate: TLegendItemCreateEvent; FOnDraw: TLegendItemDrawEvent; FOrder: Integer; FUserItemsCount: Integer; procedure SetGroupIndex(AValue: Integer); procedure SetMultiplicity(AValue: TLegendMultiplicity); procedure SetOnDraw(AValue: TLegendItemDrawEvent); + procedure SetOnCreate(AValue: TLegendItemCreateEvent); procedure SetOrder(AValue: Integer); procedure SetUserItemsCount(AValue: Integer); public constructor Create(AOwner: TCustomChart); public procedure Assign(Source: TPersistent); override; + procedure InitItem( + AItem: TLegendItem; AIndex: Integer; ALegend: TChartLegend); published property GroupIndex: Integer read FGroupIndex write SetGroupIndex default LEGEND_ITEM_NO_GROUP; @@ -232,6 +239,7 @@ type property Visible default true; published + property OnCreate: TLegendItemCreateEvent read FOnCreate write SetOnCreate; property OnDraw: TLegendItemDrawEvent read FOnDraw write SetOnDraw; end; @@ -318,9 +326,13 @@ end; procedure TLegendItemUserDrawn.Draw(ADrawer: IChartDrawer; const ARect: TRect); var ic: IChartTCanvasDrawer; + t: String; begin - if Supports(ADrawer, IChartTCanvasDrawer, ic) and Assigned(FOnDraw) then - FOnDraw(ic.Canvas, ARect, FIndex, FText); + if Supports(ADrawer, IChartTCanvasDrawer, ic) and Assigned(FOnDraw) then begin + t := Text; + FOnDraw(ic.Canvas, ARect, FIndex, t); + Text := t; + end; inherited Draw(ADrawer, ARect); end; @@ -670,6 +682,19 @@ begin FUserItemsCount := 1; end; +procedure TChartSeriesLegend.InitItem( + AItem: TLegendItem; AIndex: Integer; ALegend: TChartLegend); +begin + if Assigned(OnCreate) then + OnCreate(AItem, AIndex); + if AItem.Font = nil then + AItem.Font := ALegend.Font; + if AItem.GroupIndex = LEGEND_ITEM_NO_GROUP then + AItem.GroupIndex := GroupIndex; + if AItem.Order = LEGEND_ITEM_ORDER_AS_ADDED then + AItem.Order := Order; +end; + procedure TChartSeriesLegend.SetGroupIndex(AValue: Integer); begin if FGroupIndex = AValue then exit; @@ -691,6 +716,13 @@ begin StyleChanged(Self); end; +procedure TChartSeriesLegend.SetOnCreate(AValue: TLegendItemCreateEvent); +begin + if TMethod(FOnCreate) = TMethod(AValue) then exit; + FOnCreate := AValue; + StyleChanged(Self); +end; + procedure TChartSeriesLegend.SetOrder(AValue: Integer); begin if FOrder = AValue then exit;