AssignYAxisIndex for the SampleDataSeries controls which Y-axis it is bound to. It is the index number of the Y-axis in chart.ViewXY.YAxes -array. Now if you remove an axis from the chart, a series cannot find its axis in that array, in which case AssignYAxisIndex gets value -1. This leads to a crash since the YAxes -array you are using cannot have an index of -1.
There are several ways to fix this. One way could be to store the removed/hidden axes to a separate array, for example:
Code: Select all
private void LegendBox_CheckBoxStateChanged(object sender, Arction.Wpf.Charting.Views.CheckBoxStateChangedEventArgs e)
PointLineSeries p = (PointLineSeries)e.Series;
int ind = _chart.ViewXY.PointLineSeries.IndexOf(p);
if (hiddenYaxes[ind] != null)
AxisY ay = hiddenYaxes[ind];
hiddenYaxes[ind] = null;
p.AssignYAxisIndex = _chart.ViewXY.YAxes.IndexOf(ay);
AxisY ay = _chart.ViewXY.YAxes[p.AssignYAxisIndex];
hiddenYaxes[ind] = ay;
We are using PointLineSeries here but the exact same method works for SampleDataSeries as well. Note that the above assumes that the amount of axes and series doesn't change (in your case it stays 8). Of course, a list can be used to store the hidden axes as well.
Removing axes has one flaw that has to be handled. If you remove all the Y-axes the view cannot be drawn. For instance, a check could be added which allows removing only if visible series count is more than 1.
An alternative would be to use Segmented layout instead of Stacked. In this case you need to add a Segment for each Y-axis and then adjust the height of the Segments, for example set it to 1 for every Segment (Segment height is relative height to other segments). It is slightly more complicated to set up compared to Layered, but it has the upside that you don't need to add or remove anything in CheckBoxStateChanged -event. You can just set the height to 0 or back to 1.
Hope this helps.