Getting dimensions of a chart in template as zero

Hi,

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;

@rpmati,

Could you please zip and attach your template Excel file. We will check your issue soon.

@rpmati
Please check whether the size of chart area is automatic with chart.ChartArea.IsAutomaticSize.

TestTemplate.zip (10.3 KB)

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?

I tried setting
chart.Title.IsAutomaticSize = true
chart.ChartArea.IsAutomaticSize = true
chart.Label.IsAutomaticSize = true

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.

@rpmati,

Thanks for the template Excel file and details.

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.

I know this is not related to what we are discussing here. But I would appreciate if you provide any leads on the following:

@rpmati,

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.

Hi,

I tried it. It didn’t resolved the issue. Thank you.

@rpmati,

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:

Workbook workbook = new Aspose.Cells.Workbook("e:\\test2\\TestTemplate.xlsx");
Worksheet worksheet = workbook.Worksheets["sheet1"];
Chart chart = worksheet.Charts["Chart1"];
Console.WriteLine(chart.ChartArea.IsAutomaticSize);//True
int[] chartSize = chart.GetActualSize();
Console.WriteLine(chartSize[0]);//1088
Console.WriteLine(chartSize[1]);//481

@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.

Workbook workbook = new Aspose.Cells.Workbook(“e:\test2\TestTemplate.xlsx”);
Worksheet worksheet = workbook.Worksheets[“sheet1”];
Chart chart = worksheet.Charts[“Chart1”];
int[] chartSize = chart.GetActualSize();
var chartAreaWidth = chartSize[0];
var chartAreaHeight = chartSize[1];
chart.PlotArea.Width =
chart.Legend.Width =
chart.Title.Width
= chartAreaWidth;
chart.Title.Y =
chart.Title.X =
chart.Legend.X =
chart.PlotArea.X = 0;
chart.Title.Height = (int)(chartAreaHeight * 0.05);
chart.Legend.Height = (int)(chartAreaHeight * 0.15);
chart.Legend.Y = chartAreaHeight - chart.Legend.Height;
chart.PlotArea.Height = (int)(chartAreaHeight * 0.80);
chart.PlotArea.Y = chartAreaHeight - chart.PlotArea.Height;

@rpmati,

Thank you for your feedback and the provided code snippet.

We will evaluate it and get back to you soon.

Additionally, your original issue (Ticket ID: “CELLSNET-57734”) is currently marked as “In Progress” and we aim to resolve it soon.

Hi @rpmati

Currently,

int[] chartSize = chart.GetActualSize();

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:

chart.Title.Height = (int)(chartAreaHeight * 0.05);

Please try the following code:

chart.Title.Height = 4000 * 0.05;

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.

Hi @leoluo

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.

Thanks.

@rpmati,

We will provide details on the attributes soon.

We plan to release Aspose.Cells for .NET v25.3 next week. You will receive a notification when the new version is published.

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:

WidthPixel = WidthFraction * ChartAreaWidth;
HeightPixel = HeightFraction * ChartAreaHeight;
XPixel = XFraction * ChartAreaWidth;
YPixel = YFraction * ChartAreaHeight;

You can use these properties after the 25.3 release, they are more convenient to use than the value of 1/4000 units, so we added them.

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:

WidthRatioToChart
HeightRatioToChart
XRatioToChart
YRatioToChart

These modifications will also be documented in the 25.3 release notes.
When 25.3 is released, we will also notify you here. Thank you.

1 Like

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