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

View File

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

View File

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

View File

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

View File

@ -28,8 +28,6 @@ const
DEF_LEGEND_SYMBOL_WIDTH = 20; DEF_LEGEND_SYMBOL_WIDTH = 20;
type type
TLegendMultiplicity = (lmSingle, lmPoint);
{ TLegendItem } { TLegendItem }
TLegendItem = class TLegendItem = class
@ -160,6 +158,27 @@ type
property Visible default false; property Visible default false;
end; 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 implementation
uses uses
@ -450,8 +469,39 @@ begin
TypeInfo(TChartDistance), TChartLegend, 'Margin', THiddenPropertyEditor); TypeInfo(TChartDistance), TChartLegend, 'Margin', THiddenPropertyEditor);
end; end;
initialization { TChartSeriesLegend }
SkipObsoleteProperties;
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. end.

View File

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