diff --git a/components/tachart/tacustomseries.pas b/components/tachart/tacustomseries.pas index 09f58f0817..e851a3dcce 100644 --- a/components/tachart/tacustomseries.pas +++ b/components/tachart/tacustomseries.pas @@ -605,6 +605,7 @@ begin inherited; Marks.SetOwner(FChart); Marks.Arrow.SetOwner(FChart); + Marks.Margins.SetOwner(FChart); end; procedure TChartSeries.AfterDraw; diff --git a/components/tachart/tatextelements.pas b/components/tachart/tatextelements.pas index b2c0d0d395..d2b0b9cab7 100644 --- a/components/tachart/tatextelements.pas +++ b/components/tachart/tatextelements.pas @@ -25,18 +25,28 @@ uses TAChartUtils, TADrawUtils, TATypes; const - MARKS_MARGIN_X = 4; - MARKS_MARGIN_Y = 2; + DEF_LABEL_MARGIN_X = 4; + DEF_LABEL_MARGIN_Y = 2; type TChartMarksOverlapPolicy = (opIgnore, opHideNeighbour); + TChartLabelMargins = class(TChartMargins) + published + property Bottom default DEF_LABEL_MARGIN_Y; + property Left default DEF_LABEL_MARGIN_X; + property Right default DEF_LABEL_MARGIN_X; + property Top default DEF_LABEL_MARGIN_Y; + end; + TChartTextElement = class(TChartElement) strict private FClipped: Boolean; + FMargins: TChartLabelMargins; FOverlapPolicy: TChartMarksOverlapPolicy; procedure SetAlignment(AValue: TAlignment); procedure SetClipped(AValue: Boolean); + procedure SetMargins(AValue: TChartLabelMargins); procedure SetOverlapPolicy(AValue: TChartMarksOverlapPolicy); strict protected FAlignment: TAlignment; @@ -54,6 +64,7 @@ type function GetLinkPen: TChartPen; virtual; public constructor Create(AOwner: TCustomChart); + destructor Destroy; override; public procedure Assign(ASource: TPersistent); override; procedure DrawLabel( @@ -70,6 +81,7 @@ type published property Alignment: TAlignment read FAlignment write SetAlignment; + property Margins: TChartLabelMargins read FMargins write SetMargins; end; TChartTitleFramePen = class(TChartPen) @@ -256,9 +268,16 @@ constructor TChartTextElement.Create(AOwner: TCustomChart); begin inherited Create(AOwner); FClipped := true; + FMargins := TChartLabelMargins.Create(AOwner); FOverlapPolicy := opIgnore; end; +destructor TChartTextElement.Destroy; +begin + FreeAndNil(FMargins); + inherited; +end; + procedure TChartTextElement.DrawLabel( ADrawer: IChartDrawer; const ADataPoint, ALabelCenter: TPoint; const AText: String; var APrevLabelPoly: TPointArray); @@ -319,8 +338,7 @@ begin Result := ZeroRect; InflateRect(Result, ATextSize.X div 2, ATextSize.Y div 2); if IsMarginRequired then - with ADrawer do - InflateRect(Result, Scale(MARKS_MARGIN_X), Scale(MARKS_MARGIN_Y)); + Margins.ExpandRectScaled(ADrawer, Result); end; function TChartTextElement.GetLabelAngle: Double; @@ -368,6 +386,13 @@ begin StyleChanged(Self); end; +procedure TChartTextElement.SetMargins(AValue: TChartLabelMargins); +begin + if FMargins = AValue then exit; + FMargins.Assign(AValue); + StyleChanged(Self); +end; + procedure TChartTextElement.SetOverlapPolicy(AValue: TChartMarksOverlapPolicy); begin if FOverlapPolicy = AValue then exit; diff --git a/components/tachart/tatypes.pas b/components/tachart/tatypes.pas index 04f5962c56..1778325dd0 100644 --- a/components/tachart/tatypes.pas +++ b/components/tachart/tatypes.pas @@ -189,6 +189,7 @@ type procedure SetValue(AIndex: Integer; AValue: TChartDistance); public procedure Assign(Source: TPersistent); override; + procedure ExpandRectScaled(ADrawer: IChartDrawer; var ARect: TRect); property Data: TRect read FData.FRect; published property Left: TChartDistance index 1 read GetValue write SetValue default DEF_MARGIN; @@ -547,7 +548,14 @@ end; constructor TChartMargins.Create(AOwner: TCustomChart); begin inherited Create(AOwner); - FData.FRect := Rect(DEF_MARGIN, DEF_MARGIN, DEF_MARGIN, DEF_MARGIN); + SetPropDefaults(Self, ['Left', 'Top', 'Right', 'Bottom']); +end; + +procedure TChartMargins.ExpandRectScaled( + ADrawer: IChartDrawer; var ARect: TRect); +begin + ARect.TopLeft -= Point(ADrawer.Scale(Left), ADrawer.Scale(Top)); + ARect.BottomRight += Point(ADrawer.Scale(Right), ADrawer.Scale(Bottom)); end; function TChartMargins.GetValue(AIndex: Integer): Integer;