Page 1 of 1

Custom Axis Labelling

Posted: Fri Feb 06, 2004 4:06 pm
by 8123414
Hi,

I have a chart which is configured at runtime depending on the parameters chosen by the user. The values displayed can range from single digits into thousands. For example, for one series, values could range from 0 to 10, and for another from 0 to 10,000. This means that the axis labels vary significantly in width. When the axis labels are quite long it pushes the axis caption off the viewable space. I have to remedy this by setting the left margin of the chart panel. Is there anyway to adjust the left margin based on the (max) width of the axis labels? I could set the left margin to allow for the longest likely length but I'd prefer to make full use of the space available if possible. I'm using custom axis and the axis captions are already at 90 degrees.

Thanks,

Paul[/code]

Posted: Mon Feb 09, 2004 6:16 pm
by Pep
Hi Paul,

you can use code similar to the following to calculate axis width and then use the value to assign the left margin :

Code: Select all


Steema.TeeChart.Axis custom1 = new Steema.TeeChart.Axis(false, false, tChart1.Chart); 
      
      tChart1.Aspect.View3D = false; 
      tChart1.Axes.Custom.Add(custom1); 
      custom1.RelativePosition = 50; 
      line1.CustomVertAxis = custom1; 
      line1.FillSampleValues(150); 

      Bitmap bmp = tChart1.Bitmap; 

      int axisWidth = custom1.AxisPen.Width + custom1.Ticks.Length + custom1.MaxLabelsWidth(); 
      tChart1.Header.Text = axisWidth.ToString(); 
 

Posted: Tue Feb 10, 2004 4:15 pm
by 8123414
Hi Pep,

Thanks for the reply. Unfortunately, the formula you gave does seem to work too well. The MaxLabelsWidth does not seem to give back the correct results. I plotted a graph with values ranging from 100 t0 1900 and MaxLabelsWidth returned 12. Then when I formatted the labels e.g. .Labels.ValueFormat = "0.00" it returned 27. I could not match either value with a corresponding area of the chart after examining the plot in an image editor.

Paul

Posted: Wed Feb 11, 2004 4:14 pm
by Chris
Hi Paul,

The problem you mention only seems to exist for custom axes -- for the default axes the behaviour is automatic, e.g.

Code: Select all

private void Form1_Load(object sender, System.EventArgs e) {
      Random rnd = new Random();
      double xVal, yVal;
      for(int i=0; i<10; ++i) {
        xVal = (double)i;
        yVal = rnd.Next(100);
        line1.Add(xVal, yVal);
      }

      tChart1.Axes.Left.Title.Caption = "Left Axis Caption";
      tChart1.Axes.Left.Title.Angle = 90;
    }


   private void button1_Click(object sender, System.EventArgs e) {
      Random rnd = new Random();
      double xVal, yVal;

      line1.XValues.Clear();
      line1.YValues.Clear();
      for(int i=0; i<10; ++i) {
        xVal = (double)i;
        yVal = rnd.Next(10000000);
        line1.Add(xVal, yVal);
      }
    }
Using custom axes, the following code seems to help the situation (MaxLabelsWidth works OK here):

Code: Select all

   private int labelWidth;

    private void Form1_Load(object sender, System.EventArgs e) {
      Random rnd = new Random();
      double xVal, yVal;
      for(int i=0; i<10; ++i) {
        xVal = (double)i;
        yVal = rnd.Next(100);
        line1.Add(xVal, yVal);
      }
      
      line1.CustomVertAxis = axis1;
      axis1.Title.Caption = "Left Axis Caption";
      axis1.Title.Angle = 90;

      tChart1.Panel.MarginLeft = 10;
      labelWidth = axis1.MaxLabelsWidth();
      label1.Text = labelWidth.ToString();
    }


    private void button1_Click(object sender, System.EventArgs e) {
      Random rnd = new Random();
      double xVal, yVal;

      line1.XValues.Clear();
      line1.YValues.Clear();
      for(int i=0; i<10; ++i) {
        xVal = (double)i;
        yVal = rnd.Next(10000000);
        line1.Add(xVal, yVal);
      }

      int newLabelWidth = axis1.MaxLabelsWidth();
      if(newLabelWidth > labelWidth) {
        int labelWidthDifference = newLabelWidth / labelWidth;
        tChart1.Panel.MarginLeft = tChart1.Panel.MarginLeft * labelWidthDifference;
      }
    }

Posted: Fri Feb 13, 2004 11:29 am
by 8123414
Hi Chris,

Thanks for the reply. I'll give it a go.