Recent

Author Topic: Notification when adding a new series to a TChart  (Read 6340 times)

wp

  • Hero Member
  • *****
  • Posts: 13502
Notification when adding a new series to a TChart
« on: July 05, 2011, 06:39:48 pm »
As discussed in another thread I am writing a TChartListbox for being able to show/hide series of a TChart. In order to get notified for changes in the series I added a TListener to the component.

Code: [Select]
  FListener := TListener.Create(@FChart, @SeriesChanged);

"SeriesChanged" presently just calls "Populate" which reads all series into the listbox:

Code: [Select]
procedure TChartListbox.Populate;
var
  i : integer;
begin
  try
    FreeAndNil(FLegendItems);
    Items.BeginUpdate;
    Items.Clear;
    if (FChart <> nil) then begin
      FLegendItems := GetLegendItems;
      for i:=0 to FLegendItems.Count-1 do begin
        Items.AddObject('', FLegendItems[i]);
        Checked[i] := TCustomChartSeries(FLegendItems[i].Owner).Active;
      end;
    end;
  finally
    Items.EndUpdate;
  end;
end

There is a button on the form which adds a new series with random data to the chart.

Code: [Select]
procedure TForm1.Button1Click(Sender:TObject);
var
  ser : TLineSeries;
begin
  RandomChartSource.RandSeed := random(65000);
  ser := TLineSeries.Create(Chart);
  ser.Source := RandomChartSource;
  ser.SeriesColor := rgbToColor(random(255), random(256), random(256));
  ser.Title := Format('Series %d', [Chart.SeriesCount]);
  Chart.AddSeries(ser);
end;

The new series appeas in the chart immediately. In the listbox, however, it appears only after clicking the button for a second time. There must be something in the listening/broadcasting mechanism which I do not understand.

What am I doing wrong?


Ask

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 687
Re: Notification when adding a new series to a TChart
« Reply #1 on: July 05, 2011, 07:09:45 pm »
Try r31568.

The Broadcaster will react on any visible change to a chart, from zooming to adding a point.
Unfortunately, there are quite a few potential events that may change legend appearance --
such as changing SeriesColor etc., so filtering relevant events seems impossible to me.

wp

  • Hero Member
  • *****
  • Posts: 13502
Re: Notification when adding a new series to a TChart
« Reply #2 on: July 06, 2011, 11:03:55 am »
Yes, now the listbox reacts immediately.

Does the broadcaster transmit supplementary information on what has been changed in the chart? I try to avoid building the listbox items each time data were added to a series by means of AddXY.

Ask

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 687
Re: Notification when adding a new series to a TChart
« Reply #3 on: July 06, 2011, 12:03:10 pm »
Notification event receives an argument (Sender), containing the object which initiated the change.
In particular, adding points results in Sender being equal to the corresponding chart source.

However, I am not sure there is a robust way to distinguish which changes
will accept the legend look -- for example, somebody can write a legend item
which encodes the size of the series.

 

TinyPortal © 2005-2018