Page 1 of 1

Exception in axis.MaxLabelsWidth()

Posted: Fri Aug 11, 2006 5:20 am
by 9790735
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

Posted: Fri Aug 11, 2006 7:09 am
by Chris
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.

Posted: Fri Aug 11, 2006 8:10 am
by Chris
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;
    }

Posted: Fri Aug 11, 2006 12:36 pm
by 9790735
Ok thanks. If the next release is in September, that is OK for me.

Posted: Fri Aug 11, 2006 2:02 pm
by Chris
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.