Displaying Data in a Chart

These demonstrations are for the TAChart in Lazarus. See the well-documented comparison with the TeeChart in Delphi XE2 and later. If you only want to draw a bar chart with horizontal bars then the (deprecated) TBarChart is easy to use and has a straightforward sample with a few lines of code.

The TChart (the first of many items within the Chart component tag) is versatile and complicated. You can access specialised property editors for the chart and its sub-components from the Project Inspector. There is a main page of documentation plus many tutorial sections such as the BarSeries and a good assortment of supplied demonstrations (in the components\tachart\demo folder) such as basic, labels and db.

In some demonstrations you need to look at the properties in the Project Inspector (or the XML view of the form) in conjunction with the Pascal code to appreciate how the chart is working. We decided to add only a TChart to the form and to change all properties by Pascal code so that it would be easier for you to comprehend. We advise you to:
  • start each project by getting a minimal chart showing (such as a single bar) before introducing the bulk of the code;
  • test frequently so that the output matches your expectations;
  • experiment.

Bar Chart

The code for the bar chart demo follows a screenshot of the output.

Output from program BarChartDemo

Output from program BarChartDemo

unit uBarChartDemo;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, TAGraph, TASeries, TAChartUtils, Forms,
  Controls, Graphics, Dialogs;

type
  TForm1 = class(TForm)
    Chart1: TChart;
    Bars : TBarSeries;
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  Values: array[1..10] of double = (17, 9, 6, 5, 4, 3, 3, 2, 2, 2);
  Colours: array[1..10] of TColor = (clRed, clYellow, clFuchsia, clGreen, clBlue,
                                     clMaroon, clOlive, clBlack, clGray, clAqua);
  Labels: array[1..10] of string = ('UK', 'US','Germany', 'India', 'Brazil', 'Indonesia',
                                    'Russia', 'Netherlands', 'Italy', 'France');
  Count: integer;
begin
  Form1.Caption := 'Bar Chart Demo';
  Bars := TBarSeries.Create(Chart1);
  Chart1.Title.Text.Strings[0] := 'Percentage of Sessions on PPS (Sept 2014)';
  Chart1.Title.Font.Size := 16;;
  Chart1.Title.Visible := true;
  Bars.Marks.Style := TSeriesMarksStyle(smsValue); //or smsLabelValue
  Bars.Marks.Distance:= 5;
  for Count := 1 to 10 do
    Bars.AddXY(count, Values[count], Labels[Count], Colours[Count]);
  Chart1.AddSeries(Bars);
  Chart1.AxisList.BottomAxis.Marks.Source := Bars.Source;
  Chart1.AxisList.BottomAxis.Marks.Style := smsLabel;
end;

end.

Pie Chart

The code for the pie chart demo follows a screenshot of the output.

Output from program PieChartDemo

Output from program PieChartDemo

unit uPieChartDemo;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, TAGraph, TASeries, TAChartUtils, Forms,
  Controls, Graphics, Dialogs;

type

  TForm1 = class(TForm)
    Chart1: TChart;
    Slices : TPieSeries;
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  Values: array[1..5] of double = (52, 20, 20, 5, 2);
  Colours: array[1..5] of TColor = (clRed, clYellow, clFuchsia, clGreen, clBlue);
  Labels: array[1..5] of string = ('Europe', 'Americas', 'Asia', 'Africa', 'Oceania');
  Count: integer;
begin
  Form1.Caption := 'Pie Chart Demo';
  Slices := TPieSeries.Create(Chart1);
  Chart1.Title.Text.Strings[0] := 'Percentage of Sessions on PPS (Sept 2014)';
  Chart1.Title.Font.Size := 16;;
  Chart1.Title.Visible := true;
  Slices.Marks.Style := TSeriesMarksStyle(smsLabelValue);
  Slices.Marks.Distance:= 5;
  for Count := 1 to 5 do
    Slices.AddPie(Values[count], Labels[Count], Colours[Count]);
  Chart1.AddSeries(Slices);
  Chart1.AxisVisible := false;
end;

end.

Programming - a skill for life!

Using widgets (such as list boxes, combo boxes, string grids, DBgrids, charts and maps) and drawing on the canvas