Page 1 of 1

Best Method for building minor logarithmic horizontal grid.

Posted: Fri Feb 18, 2005 10:45 pm
by 8124568
Hi TeeChart

I am nearing completion of my first chart app and I will need a minor logarithmic horizontal grid soon.

In my Feb 4, 2005 thread:
Major Grid and Minor Grid do not line up

Teechart stated
“At the moment the only workaround I can think is to manually draw vertical/horizontal gridlines directly on tChart.Graphics3D in tChart AfterDraw event.”

Would this be done with the ColorLine Tool or is there a better quicker way?

I know you just did a minor release and it is hard to say when the next release will be but is this bug fix on the horizon? Would it be best to wait?

Posted: Sat Feb 19, 2005 9:33 am
by Marjan
Hi.
ColorLine Tool
Well, you could use multiple color line tools, but I'd rather manually draw required minor gridlines directly on chart Graphics3D (in tChart AfterDraw event). Commented example (we'll also add it to TeeChart .NET demo):

Code: Select all

const int majorcount = 4;
private double[] majors = new double[majorcount];
private double[] minors = new double[2*majorcount-2];

/// <summary>
/// Calculate value decade.
/// </summary>
/// <param name="Value"></param>
/// <returns>the decade</returns>
private int GetDecade(double Value)
{
  int res = -1;
  while (Value >= 1.0)
  {
    Value /= 10.0;
    res ++;
  }
  return res;
}
    
/// <summary>
/// Generates specific logarithmic labels for axis.
/// </summary>
/// <param name="g">Graphics3D object</param>
/// <param name="ax">Axis</param>
/// <param name="majorlabels">array of logarithmic axis labels</param>
/// <param name="minorticks">array of minor ticks</param>
/// <param name="start">left/lower bound for minor tick</param>
/// <param name="end">right/upper bound for minor tick</param>
private void GenerateLogLabels(Steema.TeeChart.Drawing.Graphics3D g, Steema.TeeChart.Axis ax, double [] majorlabels, double [] minorticks, int start, int end)
{
  ax.Labels.Items.Clear();
  int mindecade = GetDecade(ax.Minimum);
  int maxdecade = GetDecade(ax.Maximum);
  double currval;
  int tickypos;

  start -= ax.MinorTicks.Length;
  end += ax.MinorTicks.Length;

  g.Pen.Style = ax.MinorGrid.Style;
  g.Pen.Color = ax.MinorGrid.Color;

   for (int i = mindecade ; i<=maxdecade; i++)
  {
    for (int j = 0; j<majorlabels.Length; j++)
    {
      currval =majorlabels[j]*Math.Pow(10,i) ;
      if (currval >= ax.Minimum && currval <= ax.Maximum)
        ax.Labels.Items.Add(currval);
    }
    for (int k = 0; k<minorticks.Length; k++)
    {
      currval =minorticks[k]*Math.Pow(10,i) ;
      if (currval >= ax.Minimum && currval <= ax.Maximum)
      {
        tickypos = ax.CalcPosValue(currval);
        if (ax.Horizontal)
          g.Line(tickypos,start,tickypos,end);
        else 
          g.Line(start,tickypos,end,tickypos);
      }
    }        
  }
}

public Form1()
{
  //
  InitializeComponent();

  // major ticks = labels
  majors[0] = 1;
  majors[1] = 2;
  majors[2] = 5;
  majors[3] = 10;

  // two minors between each major tick
  minors[0] = 1.2;
  minors[1] = 1.7;

  minors[2] = 3.0;
  minors[3] = 3.8;

  minors[4] = 5.5;
  minors[5] = 7.0;

  line1.FillSampleValues(100);
}


private void tChart2_BeforeDrawSeries(object sender, Steema.TeeChart.Drawing.Graphics3D g)
{
  GenerateLogLabels(g,line1.GetVertAxis,majors,minors,
        line1.GetHorizAxis.IStartPos,line1.GetHorizAxis.IEndPos);
      tChart2.Invalidate();
}