Difference between revisions of "TAChart Tutorial: ListChartSource, Logarithmic Axis, Fitting"

From Free Pascal wiki
Jump to navigationJump to search
Line 1: Line 1:
After doing the first steps with TAChart in the Getting Started tutorial, here is another tutorial. This one will be more advanced. It will cover the aspects
+
== Introduction ==
 +
After doing the first steps with TAChart in the [[TAChart tutorial: Getting started|Getting Started]] tutorial, here is another tutorial. This one will be more advanced. It will cover the aspects
  
 
* How to apply a user-defined chart source
 
* How to apply a user-defined chart source
Line 194: Line 195:
  
 
== Fitting ==
 
== Fitting ==
 +
Now let's look for a relation between the data, i.e. we want to find a mathematical formula which is able to describe the dependence of transistor count on market introduction year.This is called "fitting": we select a forumula with parameters and adjust the parameters such that the deviation to the data is at minumum.
 +
 +
TAChart does not contain a full-fledged fitting engine. It just "borrows" the fitting routines available within FPC ("numlib"). Therefore, TAChart cannot cover all variants of fitting, but it covers the most important case, fitting of a polynomial by means of the [[wikipedia:least squares|linear least squares technique]]. This is about the level available to Excel users when they add a "trend line" to their chart.
 +
 +
=== TFitSeries ===
 +
 +
TAChart provides a specialized TFitSeries for fitting. This series has a property <code>FitEquation</code> which defines the formula that is used:
 +
 +
* <code>fePolynomial</code>: y = a<sub>0</sub> + a<sub>1</sub> x + ... + a<sub>n</sub> x<sup>n</sup>. Specify the number of fitting parameters a<sub>i</sub> by the property <code>ParamCount</code> = n + 1.
 +
* <code>feLinear</code>: y = a + b x -- 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.
 +
* <code>feExp</code>: y = a e<sup>x</sup> = a exp(b x) - This equation can also be reduced to the polynomial case. It is not straightforward to see. But take the logarithm of this equation, and you get to log y = a + b x. Now when we fit log y instead of y we have the linear case again.
 +
* <code>fePower</code>: y = a x<sup>b</sup>. Again, this can be reduced to a linear equation by a logarithmic transformation.
 +
 +
[[File:TAChart_LogAx_Tutorial11.png]]
 +
 +
Enough of theory. Let's add a <code>FitSeries</code> 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.
 +
 +
Which one of the four <code>FitEquation</code> possibilities do we select? Well, the data look like lying on a straight line. So let's select <code>feLinear</code>.
 +
 +
Before we compile we need to tell the fit series where it finds its data. For this purpose, we connect the series' <code>Source</code> with the <code>UserDefinedChartSource1</code> as we had done with the line series. You see: the same chart source can be used in several series.

Revision as of 12:47, 11 August 2012

Introduction

After doing the first steps with TAChart in the Getting Started tutorial, here is another tutorial. This one will be more advanced. It will cover the aspects

  • How to apply a user-defined chart source
  • How to create a logarithmic axis
  • How to fit an equation to data

In order to have some meaningful data we will have a look at the development of integrated circuits. The reference www.intel.com/pressroom/kits/quickreffam.htm contains a list of microprocessors, their date of market introduction and their number of transistors per chip. We want to plot the transistor count as a function of the year of market introduction. In a susequent tutorial we will fit an exponential function to the data and verify "Moore's law" saying that the transistor count doubles every two years.

Preparation

Setting up the chart

  • Create a new project.
  • Resize the main form to 540 x 480 pixels.
  • Add a TAChart component, align it to alClient, set its BackColor to clWhite and the Grid.Color of each axis to clSilver.
  • Add a title to the x axis ("Year of market introduction") and to the y axis ("Number of transistors").
  • Use the text "Progress in Microelectronics" as the chart's title.
  • Set these font styles to fsBold.
  • Maybe it is a good idea to display above reference for our data in the footer - the chart property Foot can be used for that. Note that the property editor of Foot.Text (as well as Title.Text) allows to enter linefeeds for multi-lined titles.

TAChart LogAx Tutorial1.png

Data

For simplicity, we hard-code the data into our form. Of course it would be more flexible to read the data from a file, but this is a tutorial on TAChart, not on reading data files.

For the data, we declare a TDataRecord

type
  TDataRecord = record
    Year: double;
    Processor: string;
    TransistorCount: double;
  end;

and the data simply are stored in an array of these records:

const
  MAXDATA = 10;
  Data: array[0..MAXDATA] of TDataRecord = (
    (Year:1972; Processor:'4004'; TransistorCount:2300),
    (Year:1974; Processor:'8080'; TransistorCount:6000),
    (Year:1978; Processor:'8086'; TransistorCount:29000),
    (Year:1982; Processor:'80286'; TransistorCount:134000),
    (Year:1986; Processor:'80386'; TransistorCount:275000),
    (Year:1989; Processor:'80486'; TransistorCount:1.2E6),
    (Year:1993; Processor:'Pentium'; TransistorCount:3.1E6),
    (Year:1997; Processor:'Pentium II'; TransistorCount:7.5E6),
    (Year:2001; Processor:'Xeon'; TransistorCount:42E6),
    (Year:2006; Processor:'Core Duo'; TransistorCount:152E6),
    (Year:2009; Processor:'Core i7'; TransistorCount:731E6)
  );

The original table contains much more data, if you want to add more, don't forget to update MAXDATA, the upper index of the array. It should be mentioned that the table shows the date of market introduction by month and year - for simplicity, I skipped the month and rounded the date to the next nearest calendar year.

Creating a point series

We want to draw each TDataRecord as a single data point - this is called "PointSeries" in other charting programs. The TAChart series editor does not show this type of series because TLineSeries can do this as well. We only have to set its property ShowPoints to true and to turn off the connecting lines (LineType = ltNone). The symbols are determined by the property Pointer.

So, add a LineSeries to the form and set the properites to create a point series. Additionally, let's set the Pointer.Brush.Color to clRed, and the Pointer.Style to psCircle to draw a red circle at each data point.

The TUserDefinedChartSource

Normally, we would add the data to the built-in chart source of this series, as we did in the "Getting Started" tutorial. However, then we would have the data in memory twice: in the array shown above, and in the series - not so good: We would be wasting memory, and we would have to synchronize both storages when data points are added, deleted or edited.

TAChart LogAx Tutorial2.png

To avoid this, we add a TUserDefinedChartSource to the form, it is the third icon from the left in the Chart component palette. This chart source is made to interface to any kind of data storage. You just have to write an event handler for OnGetChartDataItem in order to define the data. For this purpose, the event takes a var parameter AItem of type TChartDataItem that is defined as follows (with elements omitted that are not needed here):

type
  TChartDataItem = object
    X, Y: Double;
    Color: TChartColor;
    Text: String;
    // ...
  end;

X and Y indicate the coordinates of the data point. The field Text can be used to assign a string to each data point. This string can be displayed in the chart above each data point. For this purpose, the series has a property Marks which determines how this string is displayed. Marks.Style determines whether the x or y value, or the Text label is displayed (smsXValue, smsValue, smsLabel, respectively), and there are even more options.

In our case, it may be a good idea to show the processor name above each data point. So we set the series' Marks.Style to smsLabel.

Marks usually have a white connecting line to the data point. Since our chart background is white as well these connecting lines are not visible. Open the series property Marks.LinkPen and set its color to clGray.

We could even give each data point an individual color by assigning a corresponding value to the property Color, but we don't want to use this feature here.

Now we can write the event handler for OnGetChartDataItem as follows:

procedure TForm1.UserDefinedChartSource1GetChartDataItem(
  ASource: TUserDefinedChartSource; AIndex: Integer; var AItem: TChartDataItem);
begin
  AItem.X := Data[AIndex].Year;
  AItem.Y := Data[AIndex].TransistorCount;
  AItem.Text := Data[AIndex].Processor;
end;

AIndex is the index of the datapoint which is queried. Since both chart source and our data array begin at index 0 we just look into our data array at this index and copy the data to the corresponding elements of the AItem.

There are still some important things to do:

  • Tell the UserDefinedChartSource how many data points the external data array contains. We have to enter this number in the property PointsNumber of the UserDefinedChartSource. In our case, this is the value of MAXDATA+1 (+1 because counting starts at 0), i.e. 11.
  • Tell the series to use the UserDefinedChartSource instead of the built-in source. For this purpose, we point the series' propery Source to UserDefinedChartSource1.
  • Since the UserDefinedChartSource does not know anything about the structure of the external data we have to notify it whenever data are available or have been changed. We do this by calling UserDefinedChartSource1.Reset at an appropriate place. Sometimes you are lucky that some other method may have done this already. But keep in mind that whenever a chart with a UserDefinedChartSource does not behave as you expect there is a chance that you may have forgotten to call Reset. Since our data are hardcoded in the project source the form's OnCreate event handler is a good place to do this:
procedure TForm1.FormCreate(Sender: TObject);
begin
  UserDefinedChartSource1.Reset;
end;

Now we can compile the project for the first time.

TAChart LogAx Tutorial3.png

As you can see, there are two things which can be improved at this stage:

  • The point labels are cut off at the edges of the chart. We can fix this by increasing the chart's Margin.Left and Margin.Right to 24.
  • Since almost all data points are crowded at the bottom of the chart the plot not very meaningful. This is when the logarithmic axis comes into play.

Using a logarithmic axis

Plotting data on a logarithmic axis means that the logarithms of the data values are plotted, not the values directly. The y values in our diagram, for example, range between 2300 and 731 millions. When we calculate the (decadic) logarithms, the range is only between about 3.3 and 7.1 - in such a diagram the data can be distinguished much easier.

TChartTransformations and LogarithmicAxisTransform

TAChart LogAx Tutorial4.png

Calculating the logarithm can be done by TAChart automatically. In fact, there is an entire group of components for this and other axis transformations: TATransformations. A transformation is a function which maps "real world" data in units as displayed on the axis ("axis coordinates") to 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(2300) = 3.36.

TAChart provides a variety of transforms. In addition to the logarithm transform, there is a linear transform which allows to multiply the data by a factor and to add an offset. The auto-scaling transform is useful when several independently scaled series have to be drawn on the same axis. The user-defined transform allows to apply any arbitrary transformation.

TAChart LogAx Tutorial5.png

Add 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...

In 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.

Now 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 connect it to ChartAxisTransformations1 by assigning the Transformations property accordingly. Ignore the strange axis labels for the moment.

Compile the program. Oh, it crashes due to a floating point exception. What is wrong? Well, TChart may contain several axes, and the series does not yet know which axis it belongs to. Normally, this is not a problem, but when transformations are involved the series' properties AxisIndexX and AxisIndexY have to be set properly. AxisIndexX is the index of the x axis in the chart's AxisList, AxisIndexY accordingly. When you look at the object tree you will see that the left axis has index 0 and the bottom axis has index 1. So we have to set AxisIndexX = 1 and AxisIndexY = 0.

Now the program compiles and runs. The data points spread nicely across the y axis range. But what's wrong with the y axis labels? And the years on the x axis are too close and partly overlap.

TAChart LogAx Tutorial6.png

Finding 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.

Automatic finding of axis labels

For automatic label positioning, each axis has a property 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 and compile again.

TAChart LogAx Tutorial7.png

Depending 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.

You can improve the quality of the labels in the following way:

  • Moderately increase the Intervals.Tolerance (5).
  • Adjust the range the distance between labels 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.

In the same way, the overlapping year labels of the x axis can be addressed. Just increase the BottomAxis.Intervals.MaxLength to 70.

What is left now is the "1" that appears at the y axis between "10000000" and "1E009". This is due to a bug of some FPC versions. If you have that issue like me, 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.

TAChart LogAx Tutorial8.png

Manual finding of axis labels

This is the best we can do with automatic label positioning. It is not perfect because when we increase the height of the window the half-decade values may appear, or the label interval may be two decads as in above figure.

If 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.

Add a TListChartSource to the form, and populate it in the FormCreate event:

procedure TForm1.FormCreate(Sender: TObject);
const
  MIN = 0;
  MAX = 12;
var
  i: Integer;
  value: double;
begin
  for i:=MIN to MAX do begin
    value := Power(10, i);
    ListChartSource1.Add(value, value);
  end;
end;

In this procedure, an integer between 0 and 12 is taken to the power of 10, and the result is stored in the ListChartSource by means of its Add procedure. The first parameter in this call is the x, the second parameter the y value of the TChartDataItem. We pass the result to both x and y values which is some kind of overkill, but it has the advantage that we'd already have labels if we'd once decide to draw the x axis logarithmically as well. Similarly, the range of labels between 1E0 and 1E12 is a bit generous for the same reason of flexibility.

Connect 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.

TAChart LogAx Tutorial9.png

Minor tick marks

Very 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 <coude>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.

Usually the plot gets too crowded by the minor grid which appears now, you should set the minor's Grid.Visible to false.

TAChart LogAx Tutorial10.png

Fitting

Now let's look for a relation between the data, i.e. we want to find a mathematical formula which is able to describe the dependence of transistor count on market introduction year.This is called "fitting": we select a forumula with parameters and adjust the parameters such that the deviation to the data is at minumum.

TAChart does not contain a full-fledged fitting engine. It just "borrows" the fitting routines available within FPC ("numlib"). Therefore, TAChart cannot cover all variants of fitting, but it covers the most important case, fitting of a polynomial by means of the linear least squares technique. This is about the level available to Excel users when they add a "trend line" to their chart.

TFitSeries

TAChart provides a specialized TFitSeries for fitting. This series has a property FitEquation which defines the formula that is used:

  • fePolynomial: y = a0 + a1 x + ... + an xn. Specify the number of fitting parameters ai by the property ParamCount = n + 1.
  • feLinear: y = a + b x -- 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.
  • feExp: y = a ex = a exp(b x) - This equation can also be reduced to the polynomial case. It is not straightforward to see. But take the logarithm of this equation, and you get to log y = a + b x. Now when we fit log y instead of y we have the linear case again.
  • fePower: y = a xb. Again, this can be reduced to a linear equation by a logarithmic transformation.

TAChart LogAx Tutorial11.png

Enough 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.

Which one of the four FitEquation possibilities do we select? Well, the data look like lying on a straight line. So let's select feLinear.

Before we compile we need to tell the fit series where it finds its data. For this purpose, we connect the series' Source with the UserDefinedChartSource1 as we had done with the line series. You see: the same chart source can be used in several series.