We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Placing the chart legend in a custom position with Aspose Cells

Hello.


I’ve seen that when you use LegendPositionType.Corner to position the legend of the chart, there is a problem. The legend’s bottom is the same to the chart’s top.
(See attached image)
The same happens when I place the legend to the Top-Right position with Excel (I don’t know if I need installing updates for Office).
To solve this, I’ve tried placing the chart legend in a custom position with Aspose Cells after setting the position type to LegendPositionType.Right :

	lChart.Legend.Y = 0;
lChart.Legend.X = lChart.PlotArea.X + lChart.PlotArea.Width;
However, both lChart.PlotArea.X and lChart.PlotArea.Width are zero and lChart.Legend.Y is zero already, before I even set it (while the legend is not on the top of the chart)!
Is the a way to position the legend on the top-right of the chart?
I could do it if I knew what were the dimensions of the Plot Area, but I can’t get this information.
Thanks in advance,
Kostas

Hi,

Thanks for your posting and using Aspose.Cells for .NET.

Please provide us your expected output xlsx file which you can create manually using MS-Excel 2010. We will look into your file and provide you a sample code how to achieve the legend with your desired settings.

Hello.


I have the sample XLSX attached.
I just want to have the legend placed to the top-right position.
I’ve made this XLSX by setting the legend position to “Right” and then moving the legend by hand.
Setting the position to “Top-Right” generates the problems I’ve described earlier.

Best Regards,
Kostas
Hmm...

I've just seen that the same thing (the legend appearing too high comparing to the chart) happens in PPTX too in some occasions when you specify a legend position "Top Right".
I haven't figured out yet when it happens and when not.
My guess is that this is an Office bug, but I don't know if it is fixed or not...
If there is a way to position the legend by code, let me know.

Thanks,
Kostas

Hi,

Thanks for your posting and using Aspose.Cells for .NET.

You will have to set legend position right and then set the X, Y coordinates and Height, Width dimensions. Please see the following code, I have attached the input and output files for your reference.

C#


string filePath = @“F:\Shak-Data-RW\Downloads\input.xlsx”;


Workbook workbook = new Workbook(filePath);

Worksheet worksheet = workbook.Worksheets[0];


Chart chart = worksheet.Charts[0];


chart.Legend.Position = LegendPositionType.Right;

chart.Legend.Width = 887;

chart.Legend.Height = 1164;

chart.Legend.X = 3070;

chart.Legend.Y = 115;


workbook.Save(filePath + “.out.xlsx”);

Hello.


My main problem is how to calculate the value for chart.Legend.X.
The chart.PloatArea has no valid sizes in it and without it I can’t guess where
to put the legend to be next to the chart. I can’t use a constant.

Thanks,
Kostas

Hi,

Thanks for your posting and using Aspose.Cells.

I am afraid, there is no good solution to it. You will have to load your workbook and record the coordinates and then manually use the constants for position and size of the legend as shown in the previous post.