Recent

Author Topic: [SOLVED] How to completely remove series from a chart?  (Read 9567 times)

madepabloh

  • Full Member
  • ***
  • Posts: 160
[SOLVED] How to completely remove series from a chart?
« on: September 24, 2014, 12:12:18 am »
Long time ago i asked about to reset the series of a chart:
http://forum.lazarus.freepascal.org/index.php?topic=14374.0

Now, i would like to know how could i completely remove/delete all the series from a chart. Not only to delete the data from the series but to destroy the series created on runtime.

I tried this code i located somewhere in internet (with small changes to allow its compilation), without results:
Code: [Select]
procedure TMain.DeleteSeries(AChart: TChart);
var
MySeries: TBasicChartSeries;
i: integer;
begin
  with AChart do
    while AChart.SeriesCount > 0 do
      for i := AChart.SeriesCount-1 downto 0 do
        begin
          MySeries := AChart.Series.Items[i];
          RemoveSeries(AChart.Series.Items[i]);
          MySeries.Free;
        end;
end;

Thanks!
« Last Edit: September 24, 2014, 01:34:16 am by madepabloh »

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: How to completely remove series from a chart?
« Reply #1 on: September 24, 2014, 12:43:59 am »
Code: [Select]
  Chart1.ClearSeries;

This also deletes the series data if they are stored in the built-in listsource of the series. If you had put the data into a separate chartsource you'll have to delete them separately.

But be warned: if you had created the series at designtime the identifier "Chart1LineSeries1" still exists, but of course, accessing it raises an exception because the series does no longer exist.

Except for the unnecessary "while" (or: with the "while", but using always Items[0] without the "for"), what's wrong with your code? It should work, according to my experience.

madepabloh

  • Full Member
  • ***
  • Posts: 160
Re: How to completely remove series from a chart?
« Reply #2 on: September 24, 2014, 12:56:42 am »
I tried both
Quote
Char1t.Series.Clear;

Chart1.ClearSeries;

But it seems not work because i assigned tags, and names to each series when they were created on runtime. After to clear the series, if i try to plot the same data, it returns and error because the series names already exist... what i interpret this is because the series were not completely removed... am i right?

Edited:

Ok, it seems that what you say about the identifier could be the problem.

When i create the serie in runtime, i assign a name to the new serie:
Code: [Select]
ls.Name := 'S_'+ Table.Cells[i,3];
After to clear the data, if i load the same data to be plotted, the error returned say:
Quote
Duplicate name: a component named "Vcc" already exists
Vcc is thename of the first column in the stringgrid i try to plot...

This is why i assumed that it was caused because the serie was not deleted, but you say that the serie is etleted although the component name persist... How could i delete it to not to have conflict? Thanks!
« Last Edit: September 24, 2014, 01:27:48 am by madepabloh »

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: How to completely remove series from a chart?
« Reply #3 on: September 24, 2014, 01:25:13 am »
Yes, I can confirm now that there is no exception... Strange!

Anyway, to convince you that the series is really removed I wrote two short procedures, one adds a series, and the other one removes all series. Both procedures ask the chart for the current series count, and they also seek the form for series owned. Both methods return the same number. In addition you can compile this with the heaptrace option set - there is no memory leak when the program ends:

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);  // Add a series
var
  ser: TChartSeries;
  i, n: Integer;
begin
  ser := TLineSeries.Create(self);
  TLineSeries(ser).SeriesColor := rgb(random(256), random(256), random(256));
  for i:=0 to 10 do ser.AddXY(i, random);
  Chart1.AddSeries(ser);

  n := 0;
  for i:=0 to ComponentCount-1 do begin
    if Components[i] is TChartSeries then inc(n);
  end;
  ShowMessage(
    Format('The chart reports %d series.'#13'The form owns %d series.', [
    Chart1.SeriesCount, n]));
end;

procedure TForm1.Button2Click(Sender: TObject);  // remove all series
var
  i: Integer;
  n: Integer;
begin
  Chart1.ClearSeries;

  n := 0;
  for i:=0 to ComponentCount-1 do begin
    if Components[i] is TChartSeries then inc(n);
  end;
  ShowMessage(Format('The chart reports %d series.'#13'The form owns %d series.', [
    Chart1.SeriesCount, n]));

end;

BTW, when creating series (or any other components) at runtime I normally do not set their "Name" property to avoid that duplicate name issue. For accessing these components, I use their variable name instead (like "ser" in above example).

madepabloh

  • Full Member
  • ***
  • Posts: 160
Re: How to completely remove series from a chart?
« Reply #4 on: September 24, 2014, 01:33:59 am »
@wp, of course i believe you!! I was only describing what i saw...
In fact, i edited my previous post to confirm your idea that the name was the problem...

I edited the lines of my code were i setted the name to each serie... and now everything works perfect...
So, SOLVED.

Anyway, your code is so interesting, and i will usein some way to check forthe number of series in each plot. Thanks!!

 

TinyPortal © 2005-2018