TAChart Tutorial: Userdefined ChartSource

This is an old revision of TAChart Tutorial: Userdefined ChartSource, edited by Wp at . The current revision is 82019.

Created page with "== Introduction == In this tutorial we want to create a plot the '''age distribution of world population'''. In doing this, you will learn * how to work with an '''area sea..."

Introduction

In this tutorial we want to create a plot the age distribution of world population. In doing this, you will learn

As usual, we assume that you are familiar with Lazarus and the ObjectPascal language and have some basic knowledge of the TAChart package; if you don't you should work through the Getting Started tutorial before continuing.

Preparation

Data

The site www.census.gov contains a wealth of demographic data. The link leads you directly to the data that we want to plot: world population as a function of age and gender.

Go to that page, select a year and click "submit". In the lower part of the window, you will see a table with the columns "Age", "Both Sexes Population", "Male population", "Female population", and "Sex ratio". Select the data in the table (including header) and copy them to the clipboard. Paste the data into a text editor and save as "population.txt".

Alternatively, you find the data also in the Source Code section of this tutorial.

Open Lazarus and create a new project. At first we have to load the data to make them available for plotting.

We define a TPopulationRecord and an array type TPopulationArray to hold the data of the population table:

type
  TPopulationRecord = record
    Age: Integer;
    Total: Double;
    Male: Double;
    Female: Double;
    Ratio: Double;
  end;

  TPopulationArray = array of TPopulationRecord;

var
  PopulationData: TPopulationArray;

Now let's read the file. The easiest way is to load the lines of the population file to a stringlist. The values in each line are separated by tab characters (#9). Therefore, we assign each line to the DelimitedText property of another stringlist which uses tab as a delimiter. This automatically splits each line into the values of each column. Then we convert the individual strings of the second stringlist to numbers that we store in the array PopulationData. Have a look at the procedure LoadPopulationData in the Source Code section of this tutorial. Call this method in the OnLoad event handler of the form.

Compile to check for obvious errors. You may get a runtime error that the data file cannot be found. This is because our LoadPopulationData method assumes that the file resides in the same directory as the exe file. Copy "population.txt" into that folder, and compile again. Now the program should run fine, you will see an unspectacularly empty form, though.

Adding a data selector

The file contains various categories: total, male, or femal population, and male-to-female ratio. Why not display these data in our chart?

To prepare for this, let us add a panel to the form, align it to alTop, and delete its caption.

Add a combobox to this panel, set its Style to csDropdownList, add the strings "Total population", "Male population", "Female population", and "Ratio male/female" to the Items property, and set ItemIndex to 0.

Preparing TChart

Now we are finished with preparations, and it's time for charting...

Creating an area series

This time, we want to display our data as an area series. This type of series connects the data points as in a line series, but also fills the area underneath by a given color.

So, add a TAreaSeries to the chart. As usual, you don't see it at this time because it does not yet have any data. But we can prepare some properties:

UserDefined chart source

Now, how do we get our data to the area series?

The first idea might be iterate through our PopulationArray and copy the data to the series by means of AddXY calls, like this:

var
  i: Integer;
begin
  for i:=0 to High(PopulationArray) do
    Chart1AreaSeries1.AddXY(PopulationArray[i].Age, PopulationArray[i].Total);
end;

This disadvantage of this approach is that would have the data in memory twice: in the PopulationArray 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.

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;
    // ...
  end;

The fields X and Y are the coordinates of the data point.

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

procedure TForm1.UserDefinedChartSource1GetChartDataItem(
  ASource: TUserDefinedChartSource; AIndex: Integer; var AItem: TChartDataItem);
begin
  AItem.X := PopulationData[AIndex].Age;
  case Combobox1.ItemIndex of
    0: AItem.Y := PopulationData[AIndex].Total;
    1: AItem.Y := PopulationData[AIndex].Male;
    2: AItem.Y := PopulationData[AIndex].Female;
    3: AItem.Y := PopulationData[AIndex].Ratio;
  end;
end;

AIndex is the index of the datapoint which is queried. Since both chart source and our PopulationArray 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. Note that we pick the y coordinate according to what is selected in the combobox. In this way, the UserDefinedChartSource can give provide a multitude of data.

There are still some important things to do:

Compile.

Not too bad, some flaws, though.

<source> procedure TForm1.ComboBox1Select(Sender: TObject); begin

 Chart1.Invalidate;

end;