Sign in Join
Wiki copytemporary

BGRABitmap tutorial TAChart: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11You can make beautiful charts using [[BGRABitmap]]. Add TAChartBGRA package with the project inspector.
22
33Maybe the package will not appear in the list or will not be installed. In this case, open it manually with the menu Package, Open package file (.lpk). It must be in *component\tachart*. Then click on the button to install the package.
44
55## Add a TAChart
66
77[[TAChart]] in fact has the classname TChart. To add a chart, browse the components, choose Chart tab and click on TChart icon. Click and draw a rectangle on the window to drop it.
88
99In order to supply data, click on TRandomChartSource component icon and click once in the window to drop it. In the Object Inspector, define PointsNumber to 10, XMax to 10 and YMax to 10.
1010
1111Now let's create a series that uses this source. To do that, click on your chart and in the Object Inspector, go to Series property. Click on the three dots, and add a *Bar series*. In its properties, go to Source property and choose RandomChartSource1. The chart should be filled with red bars.
1212
13-<figure>
14-<img src="tachartrandombars.png" title="tachartrandombars.png" />
15-<figcaption>tachartrandombars.png</figcaption>
16-</figure>
13+![](/wiki-images/3/3e/Tachartrandombars.png)
1714
1815As we are going to do some 3D, we need some depth. So set the Depth property of the series to 10.
1916
20-<figure>
21-<img src="tachartrandombars3D.png" title="tachartrandombars3D.png" />
22-<figcaption>tachartrandombars3D.png</figcaption>
23-</figure>
17+![](/wiki-images/6/69/Tachartrandombars3D.png)
2418
2519Finally, click on the chart and set the Align property to alClient in order to fill the window.
2620
2721## Decorate in normal rendering mode
2822
2923Go to the `Series` property of your chart. Choose the bar series, and in the Object Inspector, go to the events and define a handler for the event `OnCustomDrawBar`. Add the following code:
3024
3125``` pascal
3226uses
3327 TADrawUtils, TADrawerCanvas, TABGRAUtils;
3428
3529procedure TForm1.Chart1BarSeries1CustomDrawBar(ASeries: TBarSeries;
3630 ADrawer: IChartDrawer; const ARect: TRect; APointIndex, AStackIndex: Integer);
3731var
3832 ic: IChartTCanvasDrawer;
3933begin
4034 if not Supports(ADrawer, IChartTCanvasDrawer, ic) then
4135 raise Exception.Create('This program requires a canvas drawer.');
4236
4337 DrawPhong3DBar(ASeries, ic.Canvas, ARect, APointIndex);
4438end;
4539```
4640
4741> **Note:** This code is valid for Lazarus v2.2+. In older versions, the event `TBarSeries.OnBeforeDrawBar` had to be used instead:
4842>
4943> ``` pascal
5044> uses
5145> TABGRAUtils;
5246>
5347> procedure TForm1.BeforeDrawBarHandler(ASender: TBarSeries; ACanvas: TCanvas;
5448> const ARect: TRect; APointIndex, AStackIndex: Integer; var ADoDefaultDrawing: Boolean);
5549> begin
5650> ADoDefaultDrawing:= false;
5751> DrawPhong3DBar(ASender, ACanvas, ARect, APointIndex);
5852> end;
5953> ```
6054
6155Then run the program. Your chart should look like this:
6256
63-<figure>
64-<img src="chartwithbgra.png" title="chartwithbgra.png" />
65-<figcaption>chartwithbgra.png</figcaption>
66-</figure>
57+![](/wiki-images/e/e5/Chartwithbgra.png)
6758
6859You can also use the `DrawChocolateBar` function. Set `PointsNumber` property of the `RandomChartSource1` to 5. Change the code of the `OnCustomDrawBar` (for Laz 2.2+, or `OnBeforeDrawBar` for older versions) to:
6960
7061``` pascal
7162uses
7263 TADrawUtils, TADrawerCanvas, TABGRAUtils;
7364
7465procedure TForm1.Chart1BarSeries1CustomDrawBar(ASeries: TBarSeries;
7566 ADrawer: IChartDrawer; const ARect: TRect; APointIndex, AStackIndex: Integer);
7667var
7768 ic: IChartTCanvasDrawer;
7869begin
7970 if not Supports(ADrawer, IChartTCanvasDrawer, ic) then
8071 raise Exception.Create('This program requires a canvas drawer.');
8172
8273 DrawChocolateBar(ASeries, icCanvas, ARect, APointIndex, True);
8374end;
8475
8576{ or for Lazarus before v2.2:
8677
8778procedure TForm1.BeforeDrawBarHandler(ASender: TBarSeries; ACanvas: TCanvas;
8879 const ARect: TRect; APointIndex, AStackIndex: Integer;
8980 var ADoDefaultDrawing: Boolean);
9081begin
9182 ADoDefaultDrawing:= false;
9283 DrawChocolateBar(ASender, ACanvas, ARect, APointIndex, True);
9384end;}
9485```
9586
96-<figure>
97-<img src="chartwithbgrachocolate.png" title="chartwithbgrachocolate.png" />
98-<figcaption>chartwithbgrachocolate.png</figcaption>
99-</figure>
87+![](/wiki-images/4/46/Chartwithbgrachocolate.png)
10088
10189## Using BGRABitmap to render the whole chart
10290
10391Bars are not very different with or without antialiasing. Go to the Series property and remove the bars, add a *Line series*, and set its Source to RandomChartSource1. Then, set its property Depth to 10 and its SeriesColor property to clRed.
10492
105-<figure>
106-<img src="tachartlines3D.png" title="tachartlines3D.png" />
107-<figcaption>tachartlines3D.png</figcaption>
108-</figure>
93+![](/wiki-images/f/f9/Tachartlines3D.png)
10994
11095In order to change the rendering engine, look for the TChartGUIConnectorBGRA component in the Chart tab. Click to drop it on the chart. Then, click on the chart background and with the object inspector, set the GUIConnector property by choosing ChartGUIConnectorBGRA1. The rendering is now done with antialiasing, no need to start the program to see:
11196
112-<figure>
113-<img src="tachartlines3Dbgra.png" title="tachartlines3Dbgra.png" />
114-<figcaption>tachartlines3Dbgra.png</figcaption>
115-</figure>
97+![](/wiki-images/5/50/Tachartlines3Dbgra.png)
11698
11799## Drawing a chart on any Canvas
118100
119101### Simple drawing
120102
121103First add the TAChartBGRA package. To render your chart, you can for example use a PaintBox. In the OnPaint event, write:
122104
123105``` pascal
124106uses BGRABitmap, TADrawerBGRA;
125107
126108procedure TForm1.PaintBox1Paint(Sender: TObject);
127109var
128110 bmp: TBGRABitmap;
129111 id: IChartDrawer;
130112 rp: TChartRenderingParams;
131113begin
132114 bmp := TBGRABitmap.Create(PaintBox1.Width, PaintBox1.Height);
133115 Chart1.DisableRedrawing;
134116 try
135117 id := TBGRABitmapDrawer.Create(bmp);
136118 id.DoGetFontOrientation := @CanvasGetFontOrientationFunc;
137119 rp := Chart1.RenderingParams;
138120 Chart1.Draw(id, Rect(0, 0, PaintBox1.Width, PaintBox1.Height));
139121 Chart1.RenderingParams := rp;
140122 bmp.Draw(PaintBox1.Canvas, 0, 0);
141123 finally
142124 Chart1.EnableRedrawing;
143125 bmp.Free;
144126 end;
145127end;
146128```
147129
148-<figure>
149-<img src="chartbgra.png" title="chartbgra.png" />
150-<figcaption>chartbgra.png</figcaption>
151-</figure>
130+![](/wiki-images/3/3a/Chartbgra.png)
152131
153132To avoid flickering, consider using TBGRAVirtualScreen from [[BGRAControls]].
154133
155134### Text effects
156135
157136To add text effects, you need the latest version of BGRABitmap, and the latest SVN of Lazarus. There is a FontRenderer property in each TBGRABitmap image, that you can define. You must not free the renderer, as it is automatically freed by the image.
158137
159138The text effects are provided by the BGRATextFX unit that contains the TBGRATextEffectFontRenderer class. To add a golden contour to the letters and a shadow, add the following lines:
160139
161140``` pascal
162141uses BGRATextFX, BGRAGradientScanner;
163142...
164143var
165144 ...
166145 fontRenderer: TBGRATextEffectFontRenderer;
167146 gold: TBGRAGradientScanner;
168147begin
169148 ...
170149 fontRenderer:= TBGRATextEffectFontRenderer.Create;
171150 fontRenderer.ShadowVisible := true; //adds a shadow
172151 fontRenderer.OutlineVisible := true; //show the outline
173152 gold := TBGRAGradientScanner.Create(CSSGold,CSSGoldenrod,gtLinear,PointF(0,0),PointF(20,20),true,true);
174153 fontRenderer.OutlineTexture := gold; //define the texture used for the outline
175154 fontRenderer.OuterOutlineOnly := true; //just the outer pixels of the text
176155 bmp.FontRenderer := fontRenderer; //gives the FontRenderer to the image (which becomes the owner of the renderer)
177156 ...
178157 gold.Free;
179158end;
180159```
181160
182-<figure>
183-<img src="chartbgrafonteffect.png" title="chartbgrafonteffect.png" />
184-<figcaption>chartbgrafonteffect.png</figcaption>
185-</figure>
161+![](/wiki-images/7/7e/Chartbgrafonteffect.png)
186162
187163---
188164*From the Free Pascal wiki, page "BGRABitmap tutorial TAChart", as it stood on 2022-01-09 after a change by Wp, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
189165Category: BGRABitmap
190166Category: Lazarus
191167Category: TAChart
192168Category: Tutorials

Back to history