Have a template with charts and each chart is set to certain height and width. Using Aspose.Cells, when I try to retrieve the ChartArea height and width, they are returning 0.
Here is an example. Assuming Chart1 is a chart defined in the template
workbook = new Aspose.Cells.Workbook(templatePath);
worksheet = worbook.Worksheets[“sheet1”];
chart = worksheet.Charts.Where(x=> x.Name.Equals(“Chart1”));
chartAreaHeight = chart.ChartArea.Height;
chartAreaWidth = chart.ChartArea.Width;
Please find the attached template copy that has the chart1 object that I am trying to use in the above example. In addition, I would like to know if there is any documentation to refer to set the sensitivity of a workbook?
This is disrupting the dimensions of the objects and doesn’t look good.
I am intending to get the height and width of the chart from the template and set the height and width of the chartArea, Legend, Title based upon the chart dimensions. To be precise, I want to control the size of the chart programmatically and dynamically.
After initial testing, I was able to reproduce the issue as you mentioned by using your template file and following sample code snippet. I am getting dimensions of the chart in template as zero. I also found that ChartArea is automatic when using ChartArea.IsAutomaticSize Boolean attribute. Probably that is the reason your are getting height and width “0”.
Workbook workbook = new Aspose.Cells.Workbook("e:\\test2\\TestTemplate.xlsx");
Worksheet worksheet = workbook.Worksheets["sheet1"];
Chart chart = worksheet.Charts["Chart1"];
//chart.Calculate();//it does not help either
Console.WriteLine(chart.ChartArea.IsAutomaticSize);//True
int chartAreaHeight = chart.ChartArea.Height;
int chartAreaWidth = chart.ChartArea.Width;
Console.WriteLine(chartAreaHeight);//0
Console.WriteLine(chartAreaWidth);//0
We require thorough evaluation of the issue if it could be fixed in the APIs. 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-57734
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.
Aspose.Cells has supported to preserve file labels metadata (sensitivity-labels) in the output/saved XLSX file in recent vesions. Please note, currently, Aspose.Cells only supports to preserve sensitivity labels and does not support creating/updating sensitivity labels in Excel spreadsheets.
Hi @rpmati
For this issue, please use the following code to get the size of the chart object:
int[] chartSize = chart.GetActualSize();
Actual size returns in an array(width and height).
chartSize [0] is width; chartSize [1] is height.
The Width and Height of the chart.ChartArea object are currently 0, which is a bug, and we will fix it in a future version. Currectly, you can use the above method to get the size of the chart.
Which version of Aspose.Cells for .NET you are using? I used latest version/fix (Aspose.Cells for .NET v25.1.x) with the following sample code and chart.GetActualSize() works as expected.
e.g., Sample code:
@amjad.sahi
I have 25.1.2. I was able to get the height and width. But the height and width are significantly smaller than what appears in the actual workbook. Before assigning the calculated width and height to their respective values, I compared them and found the results unusual. Initially, the width and height were around 3000-4000px but after assignment, they dropped to 300-400px.
the unit of chartSize is pixels, while the unit of chart1.Title.Height is in units of 1/4000 of the chart area.
So if you want to achieve the following goals:
That is, if you set chart1.Title.Width to 4000, it will be the same width as chartAreaWidth.
This method of setting proportions is an early Excel rule, and we have realized that it causes confusion for users in use. We will unify it to pixel in subsequent versions, and we will notify you once there is progress. In the meantime, you can try the above solution. Thank you.
Hi @rpmati
For this issue, we have made optimizations. In version 25.3, you can use the XPixel,YPixel,WidthPixel,HeightPixel property to set the pixel value.
Workbook book1 = new Workbook(path + "TestTemplate.xlsx");
Chart chart1 = book1.Worksheets[0].Charts[0];
chart1.Calculate();
int[] chartSize = chart1.GetActualSize();
int chartAreaHeight = chartSize[1];
int chartAreaWidth = chartSize[0];
int pix1 = chart1.PlotArea.WidthPixel;
int pix2 = chart1.Legend.WidthPixel;
chart1.Title.HeightPixel = (int)(chartAreaHeight * 0.05);
chart1.Legend.HeightPixel = (int)(chartAreaHeight * 0.15);
chart1.Legend.YPixel = chartAreaHeight - chart1.Legend.HeightPixel;
You can use the XFraction,YFraction,WidthFraction,HeightFraction property to set the ratio, for example:
chart1.Legend.HeightPixel = (int)(chartAreaHeight * 0.15);
can be rewritten as:
chart1.Legend.HeightFraction = 0.15;
It indicates that the Height of Legend is set to 15% of the chart Height.
This is more user-friendly than the previous 1/4000 unit.
The above functions will take effect in version 25.3.
What is the base value for calculating the XFraction, YFraction, WidthFraction, HeightFraction ? All these elements such as PlotArea, Title, Legend reside in the ChartArea. I am assuming that ChartArea dimensions are considered while calculation the fractions. Please clarify on it.
Also, I don’t see 25.3.0 in NuGet Gallery | Aspose.Cells 25.2.0. Is it released? When it is scheduled to release.
Please let me know so I can try the above solution proposed.
Hi @rpmati
Fraction is a double value between 0 and 1, which represents the ratio to the size of the ChartArea, the base value is Width and Height of the ChartArea. for example:
To make the property names clearer, we will modify “Fraction” to be clearer property names when we release 25.3. The modified attribute names are as follows:
The issues you have found earlier (filed as CELLSNET-57734) have been fixed in this update. This message was posted using Bugs notification tool by leoluo
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
Enables storage, such as cookies, related to analytics.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.