TAChart Tutorial: Dual y axis, Legend

From Lazarus wiki
Revision as of 16:15, 5 September 2012 by Wp (talk | contribs) (Initial first part)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Introduction

When different quantities are plotted into the same chart it happens quite often that they cover a largely different range. In an "ordinary" chart, the series with the large values dominates over the series with the small values which is compressed to a flat line. The chart would be much more meaningful if different axes could be used, one for the large values, and one for the small values.

When you worked through the tutorial on user-defined chartsource you will have come across such a case. In that tutorial we created a diagram of the world population as a function of age. There was also an option to draw the ratio of male-to-female population. This latter number is considerably smaller than the population count. So, if both data were combined in the same chart, the population ratio would shrink to a horizontal line.

This is the background for today's project. We will go to the population data again and draw population count and the male-to-female ratio in the same chart.

You will learn

  • how to create a chart with two y axes
  • how to use user-defined axis labels
  • how to fine-tune the legend.

As usual, this is our disclaimer: You should be familiar with Lazarus and FPC, and you should have a basic understanding of the TAChart library (go through Getting Started tutorial, if you don't). In this particular tutorial, it would also be helpful if you have studied the tutorial on user-defined chartsource.

Data

As already mentioned we will be using the same data as in the user-defined chartsource tutorial. The primary data file is called "population.txt" and orginates from http://www.census.gov www.census.gov. Unit population.pas reads this file and stores the data in an array PopulationData of TPopulationRecords:

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

  TPopulationArray = array of TPopulationRecord;

Preparation

Now, we have all that we need to start a new project. Add the unit population.pas to the form's uses list. Add a variable PopulationData of type TPopulationRecord to the private section of the form. And read the data file by calling the procedure LoadPopulationData (in the population unit) from the form's OnCreate event handler:

uses
  ..., population;

type
  TForm1 = class(TForm)
  // ...
  private
    PopulationData : TPopulationArray;
  // ...
  end;

const
  POPULATION_FILE = 'population.txt';

procedure TForm1.FormCreate(Sender: TObject);
begin
  LoadPopulationData(POPULATION_FILE, PopulationData); 
end;

Now be can begin charting...

Add a TChart component to the form:

  • Align it as alClient.
  • Set its BackColor to clWhite.
  • Use the text "World population" as the chart's title.
  • Mention our reference for the data in the footer - we don't want to steel data...

Add three line series to the chart:

  • To be more descriptive, change their names to LineSeries_male, LineSeries_female, and LineSeries_ratio.
  • Set the SeriesColor of the male series to a "boyish" clSkyBlue, that of the female series to a "girlish" $00FF80FF, and leave the color of the ratio series at black.

Where do the series get their data from? Our data are stored in the PopulationData array, so it would be best to used a user-defined chart source. To be exact: we need three chart sources, one for each series.

Therefore, let's add three TUserDefinedChartSources to the form:

  • Rename them ChartSource_male, ChartSource_female, and ChartSource_ratio.