Recent

Author Topic: TAChart spline series: how to return Y value for a given X value?  (Read 13347 times)

wp

  • Hero Member
  • *****
  • Posts: 13424
Re: TAChart spline series: how to return Y value for a given X value?
« Reply #15 on: June 02, 2016, 02:46:42 pm »
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:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   i : Integer;
  4. begin
  5. //  Chart1BSplineSeries1 := TBSplineSeries.Create(Chart1);  // already created at design-time!
  6. //  ListChartSource1 := TListChartSource.Create(Chart1);
  7.   for i := -200 to 1500 do
  8.     ListChartSource1.Add(i,4*i);
  9.  
  10.   Chart1BSplineSeries1.Source := ListChartSource1;
  11. end;
And, as mentioned, remove the FormDestroy method.

And this is the alternative code to correctly create series and listsource at runtime:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   i: Integer;
  4. begin
  5.   ListChartSource1 := TListChartSource.Create(self);  
  6.   // better to use the form as "owner". Source can be shared between several charts this way!
  7.   for i := -200 to 1500 do
  8.     ListChartSource1.Add(i,4*i);
  9.  
  10.   Chart1BSplineSeries1 := TBSplineSeries.Create(Chart1);
  11.   // set properties, e.g.:
  12.   Chart1BSplineSeries1.Title := 'B-Spline';
  13.   Chart1BSplineSeries1.Source := ListChartSource;
  14.   Chart1.AddSeries(Chart1BSplineSeries);
  15. end;
But remove the design-time created series and source if you'd decide to go this way.
 
« Last Edit: June 06, 2016, 10:54:19 am by wp »

Relativity

  • Full Member
  • ***
  • Posts: 103
Re: TAChart spline series: how to return Y value for a given X value?
« Reply #16 on: June 06, 2016, 10:13:19 am »
I have implemented your second option, to correctly create series and listsource in runtime.
It perfectly works.

Thank you for your time.
You really know the ropes.
"How'm I gonna get through?"
  -- Pet Shop Boys

 

TinyPortal © 2005-2018