TAChart: Group Legend-related properties into TChartSeriesLegend class. Update demo.

git-svn-id: trunk@27559 -
This commit is contained in:
ask 2010-10-04 09:10:55 +00:00
parent 185f09f6b7
commit 91aa289f6e
6 changed files with 102 additions and 41 deletions

View File

@ -6,6 +6,7 @@ object Form1: TForm1
Caption = 'Form1'
ClientHeight = 361
ClientWidth = 510
OnCreate = FormCreate
Position = poScreenCenter
LCLVersion = '0.9.29'
object Chart1: TChart
@ -59,7 +60,6 @@ object Form1: TForm1
Extent.UseXMin = True
Extent.UseXMax = True
OnCalculate = Chart1FuncSeries1Calculate
OnDrawLegend = Chart1FuncSeries1DrawLegend
Pen.Color = clPurple
Pen.Width = 2
Title = 'Function'

View File

@ -34,6 +34,7 @@ type
procedure cbUseSidebarChange(Sender: TObject);
procedure Chart1FuncSeries1Calculate(const AX: Double; out AY: Double);
procedure Chart1FuncSeries1DrawLegend(ACanvas: TCanvas; const ARect: TRect);
procedure FormCreate(Sender: TObject);
procedure rgAlignmentClick(Sender: TObject);
procedure seMarginXChange(Sender: TObject);
procedure seMarginYChange(Sender: TObject);
@ -75,6 +76,11 @@ begin
Round(Sin(x / w * 2 * Pi) * (ARect.Bottom - ARect.Top) / 2) + y0);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Chart1FuncSeries1.Legend.OnDraw := @Chart1FuncSeries1DrawLegend;
end;
procedure TForm1.rgAlignmentClick(Sender: TObject);
begin
with Chart1.Legend do

View File

@ -36,18 +36,19 @@ type
private
FAxisIndexX: Integer;
FAxisIndexY: Integer;
FOnDrawLegend: TLegendItemDrawEvent;
FLegend: TChartSeriesLegend;
procedure SetAxisIndexX(AValue: Integer);
procedure SetAxisIndexY(AValue: Integer);
procedure SetOnDrawLegend(AValue: TLegendItemDrawEvent);
procedure SetLegend(AValue: TChartSeriesLegend);
procedure SetShowInLegend(AValue: Boolean);
protected
procedure GetGraphBounds(var ABounds: TDoubleRect); override;
procedure GetLegendItems(AItems: TChartLegendItems); virtual; abstract;
procedure GetLegendItemsBasic(AItems: TChartLegendItems); override;
function GetShowInLegend: Boolean; override;
procedure SetActive(AValue: Boolean); override;
procedure SetDepth(AValue: TChartDistance); override;
procedure SetShowInLegend(AValue: Boolean); override;
procedure SetZPosition(AValue: TChartDistance); override;
procedure StyleChanged(Sender: TObject);
procedure UpdateParentChart;
@ -73,6 +74,7 @@ type
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function GetParentComponent: TComponent; override;
function HasParent: Boolean; override;
@ -81,8 +83,10 @@ type
property AxisIndexY: Integer
read FAxisIndexY write SetAxisIndexY default DEF_AXIS_INDEX;
property OnDrawLegend: TLegendItemDrawEvent
read FOnDrawLegend write SetOnDrawLegend;
published
property Legend: TChartSeriesLegend read FLegend write SetLegend;
property ShowInLegend: Boolean
read GetShowInLegend write SetShowInLegend default true; deprecated;
end;
TChartGetMarkEvent = procedure (
@ -93,7 +97,6 @@ type
TChartSeries = class(TCustomChartSeries)
private
FBuiltinSource: TCustomChartSource;
FLegendMultiplicity: TLegendMultiplicity;
FListener: TListener;
FMarks: TChartMarks;
FOnGetMark: TChartGetMarkEvent;
@ -103,7 +106,6 @@ type
function GetSource: TCustomChartSource;
function IsSourceStored: boolean;
procedure SetLegendMultiplicity(AValue: TLegendMultiplicity);
procedure SetMarks(const AValue: TChartMarks);
procedure SetOnGetMark(const AValue: TChartGetMarkEvent);
procedure SetSource(AValue: TCustomChartSource);
@ -158,14 +160,11 @@ type
read GetSource write SetSource stored IsSourceStored;
published
property Active default true;
property LegendMultiplicity: TLegendMultiplicity
read FLegendMultiplicity write SetLegendMultiplicity default lmSingle;
property Marks: TChartMarks read FMarks write SetMarks;
property ShowInLegend;
property Title;
property ZPosition;
published
property OnDrawLegend;
property OnGetMark: TChartGetMarkEvent read FOnGetMark write SetOnGetMark;
end;
@ -203,7 +202,7 @@ type
implementation
uses
Math, Types;
Math, PropEdits, Types;
{ TCustomChartSeries }
@ -229,9 +228,15 @@ constructor TCustomChartSeries.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FActive := true;
FShowInLegend := true;
FAxisIndexX := DEF_AXIS_INDEX;
FAxisIndexY := DEF_AXIS_INDEX;
FLegend := TChartSeriesLegend.Create(FChart);
end;
destructor TCustomChartSeries.Destroy;
begin
FreeAndNil(FLegend);
inherited Destroy;
end;
function TCustomChartSeries.GetAxisX: TChartAxis;
@ -270,8 +275,8 @@ end;
procedure TCustomChartSeries.GetLegendItemsBasic(AItems: TChartLegendItems);
begin
if Assigned(FOnDrawLegend) then
AItems.Add(TLegendItemUserDrawn.Create(OnDrawLegend, Title))
if Assigned(Legend.OnDraw) then
AItems.Add(TLegendItemUserDrawn.Create(Legend.OnDraw, Title))
else
GetLegendItems(AItems);
end;
@ -281,6 +286,11 @@ begin
Result := FChart;
end;
function TCustomChartSeries.GetShowInLegend: Boolean;
begin
Result := Legend.Visible;
end;
function TCustomChartSeries.GraphToAxisX(AX: Double): Double;
begin
Result := TransformByAxis(FChart.AxisList, AxisIndexX).GraphToAxis(AX)
@ -344,10 +354,10 @@ begin
Move(Index, EnsureRange(AValue, 0, Count - 1));
end;
procedure TCustomChartSeries.SetOnDrawLegend(AValue: TLegendItemDrawEvent);
procedure TCustomChartSeries.SetLegend(AValue: TChartSeriesLegend);
begin
if FOnDrawLegend = AValue then exit;
FOnDrawLegend := AValue;
if FLegend = AValue then exit;
FLegend := AValue;
UpdateParentChart;
end;
@ -359,9 +369,7 @@ end;
procedure TCustomChartSeries.SetShowInLegend(AValue: Boolean);
begin
if FShowInLegend = AValue then exit;
FShowInLegend := AValue;
UpdateParentChart;
Legend.Visible := AValue;
end;
procedure TCustomChartSeries.SetZPosition(AValue: TChartDistance);
@ -599,13 +607,6 @@ begin
Source[AIndex]^.Color := AColor;
end;
procedure TChartSeries.SetLegendMultiplicity(AValue: TLegendMultiplicity);
begin
if FLegendMultiplicity = AValue then exit;
FLegendMultiplicity := AValue;
UpdateParentChart;
end;
procedure TChartSeries.SetMarks(const AValue: TChartMarks);
begin
if FMarks = AValue then exit;
@ -818,5 +819,15 @@ begin
end;
end;
procedure SkipObsoleteProperties;
begin
RegisterPropertyEditor(
TypeInfo(Boolean), TCustomChartSeries,
'ShowInLegend', THiddenPropertyEditor);
end;
initialization
SkipObsoleteProperties;
end.

View File

@ -48,7 +48,6 @@ type
FActive: Boolean;
FChart: TChart;
FDepth: TChartDistance;
FShowInLegend: Boolean;
FTitle: String;
FZPosition: TChartDistance;
@ -60,9 +59,9 @@ type
procedure GetBounds(var ABounds: TDoubleRect); virtual; abstract;
procedure GetGraphBounds(var ABounds: TDoubleRect); virtual; abstract;
procedure GetLegendItemsBasic(AItems: TChartLegendItems); virtual; abstract;
function GetShowInLegend: Boolean; virtual; abstract;
procedure SetActive(AValue: Boolean); virtual; abstract;
procedure SetDepth(AValue: TChartDistance); virtual; abstract;
procedure SetShowInLegend(AValue: Boolean); virtual; abstract;
procedure SetZPosition(AValue: TChartDistance); virtual; abstract;
procedure UpdateMargins(ACanvas: TCanvas; var AMargins: TRect); virtual;
procedure VisitSources(
@ -89,8 +88,6 @@ type
property Active: Boolean read FActive write SetActive default true;
property Depth: TChartDistance read FDepth write SetDepth default 0;
property ParentChart: TChart read FChart;
property ShowInLegend: Boolean
read FShowInLegend write SetShowInLegend default true;
property Title: String read FTitle write FTitle;
property ZPosition: TChartDistance read FZPosition write SetZPosition default 0;
end;
@ -509,7 +506,7 @@ begin
try
for i := 0 to SeriesCount - 1 do
with Series[i] do
if Active and ShowInLegend then
if Active and GetShowInLegend then
GetLegendItemsBasic(ALegendItems);
ARect := Legend.Prepare(ACanvas, ALegendItems, FClipRect);
except

View File

@ -28,8 +28,6 @@ const
DEF_LEGEND_SYMBOL_WIDTH = 20;
type
TLegendMultiplicity = (lmSingle, lmPoint);
{ TLegendItem }
TLegendItem = class
@ -160,6 +158,27 @@ type
property Visible default false;
end;
TLegendMultiplicity = (lmSingle, lmPoint);
{ TChartSeriesLegend }
TChartSeriesLegend = class(TChartElement)
private
FMultiplicity: TLegendMultiplicity;
FOnDraw: TLegendItemDrawEvent;
procedure SetMultiplicity(AValue: TLegendMultiplicity);
procedure SetOnDraw(AValue: TLegendItemDrawEvent);
public
constructor Create(AOwner: TCustomChart);
public
procedure Assign(Source: TPersistent); override;
published
property Multiplicity: TLegendMultiplicity
read FMultiplicity write SetMultiplicity default lmSingle;
property OnDraw: TLegendItemDrawEvent read FOnDraw write SetOnDraw;
property Visible default true;
end;
implementation
uses
@ -450,8 +469,39 @@ begin
TypeInfo(TChartDistance), TChartLegend, 'Margin', THiddenPropertyEditor);
end;
initialization
SkipObsoleteProperties;
{ TChartSeriesLegend }
procedure TChartSeriesLegend.Assign(Source: TPersistent);
begin
if Source is TChartSeriesLegend then
with TChartSeriesLegend(Source) do begin
Self.FMultiplicity := FMultiplicity;
Self.FOnDraw := FOnDraw;
Self.FVisible := FVisible;
end;
inherited Assign(Source);
end;
constructor TChartSeriesLegend.Create(AOwner: TCustomChart);
begin
inherited Create(AOwner);
FVisible := true;
end;
procedure TChartSeriesLegend.SetMultiplicity(AValue: TLegendMultiplicity);
begin
if FMultiplicity = AValue then exit;
FMultiplicity := AValue;
StyleChanged(Self);
end;
procedure TChartSeriesLegend.SetOnDraw(AValue: TLegendItemDrawEvent);
begin
if FOnDraw = AValue then exit;
FOnDraw := AValue;
StyleChanged(Self);
end;
end.

View File

@ -248,7 +248,6 @@ type
property Active default true;
property LineStyle: TLineStyle
read FLineStyle write SetLineStyle default lsHorizontal;
property OnDrawLegend;
property Pen: TPen read FPen write SetPen;
property Position: Double read FPosGraph write SetPos;
property SeriesColor: TColor
@ -298,7 +297,6 @@ type
property AxisIndexY;
property Extent: TChartExtent read FExtent write SetExtent;
property OnCalculate: TFuncCalculateEvent read FOnCalculate write SetOnCalculate;
property OnDrawLegend;
property Pen: TChartPen read FPen write SetPen;
property ShowInLegend;
property Step: TFuncSeriesStep read FStep write SetStep default 2;
@ -328,7 +326,6 @@ type
property ZPosition;
published
property OnDraw: TSeriesDrawEvent read FOnDraw write SetOnDraw;
property OnDrawLegend;
property OnGetBounds: TSeriesGetBoundsEvent
read FOnGetBounds write SetOnGetBounds;
end;
@ -952,7 +949,7 @@ procedure TPieSeries.GetLegendItems(AItems: TChartLegendItems);
var
i: Integer;
begin
case LegendMultiplicity of
case Legend.Multiplicity of
lmSingle:
AItems.Add(TLegendItemColorRect.Create(SliceColor(0), Title));
lmPoint: