Exception in axis.MaxLabelsWidth()

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Post Reply
matthbri
Newbie
Newbie
Posts: 43
Joined: Wed Mar 22, 2006 12:00 am

Exception in axis.MaxLabelsWidth()

Post by matthbri » Fri Aug 11, 2006 5:20 am

Hi,

I am drawing multiple axes on one chart. They are organised horizontally, and the spacing between them I set according to MaxLabelsWidth so I can get the best possible fit.
The problem is It randomly gives me an exception. The exception occurs in System.Drawing.Graphics.MeasureString(...)
Having googled this, it seems to be a common problem and indicates a bug in MeasureString(). Do you have any suggestions to find the width I need for an axis?
The axes are using AutoMatic mode for some and preset for others.

thanks,

System.ArgumentException occurred
Message="Parameter is not valid."
Source="System.Drawing"
StackTrace:
at System.Drawing.Graphics.MeasureString(String text, Font font, SizeF layoutArea, StringFormat stringFormat)
at System.Drawing.Graphics.MeasureString(String text, Font font, SizeF layoutArea)
at System.Drawing.Graphics.MeasureString(String text, Font font)
at Steema.TeeChart.Drawing.Graphics3DGdiPlus.MeasureString(ChartFont f, String text)
at Steema.TeeChart.Drawing.Graphics3D.TextWidth(String text)
at Steema.TeeChart.Chart.MultiLineTextWidth(String s, Int32& numLines)
at Steema.TeeChart.AxisLabels.InternalLabelSize(Double value, Boolean isWidth)
at Steema.TeeChart.AxisLabels.LabelHeight(Double value)
at Steema.TeeChart.Axis.get_CalcIncrement()
at Steema.TeeChart.Axis.MaxLabelsValueWidth()
at Steema.TeeChart.Axis.MaxLabelsWidth()
at TE.TI.Trend.UI.TrendControl.SetAxisPlacement(Boolean fUseGraphics) in E:\workspaces\trend\tipsinfoviewer\src\TE.TI.Trend.UI\TrendControl.cs:line 1048

Christopher
Site Admin
Site Admin
Posts: 1349
Joined: Thu Jan 01, 1970 12:00 am
Location: Riudellots de la Selva, Catalonia
Contact:

Post by Christopher » Fri Aug 11, 2006 7:09 am

Hello,

Well, googling also led me to a number of alternatives to MeasureString which you could probably implement in the GetAxisLabels event to calculate the MaxLabelsWidth manually.
Thank you!

Christopher Ireland (Steema crew)
Please be aware of the newsgroup archives:
http://www.teechart.net/support/search.php
http://groups.google.com
http://codenewsfast.com/

Christopher
Site Admin
Site Admin
Posts: 1349
Joined: Thu Jan 01, 1970 12:00 am
Location: Riudellots de la Selva, Catalonia
Contact:

Post by Christopher » Fri Aug 11, 2006 8:10 am

Hello,

More on this. Just wanted to thank you for pointing this one out to me as removing the depency on Graphics.MeasureString from the TeeChart method Graphics3DGdiPlus.MeasureString has resolved a couple of long standing issue's I've had.

Our code for MeasureString (available in the next maintenance release) now looks like this:

Code: Select all

		public override SizeF MeasureString(ChartFont f,string text)
		{
			if (g==null)
			{
				//MM Alternative approach required for Win98
				Bitmap b=new Bitmap(1,1);
				g=Graphics.FromImage(b);
			}
      return MeasureDisplayStringWidth(text, f.DrawingFont);
		}

    public SizeF MeasureDisplayStringWidth(string text, Font font)
    {
      System.Drawing.StringFormat format = new System.Drawing.StringFormat();
      System.Drawing.RectangleF rect = new System.Drawing.RectangleF(0, 0,
                                                                    1000, 1000);
      System.Drawing.CharacterRange[] ranges = 
                                       { new System.Drawing.CharacterRange(0, 
                                                               text.Length) };
      System.Drawing.Region[] regions = new System.Drawing.Region[1];

      format.SetMeasurableCharacterRanges(ranges);

      regions = g.MeasureCharacterRanges(text, font, rect, format);
      rect = regions[0].GetBounds(g);
      rect.Inflate(1, 1);

      return rect.Size;
    }
Thank you!

Christopher Ireland (Steema crew)
Please be aware of the newsgroup archives:
http://www.teechart.net/support/search.php
http://groups.google.com
http://codenewsfast.com/

matthbri
Newbie
Newbie
Posts: 43
Joined: Wed Mar 22, 2006 12:00 am

Post by matthbri » Fri Aug 11, 2006 12:36 pm

Ok thanks. If the next release is in September, that is OK for me.

Christopher
Site Admin
Site Admin
Posts: 1349
Joined: Thu Jan 01, 1970 12:00 am
Location: Riudellots de la Selva, Catalonia
Contact:

Post by Christopher » Fri Aug 11, 2006 2:02 pm

Hello,

You don't have to wait until then if you don't want to. You can override the Graphics3DGdiPlus class and override the MeasureString method using the code I mentioned below. Then all you have to do is to set an instance of this new class to tChart.Graphics3D and do a tChart.Invalidate.
Thank you!

Christopher Ireland (Steema crew)
Please be aware of the newsgroup archives:
http://www.teechart.net/support/search.php
http://groups.google.com
http://codenewsfast.com/

Post Reply