Nevron Forum

Binding with Secondary Y-Axis

https://www.nevron.com/Forum/Topic2488.aspx

By David Huxtable - Wednesday, July 15, 2009

Good Afternoon,

I didn't notice an example in the Data Manipulation section of the examples where a chart is bound to a datatable, in which a secondary y axis is also bound to a column. Is there such an example?

If not would it be possible for somebody a post a very basic example doing so?

My main problem is allowing the secondary y axis series to use the same X values as the series with the primary x, y and possibly z axes.

I hope this made sense.

Thankyou in advance,

David.
By David Huxtable - Thursday, July 16, 2009

For anybody who has come up with the same issue I have worked out the solution.

It turned out to be quite simple and I cannot believe it took me so long to figure the answer.

Basically, you will have another series for the secondary y-axis, in which you must tell it use the x and possibly y values, as with the first series containing the x, y and possible z axes values and bind those x and z (if applicable) values to the second series also so they share the same x and z values in the one chart.

Below is a simple example of such:

// DataTable
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn() { DataType = typeof(double), ColumnName = "X" });
dt.Columns.Add(new DataColumn() { DataType = typeof(double), ColumnName = "Y" });
dt.Columns.Add(new DataColumn() { DataType = typeof(double), ColumnName = "Y2" });
dt.Columns.Add(new DataColumn() { DataType = typeof(double), ColumnName = "Z" });
dt.Columns.Add(new DataColumn(){DataType=typeof(double), ColumnName="ZTest"});

// Populate table with random data.
Random rand = new Random();

for (int rowIndex = 0; rowIndex < dt.Rows.Count; rowIndex++)
{
for (int columnIndex = 0; columnIndex < dt.Columns.Count; columnIndex++)
{
dt.Rows[rowIndex][columnIndex] = rand.NextDouble();
}
}

NChart chart = nChartControl1.Charts[0];

// 1st series.
NBarSeries barSeries = (NBarSeries)chart.Series.Add(SeriesType.Bar);
barSeries.UseXValues = true;
barSeries.UseZValues = true;

// 1st series binding.
NDataBindingManager dataBindingManager = nChartControl1.DataBindingManager;

// The series index to bind to is the second parameter.
dataBindingManager.AddBinding(0, 0, "XValues", dt, "X");
dataBindingManager.AddBinding(0, 0, "Values", dt, "Y");
dataBindingManager.AddBinding(0, 0, "ZValues", dt, "Z");

2nd series.
NLineSeries lineSeries = (NLineSeries)chart.Series.Add(SeriesType.Line);
lineSeries.UseXValues = true;
lineSeries.UseZValues = true;

// Declare a secondary y axis and dock it to the right of the chart.
NAxis secondaryY = (NAxis)chart.Axis(StandardAxis.SecondaryY);
secondaryY.Anchor = new NDockAxisAnchor(AxisDockZone.FrontRight);
secondaryY.Visible = true;

// 2nd series binding, notice the series index (parameter 2).
dataBindingManager.AddBinding(0, 1, "XValues", dt, "X");
dataBindingManager.AddBinding(0, 1, "Values", dt, "Y2");
dataBindingManager.AddBinding(0, 1, "ZValues", dt, "Z");

// Create a scale configurator and set it to the secondary y axis.
NLinearScaleConfigurator secYConfig = (NLinearScaleConfigurator)secondaryY.ScaleConfigurator;
// Optinally you can configure some styling.
secYConfig.RulerStyle.FillStyle = new NColorFillStyle(System.Drawing.Color.DarkRed);
secYConfig.RulerStyle.BorderStyle.Color = System.Drawing.Color.Red;
secYConfig.InnerMajorTickStyle.LineStyle.Color = System.Drawing.Color.Red;
secYConfig.OuterMajorTickStyle.LineStyle.Color = System.Drawing.Color.Red;
secYConfig.InnerMajorTickStyle.Length = new NLength(0);
secYConfig.MajorGridStyle.ShowAtWalls = new ChartWallType[] { ChartWallType.Back, ChartWallType.Left };
secYConfig.LabelStyle.TextStyle.FillStyle = new NColorFillStyle(System.Drawing.Color.DarkRed);

// Display on standard secondary y axis, ensuring not on primary y axis.
secYConfig.DisplayOnAxis(StandardAxis.PrimaryY, false);
secYConfig.DisplayOnAxis(StandardAxis.SecondaryY, true);

In the case I have missed something, this quick example should hopefully guide you to binding a chart containing a secondary y axis to a datatable and allow it to share the x and z values.

David