Search code examples
c#teechart

Set static space between custom axes


I have to have static space (lets say 20 px) between axes.
If I have more axes on the left, it is possible to set their StartPosition and EndPosition in pixels or in percents.

  1. For pixels setting - is there some way, how I can get the height of the space, where are the axes (red line in the picture)?
  2. For percents setting - is there some way, how I can automatically convert 20 px to percents? I could calculate that by myself if I know the height, but I don't know how to get it - see 1.

highlighted axes space

I am able to get the height of the whole panel, where the chart is, but I don't know where to get the actual height of the space for left axes.


Solution

  • tChart1.Chart.ChartRect gives you the Rectangle of the "drawing zone". In your case, where you have to get that size before the axes calculations, you can use GetAxesChartRect event and use e.AxesChartRect as follows:

    Chart

    private void testChartRect()
    {
      for (int i = 0; i < 4; i++)
      {
        Line line = new Line(tChart1.Chart);
        tChart1.Series.Add(line);
        line.Chart = tChart1.Chart;
        line.FillSampleValues();
    
        Axis axis = new Axis();
        tChart1.Axes.Custom.Add(axis);
        line.CustomVertAxis = axis;
        axis.AxisPen.Color = line.Color;
        axis.Labels.Font.Color = line.Color;
      }
    
      tChart1.Aspect.View3D = false;
      tChart1.Panel.MarginLeft = 10;
    
      //tChart1.AfterDraw += TChart1_AfterDraw1;
      tChart1.GetAxesChartRect += TChart1_GetAxesChartRect;
    }
    
    private void TChart1_GetAxesChartRect(object sender, GetAxesChartRectEventArgs e)
    {
      Rectangle chartRect = e.AxesChartRect;
    
      int axisLength = (chartRect.Bottom - chartRect.Top) / tChart1.Axes.Custom.Count;
      int margin = 20;
      for (int i = 0; i < tChart1.Axes.Custom.Count; i++)
      {
        Axis axis = tChart1.Axes.Custom[i];
        axis.StartEndPositionUnits = PositionUnits.Pixels;
        axis.StartPosition = i * axisLength;
        axis.EndPosition = (i + 1) * axisLength - (i != (tChart1.Axes.Custom.Count - 1) ? margin : 0);
      }
    }
    
    private void TChart1_AfterDraw1(object sender, Graphics3D g)
    {
      tChart1.Graphics3D.Brush.Color = Color.Red;
      tChart1.Graphics3D.Brush.Transparency = 80;
      tChart1.Graphics3D.Rectangle(tChart1.Chart.ChartRect);
    }