Trenline Legend Entry Removal Issue

Hi,

I am trying to add the trendlines for the series but removing the trendlines from Legend Entry.I am adding the trendline like below code for each

series.sSeriesList[i].TrendLines.Add(TrendlineType.Linear);

sSeriesList[i].TrendLines[sSeriesList[i].TrendLines.Count - 1].DisplayEquation = false;

sSeriesList[i].TrendLines[sSeriesList[i].TrendLines.Count - 1].DisplayRSquared = false;

sSeriesList[i].TrendLines[sSeriesList[i].TrendLines.Count - 1].LegendEntry.IsDeleted = true;

At this point it does not throws any error but while saving Excel book it throws error that key is already added.

If i dont remove from Legend Entry then it works.

Please let me know how can i solve this issue.

Thanks,

Amit

Hi Amit,


Thanks for your sample code snippet and some details.

I have tested your scenario/case with our latest version/fix : Aspose.Cells for .NET v7.5.3.4 (please try it ), it works fine. I have used the following sample code and please find attached the output file. In the chart (in the output file), the legend entry for the trendline is removed which it should.

Sample code:

Workbook excel = new Workbook();
Worksheet sheet = excel.Worksheets[0];
Cells cells = excel.Worksheets[0].Cells;
cells[“A1”].PutValue(“Daily Rainfall”);
cells[“B1”].PutValue(“Particulate”);
cells[“A2”].PutValue(1.9);
cells[“B2”].PutValue(137);
cells[“A3”].PutValue(3.6);
cells[“B3”].PutValue(128);
cells[“A4”].PutValue(4.1);
cells[“B4”].PutValue(122);
cells[“A5”].PutValue(4.3);
cells[“B5”].PutValue(117);
cells[“A6”].PutValue(5);
cells[“B6”].PutValue(114);
cells[“A7”].PutValue(5.4);
cells[“B7”].PutValue(114);
cells[“A8”].PutValue(5.7);
cells[“B8”].PutValue(112);
cells[“A9”].PutValue(5.9);
cells[“B9”].PutValue(110);
cells[“A10”].PutValue(7.3);
cells[“B10”].PutValue(104);
excel.Worksheets.Add(SheetType.Chart);
ChartCollection charts = excel.Worksheets[1].Charts;
int chartIndex = charts.Add(ChartType.LineWithDataMarkers, 1, 3, 25, 12);
Chart chart = charts[chartIndex];

chart.Legend.Position = LegendPositionType.Top;
chart.Title.Text = “Scatter Chart:Particulate Levels in Rainfall”;
chart.Title.TextFont.Color = Color.Black;
chart.Title.TextFont.IsBold = true;
chart.Title.TextFont.Size = 12;
chart.ValueAxis.MinValue = 50;
chart.ValueAxis.MaxValue = 200;
chart.NSeries.Add(“Sheet1!B2:B10”, true);
// chart.NSeries.CategoryData = “Sheet1!A2:A10”;
chart.NSeries[0].XValues = “Sheet1!A2:A10”;
SeriesCollection nseries = chart.NSeries;
for (int i = 0; i < nseries.Count; i++)
{
nseries[i].Name = “Test Series”;
nseries[i].DataLabels.Position = LabelPositionType.Below;


}

chart.NSeries[0].TrendLines.Add(TrendlineType.Linear, “test_line”);
chart.NSeries[0].TrendLines[0].Weight = WeightType.HairLine;
chart.NSeries[0].TrendLines[0].Color = Color.Blue;
chart.NSeries[0].TrendLines[0].Backward = 0.4;
chart.NSeries[0].TrendLines[0].DisplayRSquared = false;

chart.NSeries[0].TrendLines[0].DisplayEquation = false;


chart.NSeries[0].TrendLines[0].LegendEntry.IsDeleted = true;


chart.CategoryAxis.Title.Text = cells[“A1”].Value.ToString();
chart.ValueAxis.Title.Text = cells[“B1”].Value.ToString();

excel.Save(“e:\test2\out_scatterchart_trendlines_removedlegendent1.xlsx”);

If you still find the issue with our latest version/fix v7.5.3.4, please provide us your complete runnable sample code and attach your template file(s) to reproduce the issue on our end, we will check your issue soon.

Thank you.