[Lazarus] TAChart with transparent background

Werner Pamler werner.pamler at freenet.de
Tue Sep 2 18:18:17 CEST 2014


It is not fully clear to me what you mean by "a chart with transparent 
background". Do you want to see the form underneath the chart? I fear 
this is not easily possible. Or do you want to have a background image 
in the chart? For the latter case this code is working fine with me:

type
   TForm1 = class(TForm)
     Chart1: TChart;
     procedure Chart1BeforeDrawBackground(ASender: TChart; ACanvas: TCanvas;
       const ARect: TRect; var ADoDefaultDrawing: Boolean);
     procedure Chart1BeforeDrawBackWall(ASender: TChart; ACanvas: TCanvas;
       const ARect: TRect; var ADoDefaultDrawing: Boolean);
     procedure FormCreate(Sender: TObject);
     procedure FormDestroy(Sender: TObject);
   private
     { private declarations }
     FBackImage: TPicture;
   public
     { public declarations }
   end;
....
procedure TForm1.Chart1BeforeDrawBackWall(ASender: TChart; ACanvas: TCanvas;
   const ARect: TRect; var ADoDefaultDrawing: Boolean);
begin
   ADoDefaultDrawing := false;
   ACanvas.StretchDraw(ARect, FBackImage.Graphic);
end;

procedure TForm1.Chart1BeforeDrawBackground(ASender: TChart; ACanvas: 
TCanvas;
   const ARect: TRect; var ADoDefaultDrawing: Boolean);
begin
   ACanvas.GradientFill(ARect, clSkyBlue, clWhite, gdVertical);
   ADoDefaultDrawing := false;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
   FBackImage := TPicture.Create;
   FBackImage.LoadFromFile('C:\lazarus-svn\images\splash_logo.png');
   // please adapt this path to your lazarus installation
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
   FBackImage.Free;
end;

The basic idea is to assign a handler to the chart events 
OnBeforeDrawBackWall or OnBeforeDrawBackground and disable the default 
background painting by setting ADoDefaultDrawing to false. The first 
event replaces painting of the area enclosed by the chart's frame ("Back 
wall"), the other one replaces painting of the entire chart background. 
Above example paints the Lazarus splash-logo as BackWall and a gradient 
as background.

Maybe I'll once write a tutorial on this topic...





More information about the Lazarus mailing list