Align only the origin of primary and secondary Y-Axes


https://www.nevron.com/Forum/Topic13224.aspx
Print Topic | Close Window

By Alexander Haberl - 3 Years Ago
Hi!
Is it possible to align only the origin of a primary and a secondary NLinearAxis? The black lineseries is projected onto the right (secondary) y-axis, but its origin (value 0) should be aligned with the origin (value 0) of the left y-axis. If I add the right axis as a slave to the left axis, the whole axis is aligned, but then the bars are too small...
Thanks!

https://www.nevron.com/forum/uploads/images/e272c194-2fa5-4d9a-8c15-3a55.png
By Nevron Support - 3 Years Ago
Hi Alexander,

Currently, there is no build-in functionality to align the axes in this way, but you can use custom code to achieve this. The following code shows how to align two axes relative to a factor and value. The factor specifies how the value should be positioned relative to the axis range:

  private void Form1_Load(object sender, EventArgs e)
   {
    NChart chart = (NChart)nChartControl1.Charts[0];
    chart.Axis(StandardAxis.SecondaryY).Visible = true;

    NBarSeries bar = new NBarSeries();

    bar.Values.Add(10);
    bar.Values.Add(20);
    bar.Values.Add(-10);

    chart.Series.Add(bar);

    NLineSeries line = new NLineSeries();
    line.Values.Add(100);
    line.Values.Add(200);
    line.Values.Add(-400);
    chart.Series.Add(line);

    line.DisplayOnAxis(StandardAxis.PrimaryY, false);
    line.DisplayOnAxis(StandardAxis.SecondaryY, true);

    AlignAxes();
   }

   void AlignAxes()
   {
    NChart chart = nChartControl1.Charts[0];

    chart.Axis(StandardAxis.PrimaryY).ConstLines.Clear();
    chart.Axis(StandardAxis.SecondaryY).ConstLines.Clear();

    nChartControl1.RecalcLayout();

    AlignAxis(chart.Axis(StandardAxis.PrimaryY), 0.5, 0);
    AlignAxis(chart.Axis(StandardAxis.SecondaryY), 0.5, 0);

    nChartControl1.Refresh();
   }

   private void AlignAxis(NAxis axis, double factor, double value)
   {
    NRange1DD viewRange1 = axis.Scale.RulerRange;

    double valueFactor = viewRange1.GetValueFactor(value);
    double includedValue = value;

if (valueFactor > factor)
{
      // extend view range end
      includedValue = (value - viewRange1.Begin + factor * viewRange1.Begin) / factor;
}
else if (valueFactor < factor)
    {
      // extend view range begin
      includedValue = (factor * viewRange1.End - value) / (factor - 1);
    }

    axis.ConstLines.Clear();

    NAxisConstLine constLine = new NAxisConstLine();
    constLine.StrokeStyle.Width = new NLength(0);
    constLine.Value = includedValue;
    constLine.IncludeInAxisRange = true;
    axis.ConstLines.Add(constLine);
   }

Since both axes are aligned with the same factor and value the value is positioned relatively in the same position on the X and Y axes.

We hope this helps - let us know if you meet any problems or have any questions.