Forum > TAChart

How to hide item(s) in legend in TAChart

(1/2) > >>

chrv:
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

wp:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  Chart1LineSeries1.Legend.Visible := Chart1lineSeries1.Count > 0;

chrv:
@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)

wp:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  TAddStyleToLegendEvent = procedure (AStyle: TChartStyle; ASeries: TObject;    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:
@wp
Everything seems to word as expected in TAreaSeries with this event handler.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.AddStyleToLegend(aStyle:TChartStyle;aSeries:TObject;var AddToLegend:boolean);var  aAreaSeries: TAreaSeries;  i, idx: Integer;  noData: Boolean;begin  if not (aSeries is TAreaSeries) then    exit;  aAreaSeries := TAreaSeries(aSeries);  idx := aStyle.Index;  noData := true;  for i:=0 to pred(aAreaSeries.Count) do    if not (aAreaSeries.YValues[i,idx]=0) then      begin        noData:=false;        break;      end;  AddToLegend:= not noData;end;        I will try to adapt it in my production app.
Thank you very much.

Navigation

[0] Message Index

[#] Next page

Go to full version