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 ?
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:
Ensure that you are enabling the series correctly and that the legend entries are updated accordingly.
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.
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.
We are pleased to inform you that your issue (Ticket ID: “CELLSNET-59081”) has been resolved. The fix or enhancement will be included in the upcoming release (Aspose.Cells v25.10), which we plan to release either by the end of this week or early next week of October 2025. Rest assured, we will notify you in this thread as soon as the new version becomes available.
The issues you have found earlier (filed as CELLSNET-59081) have been fixed in this update. This message was posted using Bugs notification tool by leoluo
I did little test and it seems better. Now the LegendEntry.IsDeleted property is kept in the aspose chart but I have an example where legends are not hidden properly.
In my example, all the series are enabled in the excel chart by default
I just want to hide the “scatter” legend. (of course I want to keep the series enabled)
But I don’t have the good result in the excel chart whereas the good legend properties are applied.
Code below:
var pathWorkbook = @"....\TestHadrienDataLabel\TestHadrienDataLabel_TL.xlsx";
var pathWorkbookresult = @"....\TestHadrienDataLabel\Result.xlsx";
// Open workbook directly from file path
var wbtest = new Workbook(pathWorkbook);
// -------- Sheet Test (2) ----------
Worksheet sheet2 = wbtest.Worksheets["Test (2)"];
Chart chart2 = sheet2.Charts[0];
// Hide legends for Scatter series in sheet 2
for (int i = 0; i < chart2.NSeries.Count; i++)
{
var series = chart2.NSeries[i];
if (series.DisplayName != null &&
series.DisplayName.Trim().StartsWith("Scatter", StringComparison.OrdinalIgnoreCase))
series.LegendEntry.IsDeleted = true;
// else
// series.LegendEntry.IsDeleted = false;
Console.WriteLine($"Deleting legend for : {series.DisplayName} , series.LegendEntry.IsDeleted : {series.LegendEntry.IsDeleted}");
}
// -------- SAVE to result path ----------
wbtest.Save(pathWorkbookresult, SaveFormat.Xlsx);
Thanks for the template Excel file, screenshots and details.
I reproduced the issue as you mentioned by using your template Excel file and sample code snippet. I found LegendEntry.IsDeleted property is not working properly.
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-59177
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.
Hi @hormancey
CELLSNET-59177 will be fixed in Aspose.Cells 25.11, It is expected to be released in the first half of November. We will notify you here when it is released, thank you.