mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-08-17 20:59:12 +02:00
TAChart: Implement TChartLegend.Alignment property
git-svn-id: trunk@21893 -
This commit is contained in:
parent
b3570afa1e
commit
66ad4ed453
@ -60,6 +60,8 @@ type
|
|||||||
property Color default clWhite;
|
property Color default clWhite;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
TLegendAlignment = (laTopLeft, laBottomLeft, laTopRight, laBottomRight);
|
||||||
|
|
||||||
{ TChartLegend }
|
{ TChartLegend }
|
||||||
|
|
||||||
TChartLegend = class(TChartElement)
|
TChartLegend = class(TChartElement)
|
||||||
@ -89,7 +91,7 @@ type
|
|||||||
AItems: TObjectList; ACanvas: TCanvas; var AClipRect: TRect);
|
AItems: TObjectList; ACanvas: TCanvas; var AClipRect: TRect);
|
||||||
published
|
published
|
||||||
property Alignment: TLegendAlignment
|
property Alignment: TLegendAlignment
|
||||||
read FAlignment write SetAlignment default laRight;
|
read FAlignment write SetAlignment default laTopRight;
|
||||||
property BackgroundBrush: TChartLegendBrush
|
property BackgroundBrush: TChartLegendBrush
|
||||||
read FBackgroundBrush write SetBackgroundBrush;
|
read FBackgroundBrush write SetBackgroundBrush;
|
||||||
property Font: TFont read FFont write SetFont;
|
property Font: TFont read FFont write SetFont;
|
||||||
@ -186,7 +188,7 @@ end;
|
|||||||
constructor TChartLegend.Create(AOwner: TCustomChart);
|
constructor TChartLegend.Create(AOwner: TCustomChart);
|
||||||
begin
|
begin
|
||||||
inherited Create(AOwner);
|
inherited Create(AOwner);
|
||||||
FAlignment := laRight;
|
FAlignment := laTopRight;
|
||||||
FMargin := DEF_LEGEND_MARGIN;
|
FMargin := DEF_LEGEND_MARGIN;
|
||||||
FSpacing := DEF_LEGEND_SPACING;
|
FSpacing := DEF_LEGEND_SPACING;
|
||||||
FSymbolWidth := DEF_LEGEND_SYMBOL_WIDTH;
|
FSymbolWidth := DEF_LEGEND_SYMBOL_WIDTH;
|
||||||
@ -208,40 +210,52 @@ end;
|
|||||||
procedure TChartLegend.Draw(
|
procedure TChartLegend.Draw(
|
||||||
AItems: TObjectList; ACanvas: TCanvas; var AClipRect: TRect);
|
AItems: TObjectList; ACanvas: TCanvas; var AClipRect: TRect);
|
||||||
var
|
var
|
||||||
w, x1, y1, i, th: Integer;
|
w, x, y, i, textHeight, legendWidth, legendHeight: Integer;
|
||||||
pbf: TPenBrushFontRecall;
|
pbf: TPenBrushFontRecall;
|
||||||
r: TRect;
|
r: TRect;
|
||||||
begin
|
begin
|
||||||
if not Visible then exit;
|
if not Visible then exit;
|
||||||
|
|
||||||
// TODO: Legend.Alignment
|
|
||||||
|
|
||||||
pbf := TPenBrushFontRecall.Create(ACanvas, [pbfPen, pbfBrush, pbfFont]);
|
pbf := TPenBrushFontRecall.Create(ACanvas, [pbfPen, pbfBrush, pbfFont]);
|
||||||
try
|
try
|
||||||
ACanvas.Font.Assign(Font);
|
ACanvas.Font.Assign(Font);
|
||||||
w := 0;
|
|
||||||
|
// Measure the legend.
|
||||||
|
legendWidth := 0;
|
||||||
for i := 0 to AItems.Count - 1 do
|
for i := 0 to AItems.Count - 1 do
|
||||||
with AItems[i] as TLegendItem do
|
with AItems[i] as TLegendItem do
|
||||||
w := Max(ACanvas.TextWidth(FText), w);
|
legendWidth := Max(ACanvas.TextWidth(FText), legendWidth);
|
||||||
w += 2 * Spacing + SYMBOL_TEXT_SPACING + SymbolWidth;
|
legendWidth += 2 * Spacing + SYMBOL_TEXT_SPACING + SymbolWidth;
|
||||||
th := ACanvas.TextHeight('Iy');
|
textHeight := ACanvas.TextHeight('Iy');
|
||||||
|
legendHeight := Spacing + AItems.Count * (textHeight + Spacing);
|
||||||
|
w := legendWidth + 2 * Margin;
|
||||||
|
|
||||||
AClipRect.Right -= w + 2 * Margin;
|
// Determine position according to the alignment.
|
||||||
x1 := AClipRect.Right + Margin;
|
if Alignment in [laTopLeft, laBottomLeft] then begin
|
||||||
y1 := AClipRect.Top;
|
x := AClipRect.Left + Margin;
|
||||||
|
AClipRect.Left += w;
|
||||||
|
end
|
||||||
|
else begin
|
||||||
|
x := AClipRect.Right - legendWidth - Margin;
|
||||||
|
AClipRect.Right -= w;
|
||||||
|
end;
|
||||||
|
if Alignment in [laTopLeft, laTopRight] then
|
||||||
|
y := AClipRect.Top + Margin
|
||||||
|
else
|
||||||
|
y := AClipRect.Bottom - Margin - legendHeight;
|
||||||
|
|
||||||
// Border
|
// Draw the background and the border.
|
||||||
ACanvas.Brush.Assign(BackgroundBrush);
|
ACanvas.Brush.Assign(BackgroundBrush);
|
||||||
ACanvas.Pen.Assign(Frame);
|
ACanvas.Pen.Assign(Frame);
|
||||||
ACanvas.Rectangle(Bounds(
|
ACanvas.Rectangle(Bounds(x, y, legendWidth, legendHeight));
|
||||||
x1, y1, w, Spacing + AItems.Count * (th + Spacing)));
|
|
||||||
|
|
||||||
r := Bounds(x1 + Spacing, y1 + Spacing, SymbolWidth, th);
|
// Draw items.
|
||||||
|
r := Bounds(x + Spacing, y + Spacing, SymbolWidth, textHeight);
|
||||||
for i := 0 to AItems.Count - 1 do begin
|
for i := 0 to AItems.Count - 1 do begin
|
||||||
ACanvas.Brush.Assign(BackgroundBrush);
|
ACanvas.Brush.Assign(BackgroundBrush);
|
||||||
ACanvas.Pen.Assign(Frame);
|
ACanvas.Pen.Assign(Frame);
|
||||||
(AItems[i] as TLegendItem).Draw(ACanvas, r);
|
(AItems[i] as TLegendItem).Draw(ACanvas, r);
|
||||||
OffsetRect(r, 0, th + Spacing);
|
OffsetRect(r, 0, textHeight + Spacing);
|
||||||
end;
|
end;
|
||||||
finally
|
finally
|
||||||
pbf.Free;
|
pbf.Free;
|
||||||
|
@ -56,8 +56,6 @@ type
|
|||||||
property Visible: Boolean read FVisible write SetVisible default true;
|
property Visible: Boolean read FVisible write SetVisible default true;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
TLegendAlignment = (laLeft, laRight, laTop, laBottom);
|
|
||||||
|
|
||||||
TFPCanvasHelperClass = class of TFPCanvasHelper;
|
TFPCanvasHelperClass = class of TFPCanvasHelper;
|
||||||
|
|
||||||
{ TChartElement }
|
{ TChartElement }
|
||||||
|
Loading…
Reference in New Issue
Block a user