Recent

Author Topic: How to hide item(s) in legend in TAChart  (Read 2216 times)

chrv

  • Jr. Member
  • **
  • Posts: 68
How to hide item(s) in legend in TAChart
« on: March 06, 2023, 10:13:03 pm »
Hello everyone,

In a legend, is it possible to hide an item if the corresponding series is empty?

See attach to illustrate what I want to avoid

THANKS

Win32 Laz 2.0.12 FPC 3.2.0 AND Laz 3.4 FPC 3.2.2

wp

  • Hero Member
  • *****
  • Posts: 12470
Re: How to hide item(s) in legend in TAChart
« Reply #1 on: March 06, 2023, 11:49:02 pm »
You hide a series from the legend by setting the series' Legend.Visible to false (don't confuse it with the chart's Legend.Visible):
Code: Pascal  [Select][+][-]
  1.   Chart1LineSeries1.Legend.Visible := Chart1lineSeries1.Count > 0;

chrv

  • Jr. Member
  • **
  • Posts: 68
Re: How to hide item(s) in legend in TAChart
« Reply #2 on: March 11, 2023, 03:44:32 pm »
@wp

What i want to do  : hiding a legend item if there is no corresponding item in the chartArreaSeries.

In the following exemple, ther is no data with "Style 2" (yellow), do not show "Style 2" in the legend (i understend thant this is only possible at runtime)
Win32 Laz 2.0.12 FPC 3.2.0 AND Laz 3.4 FPC 3.2.2

wp

  • Hero Member
  • *****
  • Posts: 12470
Re: How to hide item(s) in legend in TAChart
« Reply #3 on: March 11, 2023, 07:23:08 pm »
I added an event OnAddStyleToLegend to the TChartStyles component in Laz/main. It has a boolean parameter in which you can decide whether this particular style level should be included in the legend:
Code: Pascal  [Select][+][-]
  1. type
  2.   TAddStyleToLegendEvent = procedure (AStyle: TChartStyle; ASeries: TObject;
  3.     var AddToLegend: Boolean) of object;
Note that, in order to avoid complicating the unit dependencies, the series which triggers the event is passed only as a TObject. Therefore, you must cast it to the series type needed.

Find a simple demo program in the attachment. Note also that it is your decision to define what you mean by "no data". In the demo, the empty values are set to NaN, as usual in TAChart. But in case of the stacked series you might use a zero value as well.

chrv

  • Jr. Member
  • **
  • Posts: 68
Re: How to hide item(s) in legend in TAChart
« Reply #4 on: March 12, 2023, 01:25:21 pm »
@wp
Everything seems to word as expected in TAreaSeries with this event handler.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.AddStyleToLegend(aStyle:TChartStyle;aSeries:TObject;var AddToLegend:boolean);
  2. var
  3.   aAreaSeries: TAreaSeries;
  4.   i, idx: Integer;
  5.   noData: Boolean;
  6. begin
  7.   if not (aSeries is TAreaSeries) then
  8.     exit;
  9.   aAreaSeries := TAreaSeries(aSeries);
  10.   idx := aStyle.Index;
  11.   noData := true;
  12.   for i:=0 to pred(aAreaSeries.Count) do
  13.     if not (aAreaSeries.YValues[i,idx]=0) then
  14.       begin
  15.         noData:=false;
  16.         break;
  17.       end;
  18.   AddToLegend:= not noData;
  19. end;        
I will try to adapt it in my production app.
Thank you very much.

Win32 Laz 2.0.12 FPC 3.2.0 AND Laz 3.4 FPC 3.2.2

chrv

  • Jr. Member
  • **
  • Posts: 68
Re: How to hide item(s) in legend in TAChart
« Reply #5 on: March 12, 2023, 02:01:30 pm »
@wp
It works perfectly within my app.

Thank you very very much
Win32 Laz 2.0.12 FPC 3.2.0 AND Laz 3.4 FPC 3.2.2

 

TinyPortal © 2005-2018