Page 1 of 1

Show/Hide individual Axis Custom Labels

Posted: Wed Oct 12, 2011 11:39 am
by 16558248
Hello, I'm having this type of problem with axis custom labels:
I have a TChart in which I dynamically create a set of TBarSeries, each one having a single bar value. I want to display in the bottom axis a string of text under each bar. I do this by adding custom labels to the chart BottomAxis:

Code: Select all

      for i:=0 to NSeries-1 do
         begin
            Series:=TBarSeries.Create(Chart);
            Series.AddXY(i,Values[i,0]);
            Chart.BottomAxis.Items.Add(i,SeriesTitles[i]);
            Series.ParentChart:=Chart;
       end;
This works. But then, I want to show/hide the above custom labels depending on which series are made visible by the user. I try do it in this way:

Code: Select all

   
with Chart do
      for i:=0 to SeriesCount-1 do     
         if (Pos('1',Series[i].Title) <> 0) then   // criteria that I'm using to decide wheter a series should be shown or not
            begin
               Series[i].Active:=true;
               BottomAxis.Items[i].Format.Visible:=true;
            end
         else
            begin
               Series[i].Active:=false;
               BottomAxis.Items[i].Format.Visible:=false;
            end;
Chart.Refresh;
but this doesn't work, because all custom labels previously defined are always displayed, independently if the corresponding Series is active or not.
I think there should be some other BottomAxis property to be set to get the desired result, but I cannot find which one niether from help nor from examples.
Thank you very much for your help.
Piero

Re: Show/Hide individual Axis Custom Labels

Posted: Fri Oct 14, 2011 8:15 am
by yeray
Hello Piero,

It seems that the Items.Format.Visible property isn't hiding the custom labels. I've added it to the wish list to be investigated for future releases (TV52015785).
In the meanwhile I'm afraid the only solution I can think is drawing the labels manually in the OnAfterDraw event, and control there if a label should be drawn or not.

Code: Select all

procedure TForm1.Chart1AfterDraw(Sender: TObject);
var i: Integer;
begin
  for i:=0 to Chart1.SeriesCount-1 do
    if Chart1[i].Active then //add your condition here
      Chart1.Canvas.TextOut(Chart1[i].CalcXPos(0), Chart1.Axes.Bottom.PosAxis+5, SeriesTitles[i]);
end;

Re: Show/Hide individual Axis Custom Labels

Posted: Fri Oct 14, 2011 2:13 pm
by 16558248
Hello Yeray,
your workaround is fine, I implemented it in my software and it does exactly what I desired!
Thank you very much for your help.

Piero

Re: Show/Hide individual Axis Custom Labels

Posted: Mon Oct 17, 2011 9:14 am
by yeray
Hello Piero,

I'm glad to hear that! :)