Offset axis labels

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Post Reply
Gp
Newbie
Newbie
Posts: 43
Joined: Thu Jan 13, 2005 5:00 am

Offset axis labels

Post by Gp » Tue Sep 05, 2006 11:00 pm

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.

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Post by Narcís » Wed Sep 06, 2006 1:17 pm

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?
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Gp
Newbie
Newbie
Posts: 43
Joined: Thu Jan 13, 2005 5:00 am

Post by Gp » Wed Sep 06, 2006 5:15 pm

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?

Gp
Newbie
Newbie
Posts: 43
Joined: Thu Jan 13, 2005 5:00 am

Post by Gp » Wed Sep 06, 2006 9:55 pm

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;
            }
         }       
      }

Gp
Newbie
Newbie
Posts: 43
Joined: Thu Jan 13, 2005 5:00 am

Post by Gp » Wed Sep 06, 2006 9:58 pm

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.

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Post by Narcís » Thu Sep 07, 2006 8:51 am

Hi Gp,

Could it be that the labels are in black font and this makes the labels not visible with black background?
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Gp
Newbie
Newbie
Posts: 43
Joined: Thu Jan 13, 2005 5:00 am

Post by Gp » Thu Sep 07, 2006 4:12 pm

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!

Post Reply