Page 1 of 1

Offset axis labels

Posted: Tue Sep 05, 2006 11:00 pm
by 8128589
I'm trying to draw the bottom axis labels offset a few pixels from their normal position. I thought I might be able to set the axis text to transparent, then in GetAxisLabel read the location and labelText and draw it a few pixels over.

However ValueIndex is not valid unless the the label style is set to Text (I need it set to Value).

Any way to get ValueIndex valid? Or alternate ways to offset the axis labels? This is a real-time graph and the x-axis scale may change often.

Posted: Wed Sep 06, 2006 1:17 pm
by narcis
Hi Gp,

To be able to change axes labels position you should use custom labels as told on this message. Could you please have a look at it?

Posted: Wed Sep 06, 2006 5:15 pm
by 8128589
I think custom labels will work for me, but I need to find a way to avoid label overlap. This code:

Code: Select all

		private void tChart1_AfterDraw(object sender, Steema.TeeChart.Drawing.Graphics3D g)
		{ 			
			tChart1.Header.Text="";

			Steema.TeeChart.AxisLabelItem CurrentLabel, PreviousLabel;	
			for (int i=1; i<tChart1.Axes.Left.Labels.Items.Count; ++i)
			{ 
				CurrentLabel = tChart1.Axes.Left.Labels.Items[i];
				PreviousLabel = tChart1.Axes.Left.Labels.Items[i-1];

				tChart1.Header.Text=tChart1.Header.Text+" "+CurrentLabel.Top.ToString();

				if (CurrentLabel.Top+CurrentLabel.Height == PreviousLabel.Top)
				{
					CurrentLabel.Visible=false;
				}
			}
		}
Doesn't work - All the coordinates for the label, Right, Left, Top, size, etc.. are 0. How can I determine the location and size of a custom label?

Posted: Wed Sep 06, 2006 9:55 pm
by 8128589
Well I kinda sorta answered my own question. This code works for me, I assume a certain label width but for my needs that is OK.

Code: Select all

      private void tChart1_AfterDraw(object sender, Steema.TeeChart.Drawing.Graphics3D g)
      {
         Steema.TeeChart.AxisLabelItem CurrentLabel;
         int labelWidth = 40;
         int labelCount = (int)(tChart1.Axes.Bottom.Maximum - tChart1.Axes.Bottom.Minimum);
         int chartWidth = tChart1.Size.Width;
         int canShow = chartWidth / labelWidth;
         int showEvery = labelCount / canShow;

         if ( showEvery < 1 ) showEvery = 1;
        
         for (int i = 0; i < labelCount; ++i) 
         { 
            CurrentLabel = tChart1.Axes.Bottom.Labels.Items[i]; 

            if ( i % showEvery == 0 ) 
            { 
               CurrentLabel.Visible = true; 
            } 
            else
            {
               CurrentLabel.Visible = false;
            }
         }       
      }

Posted: Wed Sep 06, 2006 9:58 pm
by 8128589
However, I have one additional issue with my particular graph. It has a black background, and while the grid lines show up in the correct location the labels themselves do not appear.

This works fine in a small test project, any ideas why the label text would not be shown? The labels displayed when I was not using custom labels.

Posted: Thu Sep 07, 2006 8:51 am
by narcis
Hi Gp,

Could it be that the labels are in black font and this makes the labels not visible with black background?

Posted: Thu Sep 07, 2006 4:12 pm
by 8128589
Yes, I found that the custom label color has to be set explicitly when one is added, if you want something other than black.

Code: Select all

Steema.TeeChart.AxisLabelItem item = tChart1.Axes.Bottom.Labels.Items.Add(i + (width / 2), i.ToString());
item.Font.Color = tChart1.Axes.Bottom.Labels.Color;
Also, to work in my application I had to enable/disable the labels in BeforeDrawAxis, rather than AfterDraw. Here is the final solution:

Code: Select all

      private void tChart1_BeforeDrawAxes(object sender, Steema.TeeChart.Drawing.Graphics3D g)
      {
         Steema.TeeChart.AxisLabelItem CurrentLabel;
         int labelWidth = 40; // Hardcoded width of label text.

         // The count is how many of our labels fit within the range of the graph.
         int labelCount = (int)(tChart1.Axes.Bottom.Maximum - tChart1.Axes.Bottom.Minimum);

         int chartWidth = tChart1.Size.Width;
         int canShow = chartWidth / labelWidth;
         int showEvery = labelCount / canShow;

         if ( showEvery < 1 ) showEvery = 1;
        
         for (int i = 0; i < tChart1.Axes.Bottom.Labels.Items.Count; ++i) 
         { 
            CurrentLabel = tChart1.Axes.Bottom.Labels.Items[i]; 

            if ( i % showEvery == 0 ) 
            { 
               CurrentLabel.Visible = true;
            } 
            else
            {
               CurrentLabel.Visible = false;
            }
         }         
      }
Thanks for the tips!