Hi,
I’m not sure why but using nested foreach to set custom datalabels doesn’t seem to work, but using nested for loops does, is this normal?
Here is the test code, unless I’m missing something, I’d expect both images to be the same, for now I’ll just use the “for method” but since I lost some time on this I figured I’d report it just in case it’s an issue that might need to be fixed
chartFor.png is my expected result, while chartForeach.png isn’t
Workbook workbook = new Workbook();
int sheetIndex = workbook.Worksheets.Add();
Worksheet worksheet = workbook.Worksheets[sheetIndex];
//Values to generate chart with (to ensure a minimum column size)
worksheet.Cells[0, 0].PutValue(2000);
worksheet.Cells[1, 0].PutValue(600);
worksheet.Cells[0, 1].PutValue(3000);
worksheet.Cells[1, 1].PutValue(600);
int chartIndex = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.ColumnStacked, 10, 0, 20, 10);
Aspose.Cells.Charts.Chart chart = worksheet.Charts[chartIndex];
chart.NSeries.Add(“A1:B3”, true);
foreach (Aspose.Cells.Charts.Series series in chart.NSeries)
{
foreach (Aspose.Cells.Charts.ChartPoint point in series.Points)
{
point.DataLabels.Text = “Test”;
}
}
chart.ToImage(“chartForeach.png”); //No labels are shown?
for (int seriesIdx = 0; seriesIdx < chart.NSeries.Count; seriesIdx++)
{
for (int pointIdx = 0; pointIdx < chart.NSeries[seriesIdx].Points.Count; pointIdx++)
{
chart.NSeries[seriesIdx].Points[pointIdx].DataLabels.Text = “Test”;
}
}
chart.ToImage(“chartFor.png”); //While here they are, I’d expect both methods to yield the same results
Hi,
Thanks for your posting and using Aspose.Cells.
We were able to observe this issue by executing your sample code with the latest version: Aspose.Cells for .NET v16.10.9.0. We have logged this issue in our database for investigation. We will look into it and fix this issue. Once the issue is resolved or we have some other update for you, we will let you know asap.
This issue has been logged as
- CELLSNET-44896 - Setting DataLabels.Text on data points using foreach does not work
Hi,
Thanks for using Aspose.Cells.
By default, the points of a series are not created. So when you use foreach e.g.
foreach(Aspose.Cells.Charts.ChartPoint point in series.Points)
{
point.DataLabels.Text = "Test";
}
point.DataLabels.Text = "Test"; is not executed.
The point is created when you call "chart.NSeries[seriesIdx].Points[pointIdx]".
We think, we should not create points when series is created because it will consume more memory and this is also the behavior of MS-Excel. For example, in the excel file, if a point’s setting is same as the series, we don’t find any record in excel file.