Page 1 of 1

Create a teechart dynamically

Posted: Thu Sep 25, 2008 10:14 am
by 9350489
Hi Narcis,

Why this code doesn't functions ?

Code: Select all

procedure TForm1.Button1Click(Sender: TObject);
var
  Chart1: TChart;
begin
  try
    Chart1 := TChart.Create(self);
    Chart1.Top := 0;
    Chart1.Left := 0;
    Chart1.Height := 300;
    Chart1.Width := 500;
    Chart1.RemoveAllSeries;
    Chart1.AddSeries(TBarSeries.Create(Self));
    Chart1[0].Clear;
    Chart1[0].Add(123, 'ABC', clRed);
    Chart1[0].Add(456, 'DEF', clBlue);
    Chart1[0].Add(321, 'GHI', clGreen);
    Chart1[0].Marks.Style := smsValue;
  finally
    Chart1.Free;
  end;
end;

Thank you for your help

Didou

Posted: Thu Sep 25, 2008 10:39 am
by narcis
Hi Didou,

This is because you need to assign chart's Parent control as shown in code below. Also, freeing the created chart destroys it so you should comment this line.

Code: Select all

uses Chart, TeEngine, Series;

procedure TForm1.Button1Click(Sender: TObject);
var
  Chart1: TChart;
begin
  try
    Chart1 := TChart.Create(self);
    Chart1.Parent := self;
    Chart1.Top := 0;
    Chart1.Left := 0;
    Chart1.Height := 300;
    Chart1.Width := 500;
    Chart1.RemoveAllSeries;
    Chart1.AddSeries(TBarSeries.Create(Self));
    Chart1[0].Clear;
    Chart1[0].Add(123, 'ABC', clRed);
    Chart1[0].Add(456, 'DEF', clBlue);
    Chart1[0].Add(321, 'GHI', clGreen);
    Chart1[0].Marks.Style := smsValue;
  finally
    //Chart1.Free;
  end;
end;

Posted: Thu Sep 25, 2008 12:29 pm
by 9350489
And yet if you place a ShowMessage (Self.name)
we get well "Form1".

Why is it necessary to indicate the parent in this case?

Thank you for your reply fast

Didou

Posted: Mon Sep 29, 2008 10:51 am
by narcis
Hi Didou,

TChart derives from TCustomPanel, which is derived from TControl. If you have a look at TControl's Parent property in Delphi help files you'll find some information about this.