Your chart already contains a Chart1BSplineSeries1 and a ListChartSource1 created at designtime - you see this in the object tree above the object inspector. But you create them a second time at runtime in the OnCreate event of the form. Normally this is no problem, but you make two mistakes:
- You give the runtime-created BSplineSeries and ListChartSource the same names as the already existing ones. This creates a memory leak because the already existing object no longer can be accessed and destroyed.
- You do not add the newly created series to the chart, you must call Chart.AddSeries. Therefore, the series does not know the chart to which it belongs, and this finally causes the crash you observe. Don't confuse this with the "owner" that you specify in the constructor. The "owner" is responsible for destroying an object when the program end. Therefore, it is not needed to explicitly destroy these manually created objects in FormDestroy.
This is the modification required your program to run:
procedure TForm1.FormCreate(Sender: TObject);
var
i : Integer;
begin
// Chart1BSplineSeries1 := TBSplineSeries.Create(Chart1); // already created at design-time!
// ListChartSource1 := TListChartSource.Create(Chart1);
for i := -200 to 1500 do
ListChartSource1.Add(i,4*i);
Chart1BSplineSeries1.Source := ListChartSource1;
end;
And, as mentioned, remove the FormDestroy method.
And this is the alternative code to correctly create series and listsource at runtime:
procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
begin
ListChartSource1 := TListChartSource.Create(self);
// better to use the form as "owner". Source can be shared between several charts this way!
for i := -200 to 1500 do
ListChartSource1.Add(i,4*i);
Chart1BSplineSeries1 := TBSplineSeries.Create(Chart1);
// set properties, e.g.:
Chart1BSplineSeries1.Title := 'B-Spline';
Chart1BSplineSeries1.Source := ListChartSource;
Chart1.AddSeries(Chart1BSplineSeries);
end;
But remove the design-time created series and source if you'd decide to go this way.