TAChart: Implement centered legend alignments. Update legend demo.

git-svn-id: trunk@29985 -
This commit is contained in:
ask 2011-03-22 20:21:55 +00:00
parent f965d18fea
commit 8a2a7fedfd
3 changed files with 75 additions and 44 deletions

View File

@ -1,17 +1,17 @@
object Form1: TForm1
Left = 1322
Height = 361
Height = 390
Top = 181
Width = 510
Caption = 'Form1'
ClientHeight = 361
ClientHeight = 390
ClientWidth = 510
OnCreate = FormCreate
Position = poScreenCenter
LCLVersion = '0.9.31'
object Chart1: TChart
Left = 0
Height = 299
Height = 300
Top = 0
Width = 510
AxisList = <
@ -68,11 +68,11 @@ object Form1: TForm1
end
object pnControls: TPanel
Left = 0
Height = 62
Top = 299
Height = 90
Top = 300
Width = 510
Align = alBottom
ClientHeight = 62
ClientHeight = 90
ClientWidth = 510
TabOrder = 1
object lblMarginY: TLabel
@ -112,10 +112,10 @@ object Form1: TForm1
ParentColor = False
end
object rgAlignment: TRadioGroup
Left = 324
Height = 60
Left = 264
Height = 88
Top = 1
Width = 185
Width = 245
Align = alRight
AutoFill = True
Caption = ' Alignment '
@ -126,24 +126,29 @@ object Form1: TForm1
ChildSizing.ShrinkHorizontal = crsScaleChilds
ChildSizing.ShrinkVertical = crsScaleChilds
ChildSizing.Layout = cclTopToBottomThenLeftToRight
ChildSizing.ControlsPerLine = 2
ClientHeight = 42
ClientWidth = 181
Columns = 2
ItemIndex = 2
ChildSizing.ControlsPerLine = 3
ClientHeight = 70
ClientWidth = 241
Columns = 3
ItemIndex = 6
Items.Strings = (
'TopLeft'
'CenterLeft'
'BottomLeft'
'TopCenter'
''
'BottomCenter'
'TopRight'
'CenterRight'
'BottomRight'
)
OnClick = rgAlignmentClick
TabOrder = 2
end
object cbUseSidebar: TCheckBox
Left = 256
Left = 8
Height = 17
Top = 33
Top = 60
Width = 56
Caption = 'Sidebar'
Checked = True

View File

@ -92,9 +92,14 @@ begin
with Chart1.Legend do
case rgAlignment.ItemIndex of
0: Alignment := laTopLeft;
1: Alignment := laBottomLeft;
2: Alignment := laTopRight;
3: Alignment := laBottomRight;
1: Alignment := laCenterLeft;
2: Alignment := laBottomLeft;
3: Alignment := laTopCenter;
4: Abort;
5: Alignment := laBottomCenter;
6: Alignment := laTopRight;
7: Alignment := laCenterRight;
8: Alignment := laBottomRight;
end;
end;

View File

@ -104,7 +104,10 @@ type
property Color default clWhite;
end;
TLegendAlignment = (laTopLeft, laBottomLeft, laTopRight, laBottomRight);
TLegendAlignment = (
laTopLeft, laCenterLeft, laBottomLeft,
laTopCenter, laBottomCenter, // laCenterCenter makes no sense.
laTopRight, laCenterRight, laBottomRight);
{ TChartLegend }
@ -381,43 +384,61 @@ end;
function TChartLegend.Prepare(
ADrawer: IChartDrawer; AItems: TObjectList; var AClipRect: TRect): TRect;
var
w, x, y, i, textHeight, legendWidth, legendHeight: Integer;
x, y, i, textHeight: Integer;
sidebar, legendSize: TPoint;
begin
ADrawer.Font := Font;
// Measure the legend.
legendWidth := 0;
legendSize.X := 0;
textHeight := 0;
for i := 0 to AItems.Count - 1 do
with ADrawer.TextExtent((AItems[i] as TLegendItem).FText) do begin
legendWidth := Max(X, legendWidth);
legendSize.X := Max(X, legendSize.X);
textHeight := Max(Y, textHeight);
end;
legendWidth += 2 * Spacing + SYMBOL_TEXT_SPACING + SymbolWidth;
w := 2 * MarginX;
with AClipRect do
legendWidth := EnsureRange(legendWidth, 0, Right - Left - w);
w += legendWidth;
legendHeight := Spacing + AItems.Count * (textHeight + Spacing);
legendSize.X += 2 * Spacing + SYMBOL_TEXT_SPACING + SymbolWidth;
sidebar.X := 2 * MarginX;
with AClipRect do
legendSize.X := EnsureRange(legendSize.X, 0, Right - Left - sidebar.X);
sidebar.X += legendSize.X;
legendSize.Y := Spacing + AItems.Count * (textHeight + Spacing);
sidebar.Y := 2 * MarginX;
with AClipRect do
legendSize.Y := EnsureRange(legendSize.Y, 0, Bottom - Top - sidebar.Y);
sidebar.Y += legendSize.Y;
// Determine position according to the alignment.
if Alignment in [laTopLeft, laBottomLeft] then begin
x := AClipRect.Left + MarginX;
if UseSidebar then
AClipRect.Left += w;
end
else begin
x := AClipRect.Right - legendWidth - MarginX;
if UseSidebar then
AClipRect.Right -= w;
case Alignment of
laTopLeft, laCenterLeft, laBottomLeft:
x := AClipRect.Left + MarginX;
laTopRight, laCenterRight, laBottomRight:
x := AClipRect.Right - legendSize.X - MarginX;
laTopCenter, laBottomCenter:
x := (AClipRect.Right + AClipRect.Left - legendSize.X) div 2;
end;
if Alignment in [laTopLeft, laTopRight] then
y := AClipRect.Top + MarginY
else
y := AClipRect.Bottom - MarginY - legendHeight;
Result := Bounds(x, y, legendWidth, legendHeight);
case Alignment of
laTopLeft, laTopCenter, laTopRight:
y := AClipRect.Top + MarginY;
laBottomLeft, laBottomCenter, laBottomRight:
y := AClipRect.Bottom - MarginY - legendSize.Y;
laCenterLeft, laCenterRight:
y := (AClipRect.Top + AClipRect.Bottom - legendSize.Y) div 2;
end;
if UseSidebar then
case Alignment of
laTopLeft, laCenterLeft, laBottomLeft:
AClipRect.Left += sidebar.X;
laTopRight, laCenterRight, laBottomRight:
AClipRect.Right -= sidebar.X;
laTopCenter:
AClipRect.Top += legendSize.Y + 2 * MarginY;
laBottomCenter:
AClipRect.Bottom -= legendSize.Y + 2 * MarginY;
end;
Result := Bounds(x, y, legendSize.X, legendSize.Y);
end;
procedure TChartLegend.SetAlignment(AValue: TLegendAlignment);