How To…Create a Chart in Code

Question

“How do I create a TeeChart inside my report completely in code?”

Solution

Begin by creating a TppTeeChart object and adding it to the report.  Then leverage the TppTeeChart.Chart property to add Series objects to the chart.

The key to being successfull at creating charts in code is to include the proper TeeChart units in your uses clause.  A nice trick to determining the correct units to include is to create a similar chart in Delphi using the TChart component(s) on a form, then running your application.  This will automatically add any units needed to the uses clause and give you an idea which units need to be added for your own reporting application.

Download: CreateChartInCode.zip

Sample Delphi code: 

procedure TForm1.CreateChart;
begin
  FChart := TppTeeChart.Create(self);
  FPieSeries := TPieSeries.Create(Self);

  FChart.Band := ppDetailBand1;
  FChart.UserName := 'MyChart';
  FChart.Width := 4;
  FChart.Height := 2.5;
  FChart.Chart.Title.Text.Text := 'MyChart';

  FPieSeries.ParentChart := FChart.Chart;
  FPieSeries.Clear;
  FPieSeries.Add( 30, 'Dynastar', clYellow );
  FPieSeries.Add( 45, 'Salomon', clBlue );
  FPieSeries.Add( 10, 'K2', clGreen );
  FPieSeries.Add( 15, 'Rossignol', clRed);

end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FPieSeries.Free;
  FChart.Free;
end;