Sign in Join
Wiki copytemporary

TAChart Tutorial: Getting started: what changed

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

OldNewLine
11## Introduction
22
33![](/wiki-images/2/2f/Tachart_getting_started_step6.png)
44
55If you want to add a plot or a chart to your Lazarus form you should have a look at [[TAChart]]. This is the standard Lazarus package for drawing plots and charts.
66
77In this tutorial we will draw some elemental mathematical functions by TAChart. We will create a chart with three line series for the functions
88
99- y=cos(x),
1010- y=sin(x), and
1111- y=cos(x)*sin(x).
1212
1313Maybe it is necessary to expain the words "series" and "chart" here: the entire diagram is the "chart", and each of the functions will be displayed as a curve called "series". We will connect the data points by line sections, therefore, we will use the "line series". TAChart, of course, allows to use many other types of series, for example bar series, area series, pie series, or even advanced types like spline or fit series.
1414
1515## What is needed?
1616
1717This guide will be very elemental. Of course, you'll need basic knowledge of the [[Object Pascal]] language and the Lazarus [[IDE]].
1818
1919TAChart is included in Lazarus, so there is no need for seeking this package somewhere in the internet. However, don't use a too-old version of Lazarus since TAChart is being actively developed and some features may change from version to version.
2020
2121It is best if you follow this tutorial step by step. But if you want you can use the [[Source code|source code]] of the finished project at the end of this article.
2222
2323## A new project
2424
2525![](/wiki-images/9/95/TAChart_component_palette.png)
2626
2727To begin with, we create a new project in Lazarus and, in the [[Component Palette|component palette]] we select the page [[Chart tab|"Chart"]]. Click on the left-most icon, the [[TChart]] component, and add it to the form. You will see an empty diagram with standardized x and y axes.
2828
2929![](/wiki-images/6/63/TAChart_GettingStarted_Step1.png)
3030
3131## Adding series
3232
3333Now let's add series. For this purpose, double-click on the chart (or right-click, and select "Edit series" from the context menu). The TAChart series editor appears. It is still empty, but when you select "Add" you will see a menu with all the series types available within the TAChart package. Select the "line series". Repeat two times to create, in total, three line series that are assigned to the chart. They are not yet displayed in the chart, because they do not yet have any data. No data - no display. We will fix that in a minute.
3434
3535![](/wiki-images/6/6f/TAChart_GettingStarted_Step1a.png)
3636
3737Before we do that let us have a look at the object inspector tree. TAChart uses a quite complex architecture of classes and containers, so it is important to understand the object tree. In the current state of our project we see the TChart component and its children, the three line series and the AxisList with Left and Bottom axes as children again. The series names are constructed according to `Chart<number><SeriesType><Number>`.
3838
3939![](/wiki-images/c/ce/Tachart_getting_started_object_tree.png)
4040
4141Why don't we give them more descriptive names, like `SinSeries`, `CosSeries`, and `SinCosSeries`? For this purpose, click on each series, and change the series `Name` in the corresponding field of the object inspector below.
4242
4343## Adding data
4444
4545Now it's time to add data. The easiest option is to create the data along with the form, i.e. we write a handler for the forms's `OnCreate` event. Of course, in a "real" project, you will add data at other occasions, for example after a button click which might initiate a calculation or read data from a file.
4646
4747``` pascal
4848procedure TForm1.FormCreate(Sender: TObject);
4949const
5050 N = 100;
5151 MIN = -10;
5252 MAX = 10;
5353var
5454 i: Integer;
5555 x: Double;
5656begin
5757 for i:=0 to N-1 do begin
5858 x := MIN + (MAX - MIN) * i /(N - 1);
5959 SinSeries.AddXY(x, sin(x));
6060 CosSeries.AddXY(x, cos(x));
6161 SinCosSeries.AddXY(x, sin(x)*cos(x));
6262 end;
6363end;
6464```
6565
6666This [[Procedure|procedure]] creates 100 data points for each series. The x values are calculated to be equidistant between `MIN` and `MAX`, i.e. -10 and 10. The important lines are the calls to the `AddXY` [[Method|method]] of each series. This method takes the x and y coordinates of each data point and adds them to an internal list. There are also overloaded versions of this method where you can also pass a text label for each data point, as well as an individual color, but we don't need this feature here.
6767
6868The mentioned list is a so-called chart source - this is a [[Class|class]] which provides the data for plotting. You can use the built-in chart source, or you can link the series to a separate one. There is a variety of chart sources in the component palette, like
6969
7070- `TListChartSource` which stores the data in a list (this type is used by our series internally)
7171- `TDBChartSource` which interfaces to the records and fields in a database
7272- `TUserDefinedChartSource` which gives a very general access to data, for example, stored in an array of user-defined records
7373- `TCalculatedChartSource` which allows to analyze one series and display the results in another one.
7474
7575But enough on chart sources - the built-in source is sufficient for this introductory project.
7676
7777![](/wiki-images/b/b8/Tachart_getting_started_step2.png)
7878
7979Ok, let's compile the program. We see the three curves, and the x axis automatically covers our data range between -10 and 10. Well - not too bad, but far from being perfect: The diagram is very confusing, we cannot distinguish between the individual lines, and we cannot tell which series belongs to which function.
8080
8181## Formatting the series
8282
8383Why don't we change the color of each series? To do this select each series in the object tree and, in the object inspector below, change the [[Property|property]] `SeriesColor` as you want it to be - `SeriesColor` is the color of the lines connecting the data points.
8484
8585When you recompile you will see the series colored. But we still cannot distinguish between the different functions. What we need is a legend.
8686
8787## Adding a legend
8888
8989Highlight the chart and, in the object inspector, scroll down to `Legend` and open the sub-properties. At the end, there is `Visible` which is `false` by default - so, set it to `true`, and recompile.
9090
9191![](/wiki-images/2/2e/Tachart_getting_started_step3.png)
9292
9393Well, it's getting better. What is missing, is an explanatory text for each series, like `y = sin(x)`. For this purpose each series has a property `Title` - this is the text which will appear in the legend for each entry along with the symbol for the corresponding series. Therefore you have to go through all series again and use the object inspector to set `Title` to `y=sin(x)`, `y=cos(x)`, and `y=sin(x)*cos(x)`, respectively.
9494
9595![](/wiki-images/5/5f/Tachart_getting_started_step4.png) ![](/wiki-images/2/20/Tachart_getting_started_step4a.png)
9696
9797Now when you recompile, the chart is almost perfect. Almost - because the legend is quite large, and the diagram is squeezed into the remaining space. Of course, we could increase the size of the form. But can we also put the legend underneath the diagram? No problem: go back to the chart's property `Legend` and set `Alignment` to `laBottomCenter`. You should also set `ColumnCount` to `3`, otherwise the legend items will be in a column instead of in a row.
9898
9999![](/wiki-images/3/37/Tachart_getting_started_step5.png)
100100
101101Ah! -- the third legend entry is truncated because the window is not wide enough. Align the chart to `alClient` and increase the width of the window a bit.
102102
103103## Fine-tuning
104104
105105A few more things can still be improved: The axes should have a title, like "x axis" and "y axis", respectively, and there should also be a title above the chart.
106106
107107To set the title for the x axis select in the object tree the item `Bottom` of the AxisList child component of TChart, or go to `Bottom axis` in the object inspector. Open the sub-properties where you'll find `Title` which has sub-properties again. The text in the field `Caption` will be displayed as a title above the chart. Don't forget to set `Visible` to `true`. You may also want to set the font style of the title to bold - you find that property under `LabelFont`. Repeat the same with `Left axis`.
108108
109109Essentially the same procedure can be used to set the chart title: Scroll to the chart's property `Title`, enter the title in the field `Text` (you may even use a multi-line text here), and set `Visible` to `true` again.
110110
111111And finally, you might prefer to use a white background of the chart area, this is changed by the property `BackColor` of TChart. And maybe the grid should be less emphasized, i.e. it might look better with color `clSilver`. Use the left and bottom axes property `Grid.Color` to change that.
112112
113113## Finished!
114114
115115In the end, our chart looks like this. Neat, isn't it? And it was not difficult at all...
116116
117117If you want to learn more about TAChart have a look at the official [[TAChart documentation]], and study some of the many demos that come with TAChart. They can be found in the folder `components\tachart\demo` of your Lazarus installation.
118118
119119![](/wiki-images/2/2f/Tachart_getting_started_step6.png)
120120
121121## Source code
122122
123123The source code of this tutorial project can be found in the folder *tutorials/getting_started* of trunk TAChart installations.
124124
125125### Project file
126126
127127``` pascal
128128program project1;
129129
130130{$mode objfpc}{$H+}
131131
132132uses
133133 {$IFDEF UNIX}{$IFDEF UseCThreads}
134134 cthreads,
135135 {$ENDIF}{$ENDIF}
136136 Interfaces, // this includes the LCL widgetset
137137 Forms, Unit1, tachartlazaruspkg
138138 { you can add units after this };
139139
140140{$R *.res}
141141
142142begin
143143 RequireDerivedFormResource := True;
144144 Application.Initialize;
145145 Application.CreateForm(TForm1, Form1);
146146 Application.Run;
147147end.
148148```
149149
150150### Unit1.pas
151151
152152``` pascal
153153unit Unit1;
154154
155155{$mode objfpc}{$H+}
156156
157157interface
158158
159159uses
160160 Classes, SysUtils, FileUtil, TAGraph, TASeries, Forms, Controls, Graphics,
161161 Dialogs;
162162
163163type
164164
165165 { TForm1 }
166166
167167 TForm1 = class(TForm)
168168 Chart1: TChart;
169169 SinSeries: TLineSeries;
170170 CosSeries: TLineSeries;
171171 SinCosSeries: TLineSeries;
172172 procedure FormCreate(Sender: TObject);
173173 private
174174 { private declarations }
175175 public
176176 { public declarations }
177177 end;
178178
179179var
180180 Form1: TForm1;
181181
182182implementation
183183
184184{$R *.lfm}
185185
186186{ TForm1 }
187187
188188procedure TForm1.FormCreate(Sender: TObject);
189189const
190190 N = 100;
191191 MIN = -10;
192192 MAX = 10;
193193var
194194 i: Integer;
195195 x: Double;
196196begin
197197 for i:=0 to N - 1 do begin
198198 x := MIN + (MAX - MIN) * i / (N - 1);
199199 SinSeries.AddXY(x, sin(x));
200200 CosSeries.AddXY(x, cos(x));
201201 SinCosSeries.AddXY(x, sin(x)*cos(x));
202202 end;
203203
204204end;
205205
206206end.
207207```
208208
209209### Unit.lfm
210210
211-``` pascal
211+``` lfm
212212object Form1: TForm1
213213 Left = 14
214214 Height = 244
215215 Top = 137
216216 Width = 347
217217 Caption = 'Form1'
218218 ClientHeight = 244
219219 ClientWidth = 347
220220 OnCreate = FormCreate
221221 LCLVersion = '1.1'
222222 object Chart1: TChart
223223 Left = 0
224224 Height = 244
225225 Top = 0
226226 Width = 347
227227 AxisList = <
228228 item
229229 Grid.Color = clSilver
230230 Minors = <>
231231 Title.LabelFont.Orientation = 900
232232 Title.LabelFont.Style = [fsBold]
233233 Title.Visible = True
234234 Title.Caption = 'y axis'
235235 end
236236 item
237237 Grid.Color = clSilver
238238 Alignment = calBottom
239239 Minors = <>
240240 Title.LabelFont.Style = [fsBold]
241241 Title.Visible = True
242242 Title.Caption = 'x axis'
243243 end>
244244 BackColor = clWhite
245245 Foot.Brush.Color = clBtnFace
246246 Foot.Font.Color = clBlue
247247 Legend.Alignment = laBottomCenter
248248 Legend.ColumnCount = 3
249249 Legend.Visible = True
250250 Title.Brush.Color = clBtnFace
251251 Title.Font.Color = clBlue
252252 Title.Font.Style = [fsBold]
253253 Title.Text.Strings = (
254254 'My first chart'
255255 )
256256 Title.Visible = True
257257 Align = alClient
258258 ParentColor = False
259259 object SinSeries: TLineSeries
260260 Title = 'y=sin(x)'
261261 LinePen.Color = clRed
262262 end
263263 object CosSeries: TLineSeries
264264 Title = 'y=cos(x)'
265265 LinePen.Color = clBlue
266266 end
267267 object SinCosSeries: TLineSeries
268268 Title = 'y=sin(x)*cos(x)'
269269 LinePen.Color = clGreen
270270 end
271271 end
272272end
273273```
274274
275275---
276276*From the Free Pascal wiki, page "TAChart Tutorial: Getting started", as it stood on 2023-04-12 after a change by Trev, 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).*

Back to history