TAChart: Add TPieSeries.FixedRadius property

git-svn-id: trunk@28638 -
This commit is contained in:
ask 2010-12-06 14:02:58 +00:00
parent b1d4dc1fb8
commit 09382cb3c0
2 changed files with 46 additions and 22 deletions

View File

@ -23,7 +23,7 @@ interface
uses uses
Classes, Graphics, SysUtils, Types, Classes, Graphics, SysUtils, Types,
TACustomSeries, TALegend; TACustomSeries, TALegend, TAChartUtils;
type type
TLabelParams = record TLabelParams = record
@ -41,12 +41,14 @@ type
TCustomPieSeries = class(TChartSeries) TCustomPieSeries = class(TChartSeries)
private private
FCenter: TPoint; FCenter: TPoint;
FExploded: Boolean;
FFixedRadius: TChartDistance;
FLabels: array of TLabelParams; FLabels: array of TLabelParams;
FRadius: Integer; FRadius: Integer;
FSlices: array of TPieSlice; FSlices: array of TPieSlice;
FExploded: Boolean;
procedure Measure(ACanvas: TCanvas); procedure Measure(ACanvas: TCanvas);
procedure SetExploded(const AValue: Boolean); procedure SetExploded(AValue: Boolean);
procedure SetFixedRadius(AValue: TChartDistance);
function SliceColor(AIndex: Integer): TColor; function SliceColor(AIndex: Integer): TColor;
protected protected
procedure AfterAdd; override; procedure AfterAdd; override;
@ -58,6 +60,8 @@ type
// Offset slices away from center based on X value. // Offset slices away from center based on X value.
property Exploded: Boolean read FExploded write SetExploded default false; property Exploded: Boolean read FExploded write SetExploded default false;
property FixedRadius: TChartDistance
read FFixedRadius write SetFixedRadius default 0;
property Source; property Source;
end; end;
@ -65,11 +69,12 @@ implementation
uses uses
Math, GraphMath, Math, GraphMath,
TAChartUtils, TADrawUtils, TASources; TADrawUtils, TASources;
{ TCustomPieSeries } { TCustomPieSeries }
function TCustomPieSeries.AddPie(Value: Double; Text: String; Color: TColor): Longint; function TCustomPieSeries.AddPie(
Value: Double; Text: String; Color: TColor): Longint;
begin begin
Result := AddXY(GetXMaxVal + 1, Value, Text, Color); Result := AddXY(GetXMaxVal + 1, Value, Text, Color);
end; end;
@ -192,26 +197,37 @@ var
di: PChartDataItem; di: PChartDataItem;
prevAngle: Double = 0; prevAngle: Double = 0;
begin begin
FCenter := CenterPoint(ParentChart.ClipRect);
if Marks.IsMarkLabelsVisible then begin
SetLength(FLabels, Count); SetLength(FLabels, Count);
SetLength(FSlices, Count);
for i := 0 to Count - 1 do for i := 0 to Count - 1 do
with FLabels[i] do begin with FLabels[i] do begin
FText := FormattedMark(i); FText := FormattedMark(i);
if FText = '' then
FSize := Size(0, 0)
else
FSize := Marks.MeasureLabel(ACanvas, FText); FSize := Marks.MeasureLabel(ACanvas, FText);
m.X := Max(m.X, FSize.cx); m.X := Max(m.X, FSize.cx);
m.Y := Max(m.Y, FSize.cy); m.Y := Max(m.Y, FSize.cy);
end; end;
end;
FCenter := CenterPoint(ParentChart.ClipRect);
// Reserve space for labels. // Reserve space for labels.
m := ParentChart.ClipRect.BottomRight - FCenter - m; m := ParentChart.ClipRect.BottomRight - FCenter - m;
if FixedRadius = 0 then begin
FRadius := Min(m.X, m.Y); FRadius := Min(m.X, m.Y);
if Marks.IsMarkLabelsVisible then if Marks.IsMarkLabelsVisible then
FRadius -= Marks.Distance; FRadius -= Marks.Distance;
FRadius := Max(FRadius - MARGIN, 0); FRadius := Max(FRadius - MARGIN, 0);
if Exploded then if Exploded then
FRadius := Trunc(FRadius / (Max(Source.Extent.b.X, 0) + 1)); FRadius := Trunc(FRadius / (Max(Source.Extent.b.X, 0) + 1));
end
else
FRadius := FixedRadius;
SetLength(FSlices, Count);
for i := 0 to Count - 1 do begin for i := 0 to Count - 1 do begin
di := Source[i]; di := Source[i];
with FSlices[i] do begin with FSlices[i] do begin
@ -227,13 +243,20 @@ begin
end; end;
end; end;
procedure TCustomPieSeries.SetExploded(const AValue: Boolean); procedure TCustomPieSeries.SetExploded(AValue: Boolean);
begin begin
if FExploded = AValue then exit; if FExploded = AValue then exit;
FExploded := AValue; FExploded := AValue;
UpdateParentChart; UpdateParentChart;
end; end;
procedure TCustomPieSeries.SetFixedRadius(AValue: TChartDistance);
begin
if FFixedRadius = AValue then exit;
FFixedRadius := AValue;
UpdateParentChart;
end;
function TCustomPieSeries.SliceColor(AIndex: Integer): TColor; function TCustomPieSeries.SliceColor(AIndex: Integer): TColor;
begin begin
Result := Result :=

View File

@ -91,6 +91,7 @@ type
TPieSeries = class(TCustomPieSeries) TPieSeries = class(TCustomPieSeries)
published published
property Exploded; property Exploded;
property FixedRadius;
property Source; property Source;
end; end;