Recent

Author Topic: Create and Destroy series on runtime  (Read 3509 times)

boreas

  • New Member
  • *
  • Posts: 33
Create and Destroy series on runtime
« on: January 03, 2017, 10:23:55 pm »
Hi everybody,
i use tchart and i create lineseries with source on runtime.
 
Code: Pascal  [Select][+][-]
  1.  form1.tls[form1.nbr]:=TLineSeries.Create(form1.Chart1);
  2.  form1.tls[form1.nbr].Title:=edit3.text;
  3.  form1.tls[form1.nbr].SeriesColor:=ColorBox1.Selected;
  4.  form1.src[form1.sbr]:=TListChartSource.Create(self);
  5.  form1.tls[form1.nbr].Source:=form1.src[form1.sbr];
  6.  form1.tls[form1.nbr].AddXY(0,0);
  7.  form1.Chart1.AddSeries( form1.tls[form1.nbr]) ;
  8. nbr := nbr+1;
  9. sbr := sbr+1;
  10.  

and  my code in onclose event of main form

Code: Pascal  [Select][+][-]
  1.   if nbr > 0 then begin
  2.     for i := nbr-1 downto 0 do  chart1.RemoveSeries(tls[i]);
  3.     for i := nbr-1 downto 0 do  chart1.DeleteSeries(tls[i]);
  4.     for i := nbr-1 downto 0 do src[i].free;
  5.     for i :=0 to  nbr-1 do begin
  6.       tls[i].free;
  7.       ShowMessage(inttostr(i));
  8.       end;
  9.  
But my application crash when i close the main form.
I always face on external SIGSEGV error. I coudnt solve that problem.

Thank you all for read and help

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Create and Destroy series on runtime
« Reply #1 on: January 03, 2017, 11:46:13 pm »
I assembled the attached demo from your fragments (it would have been better if you'd have posted a complete compilable project), and this runs fine...

So, I cannot present you exactly what is wrong. But I can give you some advice how to improve the code:

Your code is difficult to read because of the unnecessary usage of "form1" in front of almost every identifier. Beyond that, NEVER use the form's variable name inside the form's implementation - this will make your code work only if the instance of TForm1 is named form1. So, better replace your first code fragment with

Code: Pascal  [Select][+][-]
  1.   tls[nbr]:=TLineSeries.Create(Chart1);
  2.   tls[nbr].Title:=edit3.text;
  3.   tls[nbr].SeriesColor:=ColorBox1.Selected;
  4.   src[sbr]:=TListChartSource.Create(self);
  5.   tls[nbr].Source:=src[sbr];
  6.   tls[nbr].AddXY(0,0);
  7.   Chart1.AddSeries( tls[nbr]) ;
  8.   nbr := nbr+1;
  9.   sbr := sbr+1;

Anyway...

What is tls? An array of TLineSeries, probably? But how do you define the array length?
The same with src which seems to be an array of ListChartSource? Again, is the array properly defined? nbr and sbr must not run out of the array dimensions. You don't show any code preventing this. Also, after destruction of series and sources you should set the array elements to nil to signal that they do not exist any more, or in case of dynamic array you should set its length to zero.

As for the series, there is no need to store the line series in a separate array because the chart already takes care of that. Double storage is always bad because you have the burden to synchronize them. You can use Chart.Series[ i] to access the series contained in the chart. The type, however, is very general and you may have to cast it to the used series type in order to access all properties:

Code: Pascal  [Select][+][-]
  1. // Use dotted pens for all line series
  2.   for i:=0 to Chart1.SeriesCount-1 do
  3.     if Chart1.Series[i] is TLineSeries then
  4.       TLineSeries(Chart1.Series[i]).LinePen.Style := psDot;

BTW, if you call the destruction code at the end of your program you don't have to do anything at all. Since TChart, TLineSeries and TListChartSource are owned by the form (you did add an Owner to their constructors) there is no need to destroy them explicitely - the  form will do this automatically.

If you call it somewhere else, your code is a bit too complex: RemoveSeries and DestroySeries do the same, call only one of them. Or simply call series.Free. It will automatically remove the series from the chart's series list and then destroy it.
« Last Edit: January 04, 2017, 10:43:45 am by wp »

boreas

  • New Member
  • *
  • Posts: 33
Re: Create and Destroy series on runtime
« Reply #2 on: January 04, 2017, 08:25:15 am »
Thank you so much.
I will adapt the code according to your view.

 

TinyPortal © 2005-2018