TAChart: Draw per-point legend items with the color from the chart source

git-svn-id: trunk@27567 -
This commit is contained in:
ask 2010-10-04 10:32:34 +00:00
parent 3d801aa92c
commit c95aa6b2f3
4 changed files with 38 additions and 41 deletions

View File

@ -201,7 +201,7 @@ object Form1: TForm1
object ListChartSource1: TListChartSource object ListChartSource1: TListChartSource
DataPoints.Strings = ( DataPoints.Strings = (
'0|0|?|' '0|0|?|'
'1|2|?|' '1|2|$00FF00|'
'2|5|?|' '2|5|?|'
'3|1|?|' '3|1|?|'
'4|6|?|' '4|6|?|'

View File

@ -709,14 +709,17 @@ procedure TBasicPointSeries.GetLegendItemsRect(
AItems: TChartLegendItems; ABrush: TBrush); AItems: TChartLegendItems; ABrush: TBrush);
var var
i: Integer; i: Integer;
li: TLegendItemBrushRect;
begin begin
case Legend.Multiplicity of case Legend.Multiplicity of
lmSingle: lmSingle:
AItems.Add(TLegendItemBrushRect.Create(ABrush, Title)); AItems.Add(TLegendItemBrushRect.Create(ABrush, Title));
lmPoint: begin lmPoint:
for i := 0 to Count - 1 do for i := 0 to Count - 1 do begin
AItems.Add(TLegendItemColorRect.Create(GetColor(i), FormattedMark(i))); li := TLegendItemBrushRect.Create(ABrush, FormattedMark(i));
end; li.Color := GetColor(i);
AItems.Add(li);
end;
end; end;
end; end;

View File

@ -33,9 +33,12 @@ type
TLegendItem = class TLegendItem = class
private private
FText: String; FText: String;
FColor: TColor;
public public
constructor Create(const AText: String); constructor Create(const AText: String; AColor: TColor = clTAColor);
procedure Draw(ACanvas: TCanvas; const ARect: TRect); virtual; procedure Draw(ACanvas: TCanvas; const ARect: TRect); virtual;
public
property Color: TColor read FColor write FColor;
end; end;
TLegendItemDrawEvent = TLegendItemDrawEvent =
@ -84,16 +87,6 @@ type
procedure Draw(ACanvas: TCanvas; const ARect: TRect); override; procedure Draw(ACanvas: TCanvas; const ARect: TRect); override;
end; end;
{ TLegendItemColorRect }
TLegendItemColorRect = class(TLegendItem)
private
FColor: TColor;
public
constructor Create(AColor: TColor; const AText: String);
procedure Draw(ACanvas: TCanvas; const ARect: TRect); override;
end;
TChartLegendItems = TObjectList; TChartLegendItems = TObjectList;
TChartLegendBrush = class(TBrush) TChartLegendBrush = class(TBrush)
@ -189,8 +182,9 @@ const
{ TLegendItem } { TLegendItem }
constructor TLegendItem.Create(const AText: String); constructor TLegendItem.Create(const AText: String; AColor: TColor);
begin begin
FColor := AColor;
FText := AText; FText := AText;
end; end;
@ -253,7 +247,7 @@ begin
// Max width slightly narrower then ARect to leave place for the line. // Max width slightly narrower then ARect to leave place for the line.
sz.X := Min(FPointer.HorizSize, (ARect.Right - ARect.Left) div 3); sz.X := Min(FPointer.HorizSize, (ARect.Right - ARect.Left) div 3);
sz.Y := Min(FPointer.VertSize, (ARect.Bottom - ARect.Top) div 2); sz.Y := Min(FPointer.VertSize, (ARect.Bottom - ARect.Top) div 2);
FPointer.DrawSize(ACanvas, c, sz, clTAColor); FPointer.DrawSize(ACanvas, c, sz, Color);
end; end;
{ TLegendItemBrushRect } { TLegendItemBrushRect }
@ -267,23 +261,12 @@ end;
procedure TLegendItemBrushRect.Draw(ACanvas: TCanvas; const ARect: TRect); procedure TLegendItemBrushRect.Draw(ACanvas: TCanvas; const ARect: TRect);
begin begin
inherited Draw(ACanvas, ARect); inherited Draw(ACanvas, ARect);
ACanvas.Brush.Assign(FBrush); if FBrush = nil then
ACanvas.Rectangle(ARect); ACanvas.Brush.Style := bsSolid
end; else
ACanvas.Brush.Assign(FBrush);
{ TLegendItemColorRect } if Color <> clTAColor then
ACanvas.Brush.Color := Color;
constructor TLegendItemColorRect.Create(AColor: TColor; const AText: String);
begin
inherited Create(AText);
FColor := AColor;
end;
procedure TLegendItemColorRect.Draw(ACanvas: TCanvas; const ARect: TRect);
begin
inherited Draw(ACanvas, ARect);
ACanvas.Brush.Color := FColor;
ACanvas.Brush.Style := bsSolid;
ACanvas.Rectangle(ARect); ACanvas.Rectangle(ARect);
end; end;

View File

@ -502,6 +502,7 @@ var
lp: TPen; lp: TPen;
p: TSeriesPointer; p: TSeriesPointer;
i: Integer; i: Integer;
li: TLegendItemLinePointer;
begin begin
if LineType = ltNone then if LineType = ltNone then
lp := nil lp := nil
@ -515,8 +516,11 @@ begin
lmSingle: lmSingle:
AItems.Add(TLegendItemLinePointer.Create(lp, p, Title)); AItems.Add(TLegendItemLinePointer.Create(lp, p, Title));
lmPoint: begin lmPoint: begin
for i := 0 to Count - 1 do for i := 0 to Count - 1 do begin
AItems.Add(TLegendItemLinePointer.Create(lp, p, FormattedMark(i))); li := TLegendItemLinePointer.Create(lp, p, FormattedMark(i));
li.Color := GetColor(i);
AItems.Add(li);
end;
end; end;
end; end;
end; end;
@ -957,13 +961,20 @@ end;
procedure TPieSeries.GetLegendItems(AItems: TChartLegendItems); procedure TPieSeries.GetLegendItems(AItems: TChartLegendItems);
var var
i: Integer; i: Integer;
li: TLegendItemBrushRect;
begin begin
case Legend.Multiplicity of case Legend.Multiplicity of
lmSingle: lmSingle: begin
AItems.Add(TLegendItemColorRect.Create(SliceColor(0), Title)); li := TLegendItemBrushRect.Create(nil, Title);
li.Color := SliceColor(0);
AItems.Add(li);
end;
lmPoint: lmPoint:
for i := 0 to Count - 1 do for i := 0 to Count - 1 do begin
AItems.Add(TLegendItemColorRect.Create(SliceColor(i), FormattedMark(i))); li := TLegendItemBrushRect.Create(nil, FormattedMark(i));
li.Color := SliceColor(i);
AItems.Add(li);
end;
end; end;
end; end;