Remove Y axis at runtime

Need help in implementing some specific function to your LightningChart Ultimate powered application? Post a question and get code snippets from other LightningChart Ultimate community members.

Moderator: Queue Moderators

Post Reply
abol810
Posts: 6
Joined: Sat Aug 11, 2018 2:53 pm

Remove Y axis at runtime

Post by abol810 » Tue May 26, 2020 6:50 am

Hi,
I have added 8 Y Axes to my chart.
I want to give the user the ability to uncheck a certain series along with its Y axis.(when a series is in unchecked in the legend box, the corresponding Y axis should also disappear).
I'm using the following piece of code to do so:

Code: Select all

private void LegendBox_CheckBoxStateChanged(MouseItemBase series,bool isChecked)
        {
            SampleDataSeries s = (SampleDataSeries) series;
            
             View.YAxes[s.AssignYAxisIndex].Visible=isChecked;                                    
        }
the series and it's Y axis disappear but the Y axis gap does not and other Y Axes do not expand or shrink to fill the chart area.
i tried to change my code to the following:

Code: Select all

private void LegendBox_CheckBoxStateChanged(MouseItemBase series,bool isChecked)
        {
            SampleDataSeries s = (SampleDataSeries) series;            
            if (isChecked)
            {
                View.YAxes.Add(YAxes[s.AssignYAxisIndex]);
            }
            else
            {               
                View.YAxes.Remove(YAxes[s.AssignYAxisIndex]);                
            }                        
        }

but i get the following exception:
System.Exception: 'Invalid Y axis index'
Note: YAxes[] is an array of AxisY objects.
Note 2: Chart.ViewXY.AxisLayout.YAxesLayout = YAxesLayout.Stacked;
any help is appreciated.

Arction_LasseP
Posts: 141
Joined: Wed Mar 27, 2019 1:05 pm

Re: Remove Y axis at runtime

Post by Arction_LasseP » Tue May 26, 2020 10:51 am

Hello,

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 (e.IsChecked)
            {
                if (hiddenYaxes[ind] != null)
                {
                    AxisY ay = hiddenYaxes[ind];
                    _chart.ViewXY.YAxes.Add(ay);
                    hiddenYaxes[ind] = null;
                    p.AssignYAxisIndex = _chart.ViewXY.YAxes.IndexOf(ay);
                }
            }
            else
            {
                AxisY ay = _chart.ViewXY.YAxes[p.AssignYAxisIndex];
                _chart.ViewXY.YAxes.Remove(ay);
                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.
Best regards,
Lasse

Post Reply