LegendEntry.IsDeleted does not work as expected if series disabled are enabled just before

Hi All,
I have a problem when I try to delete legends in a chart.
I am using Aspose.Cells 25.9.0

Let me explain the issue!

I have a chart with 4 series.
2 are enabled and 2 are disabled:
image.png (46.6 KB)

Via code, I enable the two previous disabled series. (via the FilteredNSeries)
Once it is done, I want to hide the unwished legend. I just want to keep the Data 1 and 2 but I want to hide the “Scatter” legends.
But it does not work as expected. At the end of my code all the legend are deleted which is not expected.
I would like to see the legend for Data 1 and Data 2 but not for Scatter 1 and Scatter 2.

cf the code below:

var pathWorkbook = @"...\\TestHadrienDataLabel.xlsx";
            var wbtest = new Workbook(pathWorkbook);
            Worksheet sheettest = wbtest.Worksheets[0];
            Chart charttest = sheettest.Charts[0];

            // Only data 1 and Scatter 1 series is enabled in the data source and have their legend visible.
            // We want to enable data 2 and scatter 2 series
            Console.WriteLine("-----------Before enable disable series-----------");
            for (int i = charttest.FilteredNSeries.Count - 1; i >= 0; i--)
            {
                var series = charttest.FilteredNSeries[i];
                Console.WriteLine(series.DisplayName+" and series.IsFiltered is "+ series.IsFiltered);

                // Display data 2 and scatter 2
                if (series.DisplayName == "Data 2" || (series.DisplayName == "Scatter 2"))
                {
                    series.IsFiltered = false;
                    Console.WriteLine(series.DisplayName+" is now set to series.IsFiltered :" + series.IsFiltered);
                }
            }

            Console.WriteLine("-----------Before hidden legends-----------");
            for (int i = 0; i < charttest.NSeries.Count; i++)
            {
                var series = charttest.NSeries[i];
                var displayName = series.DisplayName ?? "";

                // Hide legend for series that start with "Scatter"
                if (series.DisplayName.StartsWith("Scatter"))
                {
                    series.LegendEntry.IsDeleted = true;
                    Console.WriteLine(series.DisplayName+" is now set to series.LegendEntry.IsDeleted :" + series.LegendEntry.IsDeleted);
                }
                else
                {
                    // Make sure other legends are visible (optional safety)
                    series.LegendEntry.IsDeleted = false;
                    Console.WriteLine(series.DisplayName+" is now set to series.LegendEntry.IsDeleted :" + series.LegendEntry.IsDeleted);
                }
            }

            Console.WriteLine("-----------Before the last check of the legend-----------");
            for (int i = 0; i < charttest.NSeries.Count; i++)
            {
                Console.WriteLine($"Series[{i}] - Name: '{charttest.NSeries[i].DisplayName}', LegendEntry IsDeleted: {charttest.NSeries[i].LegendEntry.IsDeleted}");
            }

Cf the result of the logs below:

-----------Before enable disable series-----------
Scatter 2 and series.IsFiltered is True
Scatter 2 is now set to series.IsFiltered :False
Data 2 and series.IsFiltered is True
Data 2 is now set to series.IsFiltered :False
-----------Before hidden legends-----------
Data 1 is now set to series.LegendEntry.IsDeleted :False
Data 2 is now set to series.LegendEntry.IsDeleted :False
Scatter 1 is now set to series.LegendEntry.IsDeleted :True
Scatter 2 is now set to series.LegendEntry.IsDeleted :True
-----------Before the last check of the legend-----------
Series[0] - Name: 'Data 1', LegendEntry IsDeleted: True
Series[1] - Name: 'Data 2', LegendEntry IsDeleted: True
Series[2] - Name: 'Scatter 1', LegendEntry IsDeleted: True
Series[3] - Name: 'Scatter 2', LegendEntry IsDeleted: True

Do you know why I don’t have the expected result ?

cf the excel file to reproduce:
TestHadrienDataLabel.zip (12.7 KB)

Thanks for your help!

@hormancey

It appears that the issue you are encountering with the LegendEntry.IsDeleted property is related to how you are enabling the series and managing the legend entries. When you enable the series, the legend entries may not update as expected, which can result in all legends being marked as deleted.

To resolve this, please follow these steps:

  1. Ensure that you are enabling the series correctly and that the legend entries are updated accordingly.
  2. After enabling the series, you may need to refresh the chart to ensure that the legend entries reflect the current state of the series.

Here’s a modified version of your code that includes a refresh step:

var pathWorkbook = @"...\\TestHadrienDataLabel.xlsx";
var wbtest = new Workbook(pathWorkbook);
Worksheet sheettest = wbtest.Worksheets[0];
Chart charttest = sheettest.Charts[0];

// Enable the desired series
for (int i = charttest.FilteredNSeries.Count - 1; i >= 0; i--)
{
    var series = charttest.FilteredNSeries[i];
    if (series.DisplayName == "Data 2" || series.DisplayName == "Scatter 2")
    {
        series.IsFiltered = false;
    }
}

// Refresh the chart to update legend entries
charttest.Refresh();

// Hide legend for series that start with "Scatter"
for (int i = 0; i < charttest.NSeries.Count; i++)
{
    var series = charttest.NSeries[i];
    if (series.DisplayName.StartsWith("Scatter"))
    {
        series.LegendEntry.IsDeleted = true;
    }
    else
    {
        series.LegendEntry.IsDeleted = false; // Ensure other legends are visible
    }
}

// Check the legend entries
for (int i = 0; i < charttest.NSeries.Count; i++)
{
    Console.WriteLine($"Series[{i}] - Name: '{charttest.NSeries[i].DisplayName}', LegendEntry IsDeleted: {charttest.NSeries[i].LegendEntry.IsDeleted}");
}

Make sure to call charttest.Refresh() after enabling the series to ensure that the legend entries are updated correctly. This should help you achieve the desired outcome of keeping the legends for “Data 1” and “Data 2” while hiding those for “Scatter 1” and “Scatter 2”.

If the issue persists, consider checking for any updates or bug fixes in the Aspose.Cells library, as there may be improvements in newer versions.

charttest.Refresh(); does not exist.
I already tested the calculate() method

@hormancey,

After initial testing, I am able to reproduce the issue as you mentioned by using your template Excel file and sample code snippet. I found LegendEntry.IsDeleted does not work as expected if series disabled are enabled.

We require thorough evaluation of the issue. We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): CELLSNET-59081

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Thanks @amjad.sahi for your quick investigation!
Keep me inform of the news.
By chance, do you have any workaround ?
Thanks!

@hormancey
Thank you for your feedback. You are welcome. We will further investigate your issue and notify you promptly of any updates.