Sign in Join
Wiki copytemporary

TAChart Tutorial: ListChartSource, Logarithmic Axis, Fitting/fi: what changed

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

OldNewLine
11## Johdanto
22
33![](/wiki-images/3/3d/TAChart_LogAx_Tutorial15.png)
44
55Tiedätkö mikä on [Mooren laki](http://fi.wikipedia.org/wiki/Mooren_laki)? Siihen tutustutaan tässä esimerkissä.
66
77Lukiessasi tätä olet todennäköisesti pöytätietokoneen, kannettavan tai älypuhelimen ääressä. Valtava edistyminen mikrotietokoneteknologiassa on tapahtunut viimeisten vuosikymmenten aikana ja on edelleen käynnissä. Tämä pienentäminen elektronissa laitteissa mahdollistaa pakata yhä enemmän toimintoja yhdelle piisirulle. Ja mitä Mooren laki on: transistoreiden määrä sirulla on kaksinkertaistunut noin kahden vuoden välein.
88
99Tässä opetusohjelmassa otamme datan mikroprosessorien valmistajan julkaistuista tiedoista ja käytetään TAChart piirtämään transistoreihen määrää piirillä eri vuosina markkinoille tulleista tuotteista. Näiden tietojen avulla yritämme vahvistaa Mooren lain.
1010
1111Tässä käytettävä data löytyy sivustolta [www.intel.com/pressroom/kits/quickreffam.htm](http://www.intel.com/pressroom/kits/quickreffam.htm) joka sisältää luettelon mikroprosessoreista, ajankohdista niiden tuomisista markkinoille ja niiden sisältämä transistorin määrä per piiri.
1212
1313Tässä esimerkissä opitaan
1414
1515- miten syötetään dataa suunnitteluaikana
1616- miten luodaan pistekuvaaja jossa nimiteksti kullakin datapisteellä
1717- miten luodaan logaritminen akseli
1818- miten sovittaa yhtälö tietoihin.
1919
2020Sinulla pitäisi olla joitakin perustietoja miten työskennellä TAChart-komponentin kanssa jos ei ole niin katso [[TAChart Tutorial: Getting started/fi|TAChart:n aloitusopas]]. Tietenkin, sinun on tunnettava [[Lazarus]] ja [[Object Pascal]] . Eikä myöskään saisi pelätä matematiikkaa (käytössä logaritmit ja eksponenttifunktio).
2121
2222## Valmistautuminen
2323
2424### Kaavion tekeminen
2525
2626- Luo uusi projekti.
2727- Koska laitamme joitakin pitkiä otsikoita akselille niin kasvata lomakkeen kokoa vähintään 540 x 320 pikseliin.
2828- Lisää `TChart` komponentti, laita komponenttimuokkaimessa sen align arvoon `alClient`, Aseta sen `BackColor` arvoksi `clWhite` ja `Grid.Color` molemmille akselille arvoon `clSilver`.
2929- Lisää otsikoksi x-akselille "Year of market introduction" (tai suomenkielisenä "Markkinoilletulo vuosi") ja y-akselille "Number of transistors" (Transistorien määrä).
3030- Lisää teksti "Progress in Microelectronics" (tai "Mikroelektroniikan edistys") koko kaavion otsikoksi.
3131- Aseta näiden fontin tyyliksi `fsBold`.
3232- Näytä viitteenä datan lähde alatunnisteessa. Käytä kaavion ominaisuutta [[TAChart documentation|Foot]] siinä. Huomaa, että omaisuus `Foot.Text` (samoin kuin `Title.Text` ) voi syöttää usean rivin otsikon.
3333
3434![](/wiki-images/1/17/TAChart_LogAx_Tutorial1.png)
3535
3636### Luodaan pistekuvaaja
3737
3838Kun halutaan piirtää kullekin datatietueelle yksi piste - tätä usein kutsutaan pistekuvaajaksi muissa kaavio-ohjelmissa. TAChart kuvajaajissa ei ole tälläistä kuvaajaa koska [[TAChart documentation|TLineSeries]] voi tehdä tämän yhtähyvin. Laittamalla sen ominaisuus `ShowPoints` arvoon `true` ja poistamalla yhdistävät viivat (`LineType` = `ltNone`). Näytettävat datapisteet kuvataan ominaisuudessa `Pointer`.
3939
4040Niin lisätään viivakuvaaja (LineSeries) ja asetetaan sen ominaisuudet niin että siitä tulee pistekuvaaja. Lisäksi asetetaan `Pointer.Brush.Color` arvoon `clRed` ja `Pointer.Style` arvoon `psCircle` tämä asettaa näyttämään kunkin datapisteen punaiselta ympyrältä.
4141
4242## Syötetään data
4343
4444### ListChartSource
4545
4646On monia tapoja, joilla tietoja voidaan syöttää kaavioon. Nyt tässä käytetään [[TAChart documentation|ListChartSource]]:a. Yleensä tiedot ladataan tähän dynaamisesti ajon aikana, esimerkiksi lukemalla tiedot tiedostosta. Tässä projektissa kuitenkin data syötetään suunnitteluaikana käyttämällä datapisteen päätoimittaja ListChartSource. Tämän suurin etu on että kaikki muutokset, jotka tehdään kaavioon näytetään heti, ei tarvitse edes kääntää projektia. Kuten , on kuitenkin olemassa joitakin haittoja samoin.
4747
4848ListChartSource tallentaa piirrettävän datan luetteloon - siitähän sen englanninkielinen nimi tulee. Luettelossa olevia kohteita kutsutaan `TChartDataItem` :ksi. Se sisältää datapisteen tietoja:
4949
5050- `x` ja `y` koordinaatit,
5151- `Text` jota käytetään datan merkintään
5252- `Color` eli väri-tieto joka ylikirjoittaa kuvaajan oletusvärin (SeriesColor)
5353- `YList` joka sisältää lisää y:n arvoja joita tarvitaan joissakin erikoistapauksissa.
5454
5555ListChartSource on samantyyppinen kaavion lähde, joka on käytössä monien kuvaajien luokkien sisällä. Kun se on erillinen komponentti on siitä kuitenkin etua, että samaa kaavion lähdettä voidaan käyttää uudelleen muissa kuvaajissa - tarvitsemme tätä ominaisuutta myöhemmin sovellettaessa FitSeries.
5656
5757![](/wiki-images/3/3b/TAChart_LogAx_Tutorial2.png) Lisäämällä TListChartSource lomakkeelle. Tämä löytyy toisena kuvakeena chart-komponenttipaletilta. Kytke kuvaaja osoittamaan tähän lähteeseen asettamalla ominaisuus `Source` uuteen arvoon `ListChartSource1`.
5858
5959### ListChartSource datapisteiden muokkain
6060
6161![](/wiki-images/a/af/TAChart_LogAx_Tutorial16.png)
6262
6363Klikkaamalla kolmeapistepainiketta ominaisuudessa `DataPoints` avautuu datapisteiden muokkain. Tämä on ruudukko, johon syötetään tiedot. Kirjoita tiedot yllä olevasta kuvasta. Ne ovat otettu verkkosivuilta joka mainitaan johdannossa. Tämä sivusto näyttää markkinoille tulon ajan vuosittain ja kuukausittain - yksinkertaisuuden vuoksi ohitetaan kuukausi ja pyöristetään se lähimpään kalenterivuoteen. Anna vuosi sarakkeeseen **X**. Sarake **Y** saa transistorien määrä per piiri. Jokaisen mikroprosessorin nimi siirtyy sarakkeeseen **Teksti**. Koska ei anneta yksittäisen datapisteen väriä ohitetaan sarake **Väri** - mutta tietenkin sitä ominaisuutta voi kokeilla muuttaa.
6464
6565Kun datapisteiden muokkain suljetaan niin sarja päivitetään automaattisesti ja näkyvissä on päivitetty tietojoukko. Vau!
6666
6767### Näytetään datapisteen nimi
6868
6969Miksi emme näe prosessorien nimiä jotka asetettiin ruudukossa? [[TAChart documentation|Datapisteen nimi]] on oletuksena pois päältä. Valitse kuvaaja ja siirry kohtaan `Marks` . Katso aliominaisuutta `Style` , se on `smsNone` joka tarkoittaa "off" eli poispäältä. Avaa kyseisen kohdan pudotusvalikko. Näet erilaisia ​​vaihtoehtoja kuinka datapisteiden nimi voidaan merkitä. Testaa eri asetuksia. Jo suunnitteluaikana nähdään mitä ne tarkoittavat. Ota lopuksi käyttöön `smsLabel` joka näyttää tekstin.
7070
7171On myös yhdistävä viiva nimen ja datapisteen välillä. Sen väri on oletuksena valkoinen, siksi sitä ei nähdä valkoisella taustalla. Tämän viivan ominaisuuksia muutetaan `LinkPen` kohdan alla. Aseta `LinkPen.Color` arvoon `clGray` .
7272
7373Näiden nimien paikkaa pystyy jossain määrin muuttamaan [[TAChart documentation|MarkPositions]] ominaisuudessa.
7474
7575![](/wiki-images/a/a4/TAChart_LogAx_Tutorial3.png)
7676
7777Jos ohjelma käännetään nyt niin mitään uutta ei tule verrattuna suunnitteluaikaiseen näkymään. Tämä on suuri etu kun työskentelee ListChartSource:n datapisteiden muokkaimella.
7878
7979On kaksi asiaa, joita voidaan parantaa tässä vaiheessa:
8080
8181- Kaavion reunoilla datapisteiden nimet leikkautuvat pois. Sitä ei voi korjata lisäämällä kaavion `Margin.Left` ja `Margin.Right` arvoksi 24. Koska [[TAChart documentation|Margin]] määrittää ympäröivän tilan sisemmällä alueella pitää sen vapaana datapisteistä. On myös ominaisuus [[TAChart documentation|MarginExternal]] joka ympäröi kaavion ulompaa aluetta ja sillä voidaan muuttaa etäisyyttä naapurikontrolleihin.
8282- Koska lähes kaikki datapisteet ovat kaavion alareunassa niin se ei ole kovin mielekästä. Tämän vuoksi tarvitsemme logaritmistä akselia.
8383
8484## Otetaan logaritminen akseli käyttöön
8585
8686Datan näyttäminen logaritmisella akselilla tarkoittaa, että logaritmit data-arvot on piirretty, ei arvoja suoraan. Kaaviossa Y: n arvot vaihtelevat välillä 2300 ja 731 miljoonaa. Kun lasketaan ([decadic](https://en.wikipedia.org/wiki/Common_logarithm)/log<sub>10</sub>) logaritmit, alue on vain noin 3,3 ja 7,1 - tällaisessa kaaviossa tämä data voidaan erottaa paljon helpommin toisistaan.
8787
8888### TChartTransformations and LogarithmicAxisTransform
8989
9090![](/wiki-images/3/36/TAChart_LogAx_Tutorial4.png) Calculating the logarithm can be done by TAChart automatically. The main advantage is that the original data units are drawn on the axis while the logarithms are used for plotting.
9191
9292In fact, there is an entire group of components for axis transformations: [[TAChart documentation|TChartAxisTransformations]]. An axis transformation is a function which maps "real world" data in units as displayed on the axis (*"axis coordinates"*) to internal units that are common to all series in the same chart (*"graph coordinates"*). The axis coordinate of the transistor count of the 4004 processor, for example, is 2300, the graph coordinate is the logarithm of that number, i.e. log<sub>10</sub>(2300) = 3.36.
9393
9494TAChart provides a variety of transforms. In addition to the logarithm transform, there is a [[TAChart documentation|linear transform]] which allows to multiply the data by a factor and to add an offset. The [[TAChart documentation|auto-scaling transform]] is useful when several independently scaled series have to be drawn on the same axis. The [[TAChart documentation|user-defined transform]] allows to apply any arbitrary transformation.
9595
9696![](/wiki-images/5/55/TAChart_LogAx_Tutorial5.png)
9797
9898Add a `TAChartTransformations` component to the form, double-click on it (or right-click on it in the object tree and select "Edit axis transformations"), click on "Add" and select "Logarithmic". This will create a `ChartAxisTransformations1LogarithmAxisTransform1` component - what a name! Anyway, there's a high chance that you will not have to type it...
9999
100100In the object inspector, you will see only a few properties - the most important one is `Base`. This is the base of the logarithm to be calculated. Change it to 10, since we want to calculate the [decadic logarithms](https://en.wikipedia.org/wiki/Common_logarithm) - this case comprises 99% of all logarithmic charts.
101101
102102A side-note: it is very important that you **save the project at this time**. Why? You will see in a minute...
103103
104104Now we must identify the axis which is to be transformed. For this purpose each axis has a property `Transformations`. In our case, the huge numbers are plotted on the y axis. So, go to the left axis and set its property `Transformations` to `ChartAxisTransformations1`.
105105
106106![](/wiki-images/0/0d/TAChart_LogAx_Tutorial17.png)
107107
108108Oh - what's that? An error message pops up reporting a range check error. Hopefully you had saved the project. If you click on Cancel Lazarus will shut down, everything will be lost. If you click on OK suddenly the chart is gone, it reappears when you click somewhere, but the faulty axis is hidden ...
109109
110110Are you getting desperate?
111111
112112This is the main disadvantage of working at design-time. If something goes wrong, there is no debugger, no indication of what caused the trouble. Since components are compiled into Lazarus you would have to debug the IDE. Sounds complicated...
113113
114114Let's sit down and think what we did. We assigned the logarithmic transform to the y axis. The transform does not yet have any connection to the data, therefore the data are still in "real" units, their maximum is 731 million. But the transform "thinks" that the data are already in graph units (logarithms). When it calculates the axis labels in axis units it takes hundreds of millions to the power of 10! That's what causes the range check error. However, the situation is not always so dramatic; the least thing that could happen is that the data are not transformed, but the axis is.
115115
116116What can we do against that? Each series has properties `AxisIndexX` and `AxisIndexY`. The transform can use this information to calculate the logarithms of the correct coordinates before the axis labels are updated. This solves our problem.
117117
118118So, click on Cancel to shutdown Lazarus. Restart and reload the project. In the saved state, the transformation is not yet connected to the axis.
119119
120120Now have a look at the object tree above the object inspector and you will see that the left axis has index 0. So set the series' `AxisIndexY` to this value. Although not necessary it may be a good idea to disarm also the x axis by assigning its index 1 to the `AxisIndexX` of the series - who can guarantee that we won't transform the x axis in the future?
121121
122122After this is done you can set the `LeftAxis.Transformation` to `ChartTransformations1` without the range check error.
123123
124124With the logarithm transform activated the data points spread nicely across the y axis range now. But what's wrong with the y axis labels? And the years on the x axis are too close and partly overlap.
125125
126126![](/wiki-images/d/d0/TAChart_LogAx_Tutorial6.png)
127127
128128Finding axis labels is a non-trivial task, in particular when transformations are active that heavily distort the axis intervals. Unfortunately, logarithmic axes belong to that group. Basically, there are two ways to control label positioning, an **automatic** and a **manual** way.
129129
130130### Automatic finding of axis labels
131131
132132For automatic label positioning, each axis has a property [[TAChart documentation|Intervals]] which gives access to several, partly mutually excluding parameters - please see [[TAChart documentation]] for an explanation. In case of the logarithmic axis the issue is usually caused by the fact that the option `aipGraphCoordinates` is not set. This option, if set, enforces calculation of the tick intervals for the transformed data ("graph coordinates"), not the "real world" data ("axis coordinates"). So, set `aipGraphCoordinates` in the `LeftAxis.Intervals.Options`. The labels jump to more evenly distributed locations.
133133
134134![](/wiki-images/f/f1/TAChart_LogAx_Tutorial7.png)
135135
136136Depending on the size of your form you may get quite nice, or not so good labels. If you resize the form you will see some "crooked" labels jump in.
137137
138138You can improve the quality of label presentation in the following way:
139139
140140- Moderately increase the `Intervals.Tolerance` to something like 5.
141141- Adjust the range in which the label distance can vary. This is defined by the properties `Intervals.MaxLength` and `Intervals.MinLength`. The optimum value depends on the size of the chart and on the range of the data. In our example project, good labels are obtained by setting these properties to 100 and 50, respectively.
142142
143143In the same way, the overlapping year labels of the x axis can be addressed. Just increase the `BottomAxis.Intervals.MaxLength` to 70.
144144
145145What is left now is the "1" that appears at the y axis between "10000000" and "1E009". This is due to a bug in some FPC versions. If you have that as well, simply change the property `LeftAxis.Marks.Format`. This string is passed to the `Format` function to convert the numbers to strings. The format specifier "%0.0n" for example avoids that conversion error and, additionally, adds nice thousand separators to the labels which makes them much more readable.
146146
147147![](/wiki-images/7/7e/TAChart_LogAx_Tutorial8.png)
148148
149149### Manual finding of axis labels
150150
151151This is the best we can do with automatic label positioning. It is not perfect because when we increase the height of the window, or zoom in, the half-decade values may appear, or the label interval may be two decades as in above figure.
152152
153153If you are not happy with that you have to use **manual axis label selection**. For this purpose, each axis has a property `Source` which can be linked to a ListChartSource containing only the allowed axes labels. So when this chart source contains only full-decade labels there is no risk of half-decade labels or omitting every other label. On the other hand, when you zoom into the chart you may come to a point where no labels are visible any more.
154154
155155Add a second ListChartSource to the form. You can use the DataPoints editor again to enter the full-decade numbers. This has the advantage that you can complete most of this project without writing a single line of code!
156156
157157But you could also populate the listsource easily in the `FormCreate` event:
158158
159159``` pascal
160160procedure TForm1.FormCreate(Sender: TObject);
161161const
162162 MIN = 0;
163163 MAX = 12;
164164var
165165 i: Integer;
166166 value: double;
167167begin
168168 for i:=MIN to MAX do begin
169169 value := Power(10, i);
170170 ListChartSource2.Add(value, value);
171171 end;
172172end;
173173```
174174
175175This procedure adds powers of 10 in a wide enough range to the ListChartSource by means of its method `Add`.
176176
177177Connect `ListChartSource1` to `LeftAxis.Marks.Source` to activate the manual labels of the ListChartSource. You should also remove all flags from the `Options` property. Otherwise automatic tick finding will still be active to some degree. If you did not use the DataPoints editor you must compile to see the effect.
178178
179179![](/wiki-images/5/51/TAChart_LogAx_Tutorial9.png)
180180
181181### Minor tick marks
182182
183183Very often minor tick marks are placed between the major tick marks. TAChart allows to add several sets of minor ticks to each axis. We only need one here. Go to `LeftAxis` and click on the ellipsis button next to the property `Minors`. This opens the editor for `Chart1.AxisList[0].Minors`. Click on "Add" and on the "M" in the list below. Now you can adjust the parameters in the object inspector to get "good" minor ticks. If the major ticks on a logarithmic axis are at full decades then the minor ticks usually are at 2, 3, 4,..., 8, 9, and, of course, powers of 10. This can be achieved easily by turning off all `Intervals.Options` except for `aipUseCount` and setting `Intervals.Count = 9`. Of course, this makes sense only when the major labels are fixed at full decades like in the manual approach above.
184184
185185Usually the plot gets too crowded by the minor grid which appears now, you should set the minor's `Grid.Visible` to `false`.
186186
187187![](/wiki-images/5/5c/TAChart_LogAx_Tutorial10.png)
188188
189189## Fitting
190190
191191Now let's look for a relation between the data, i.e. we want to find a mathematical formula which describes the dependence of transistor count on market introduction year. This is called "fitting": we select a formula with parameters and adjust the parameters such that the deviation to the data is as small as possible.
192192
193193TAChart does not contain a full-fledged fitting engine. It uses the fitting routines from the FPC numerical library [[numlib]]. Therefore, TAChart cannot address all variants of fitting, but it covers the most important case, fitting of a [polynomial](https://en.wikipedia.org/wiki/Polynomial) by means of the [linear least squares technique](https://en.wikipedia.org/wiki/Least_squares). This is about the level available to Excel users when they add a "trend line" to their chart.
194194
195195### TFitSeries
196196
197197TAChart provides a specialized [[TAChart documentation|TFitSeries]] for fitting. This series has a property `FitEquation` which defines the formula that is used:
198198
199199- `fePolynomial`: y = b<sub>0</sub> + b<sub>1</sub>x + b<sub>2</sub>x<sup>2</sup> + … + b<sub>n</sub>x<sup>n</sup>. Specify the number of fitting parameters a<sub>i</sub> by the property `ParamCount` = n + 1.
200200- `feLinear`: y = a + bx -- this is a special case of the general polynomial with n = 1 and fitting parameters a and b. It is made available as a separate item because straight lines define the most important fitting conditions.
201201- `feExp`: y = a * e<sup>bx</sup> -- This equation can also be reduced to the polynomial case although this is not straightforward to see. But take the (natural) logarithm of this equation, and you get to ln(y) = a + bx. Now when we fit ln(y) instead of y we have the linear case again.
202202- `fePower`: y = a * x<sup>b</sup>. Again, this can be reduced to a linear equation by a logarithmic transformation.
203203
204204![](/wiki-images/b/b5/TAChart_LogAx_Tutorial11.png)
205205
206206Enough of theory. Let's add a FitSeries to the chart: double-click on the chart, and in the series editor click on "Add" and select the entry "Least squares fit series" from the dropdown list.
207207
208208At first, we need to tell the fit series where it finds its data. For this purpose, we connect the series' `Source` with `ListChartSource1` as we had done with the line series. You see: the same chart source can be used for several series.
209209
210210You hopefully remember the disaster above with the AxisIndex. So, set the `AxisIndexY` to the index of the left axis as we did with the line series.
211211
212212Which one of the four `FitEquation` possibilities do we select? Well, the data look like lying on a straight line. So let's select `feLinear`.
213213
214214![](/wiki-images/0/06/TAChart_LogAx_Tutorial12.png)
215215
216216Oops... We see the black fitted curve, but it does not "fit" at all. And we wanted a straight line, but we get a twisted curve. How can this be?
217217
218218The reason is the logarithmic transform that we applied to the y data. Therefore, our plot shows the logarithms, but the fit takes the "raw" data. We are effectively fitting the straight line to the data in the screenshot in the section Displaying datapoint marks where the log transform had not yet been introduced - it is clear that the line would not "fit" there. And when the fitted function is drawn the log transform distorts the straight line to the twisted curve that we see.
219219
220220On the other hand, if the **log data** follow a straight line our fitting law is not linear, but exponential. Let's set `FitEquation` to `feExp` and try again.
221221
222222![](/wiki-images/a/ac/TAChart_LogAx_Tutorial13.png)
223223
224224Ah - much better!
225225
226226Now we know that the exponential law, y = a * x<sup>b</sup>, is a good description of our data. But how do we get the fitting parameters a and b?
227227
228228### Fit results
229229
230230The fit series has a public array property `Param` which contains the fitting parameters. `a` is in `Params[0]`, and `b` is in `Params[1]`. Of course, these values are correct only when a valid fit has been performed. How do we know that? Well, the fit series provides an event `OnFitComplete` that is generated when the fit complete successfully. That's where we can evaluate the obtained fit parameters. As an example, let's display the fit results in a message along with the fit equation:
231231
232232``` pascal
233233procedure TForm1.Chart1FitSeries1FitComplete(Sender: TObject);
234234begin
235235 with Chart1FitSeries1 do
236236 ShowMessage(Format(
237237 'Fit result: a = %g, b = %g', [
238238 Param[0], Param[1]
239239 ]));
240240end;
241241```
242242
243243And that's what we get:
244244
245245![](/wiki-images/d/d1/TAChart_LogAx_Tutorial14.png)
246246
247247Now we want to calculate the time T until the number of transistors on a chip is doubled. As an exercise try to show that
248248
249-`T = ln(2) / b`
249+```text
250+T = ln(2) / b
251+```
250252
251253It would be nice to show the doubling time as an additional line of the chart title. For this, we modify the `OnFitComplete` event handler as follows:
252254
253255``` pascal
254256procedure TForm1.Chart1FitSeries1FitComplete(Sender: TObject);
255257begin
256258 Chart1.Title.Text.Add(Format(
257259 'The number of transistors doubles every %.0f years',
258260 [ln(2) / Chart1FitSeries1.Param[1]]
259261 ));
260262end;
261263```
262264
263265![](/wiki-images/3/3d/TAChart_LogAx_Tutorial15.png)
264266
265267Vau! Tämä on Mooren laki: "Transistoreiden määrä per siru kaksinkertaistuu kahden vuoden välein" ...
266268
267269## Lähdekoodi
268270
269271### Project file
270272
271273``` pascal
272274program project1;
273275
274276{$mode objfpc}{$H+}
275277
276278uses
277279 {$IFDEF UNIX}{$IFDEF UseCThreads}
278280 cthreads,
279281 {$ENDIF}{$ENDIF}
280282 Interfaces, // this includes the LCL widgetset
281283 Forms, Unit1, tachartlazaruspkg
282284 { you can add units after this };
283285
284286{$R *.res}
285287
286288begin
287289 //RequireDerivedFormResource := True;
288290 Application.Initialize;
289291 Application.CreateForm(TForm1, Form1);
290292 Application.Run;
291293end.
292294```
293295
294296### Unit1.pas
295297
296298``` pascal
297299unit Unit1;
298300
299301{$mode objfpc}{$H+}
300302
301303interface
302304
303305uses
304306 Classes, SysUtils, FileUtil, TAGraph, TASeries, TASources, Forms, Controls,
305307 Graphics, Dialogs, TACustomSource, TATransformations, TAFuncSeries;
306308
307309type
308310
309311 { TForm1 }
310312
311313 TForm1 = class(TForm)
312314 Chart1: TChart;
313315 Chart1FitSeries1: TFitSeries;
314316 Chart1LineSeries1: TLineSeries;
315317 ChartAxisTransformations1: TChartAxisTransformations;
316318 ChartAxisTransformations1LogarithmAxisTransform1: TLogarithmAxisTransform;
317319 ListChartSource1: TListChartSource;
318320 ListChartSource2: TListChartSource;
319321 procedure Chart1FitSeries1FitComplete(Sender: TObject);
320322 procedure FormCreate(Sender: TObject);
321323 private
322324 { private declarations }
323325 public
324326 { public declarations }
325327 end;
326328
327329var
328330 Form1: TForm1;
329331
330332implementation
331333
332334{$R *.lfm}
333335
334336uses
335337 math;
336338
337339{ TForm1 }
338340
339341procedure TForm1.FormCreate(Sender: TObject);
340342const
341343 MIN = 0;
342344 MAX = 12;
343345var
344346 i: Integer;
345347 value: double;
346348begin
347349 for i:=MIN to MAX do begin
348350 value := IntPower(10, i);
349351 ListChartSource2.Add(value, value);
350352 end;
351353 Chart1FitSeries1.ExecFit;
352354end;
353355
354356procedure TForm1.Chart1FitSeries1FitComplete(Sender: TObject);
355357begin
356358 {
357359 with Chart1FitSeries1 do
358360 ShowMessage(Format(
359361 'Fit result: a = %g, b = %g', [
360362 Param[0], Param[1]
361363 ]));
362364 }
363365 Chart1.Title.Text.Add(Format(
364366 'The number of transistors doubles every %.0f years',
365367 [ln(2) / Chart1FitSeries1.Param[1]]
366368 ));
367369end;
368370
369371end.
370372```
371373
372374### Unit1.lfm
373375
374-``` pascal
376+``` lfm
375377object Form1: TForm1
376378 Left = 244
377379 Height = 356
378380 Top = 193
379381 Width = 552
380382 Caption = 'Form1'
381383 ClientHeight = 356
382384 ClientWidth = 552
383385 OnCreate = FormCreate
384386 LCLVersion = '1.1'
385387 object Chart1: TChart
386388 Left = 0
387389 Height = 356
388390 Top = 0
389391 Width = 552
390392 AxisList = <
391393 item
392394 Grid.Color = clSilver
393395 Marks.Format = '%0:.0n'
394396 Marks.Source = ListChartSource2
395397 Marks.Style = smsCustom
396398 Minors = <
397399 item
398400 Grid.Visible = False
399401 Intervals.Count = 9
400402 Intervals.MinLength = 5
401403 Intervals.Options = [aipUseCount]
402404 end>
403405 Title.LabelFont.Orientation = 900
404406 Title.LabelFont.Style = [fsBold]
405407 Title.Visible = True
406408 Title.Caption = 'Number of transistors'
407409 Transformations = ChartAxisTransformations1
408410 end
409411 item
410412 Grid.Color = clSilver
411413 Intervals.MaxLength = 60
412414 Alignment = calBottom
413415 Minors = <>
414416 Title.LabelFont.Style = [fsBold]
415417 Title.Visible = True
416418 Title.Caption = 'Year of market introduction'
417419 end>
418420 BackColor = clWhite
419421 Foot.Alignment = taLeftJustify
420422 Foot.Brush.Color = clBtnFace
421423 Foot.Font.Color = clBlue
422424 Foot.Text.Strings = (
423425 'Source:'
424426 'http://www.intel.com/pressroom/kits/quickreffam.htm'
425427 )
426428 Foot.Visible = True
427429 Margins.Left = 24
428430 Margins.Right = 24
429431 Title.Brush.Color = clBtnFace
430432 Title.Font.Color = clBlue
431433 Title.Font.Style = [fsBold]
432434 Title.Text.Strings = (
433435 'Progress in Microelectronics'
434436 )
435437 Title.Visible = True
436438 Align = alClient
437439 ParentColor = False
438440 object Chart1LineSeries1: TLineSeries
439441 Marks.Format = '%2:s'
440442 Marks.LinkPen.Color = clGray
441443 Marks.Style = smsLabel
442444 AxisIndexY = 0
443445 LineType = ltNone
444446 Pointer.Brush.Color = clRed
445447 Pointer.Style = psCircle
446448 ShowPoints = True
447449 Source = ListChartSource1
448450 end
449451 object Chart1FitSeries1: TFitSeries
450452 AxisIndexX = 1
451453 AxisIndexY = 0
452454 FitEquation = feExp
453455 OnFitComplete = Chart1FitSeries1FitComplete
454456 ParamCount = 2
455457 Source = ListChartSource1
456458 end
457459 end
458460 object ListChartSource1: TListChartSource
459461 DataPoints.Strings = (
460462 '1972|2300|?|4004'
461463 '1974|6000|?|8080'
462464 '1978|29000|?|8086'
463465 '1982|134000|?|80286'
464466 '1986|275000|?|80386'
465467 '1989|1200000|?|80486'
466468 '1993|3100000|?|Pentium'
467469 '1997|7500000|?|Pentium II'
468470 '2001|42000000|?|Xeon'
469471 '2006|152000000|?|Core Duo'
470472 '2009|731000000|?|Core i7'
471473 )
472474 left = 240
473475 top = 40
474476 end
475477 object ChartAxisTransformations1: TChartAxisTransformations
476478 left = 243
477479 top = 96
478480 object ChartAxisTransformations1LogarithmAxisTransform1: TLogarithmAxisTransform
479481 Base = 10
480482 end
481483 end
482484 object ListChartSource2: TListChartSource
483485 left = 243
484486 top = 176
485487 end
486488end
487489```
488490
489491---
490492*From the Free Pascal wiki, page "TAChart Tutorial: ListChartSource, Logarithmic Axis, Fitting/fi", as it stood on 2020-02-29 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