Free Support Forum - aspose.com

Get Center Of PlotArea of a chart

HowItIs.PNG (10.8 KB)
HowItShouldBe.png (5.3 KB)
AsposeTestApp.zip (5.3 MB)

Hi Aspose-Community, I need some help here.

I want to place a textbox centered in the hole of a doughnut-chart. We generate Charts in different sizes and sometimes the title of the chart has multiple lines. Therefore I cannot set the position hardcoded, instead I need to get the position from the chart itself.

For better visualization I made an example with colored squares (100x100 and 50x50 pixels), for better visibility of the center I set the doughnut-hole-size to 0. In the final form the hole should be size 40.

The idea to get the position is the following (see screenshot HowItShouldBe.png):

  1. Get the topleft corner of the plotArea (position of the green rectangle)
  2. Add half of the width and half of the height of the plotArea (position of the red rectangle)
  3. Subtract half of the width and half of the height of the Textbox(/Image) that needs to be placed (position of the black rectangle)

I would expect, that chart.PlotArea.X and chart.PlotArea.Y give me the coordinates for the green rectangle from the previous screenshot.

I tried some positions (see screenshot HowItIs.png):

  1. chart.ChartArea.X and chart.ChartArea.Y -> blue 100x100 rectangle
  2. chart.PlotArea.X and chart. PlotArea.Y -> pink rectangle
  3. chart.ChartObject.X and chart. ChartObject.Y -> turquoise rectangle
  4. chart.ChartObject.Left and chart. ChartObject.Top -> yellow rectangle
  5. chart.ChartObject.UpperDeltaX and chart. ChartObject.UpperDeltaY -> blue 50x50 rectangle

None of these gives me the upper left corner of the plotArea. One also sees in the screenshot, that the blue 50x50 and the turquoise rectangle depend on the chartposition.

Could anyone help me to get the position I need with Aspose.cells?

Thank you for your help.

@comansoftware,
Could you please share the following sample files to test the sample code?

  1. ExcelFiles\AsposeTestCellColor.xlsx
  2. All the images in Colored_Rectangles folder

We will test the scenario here and share our feedback accordingly.

The Excel-File and the images are created in the code.
I forgot to create the directories itself before in the code.
So you can insert the following lines to make the code work:

//insert this line in the first line of Program.cs
using System.IO;

//insert the following lines at the beginning of static void Main(string[] args) in Program.cs
Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + “\ExcelFiles”); Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + “\Colored_Rectangles”);

Thank you for your feedback

@comansoftware,
We are looking into this issue and will share our feedback soon.

@comansoftware,
We have analyzed your requirement and logged it in our database for a thorough investigation. We will write back here once any update is ready for sharing.

This requirement is logged as:
CELLSNET-47835 - Get Center of PlotArea of a chart

@comansoftware,

Please try the fllowing code for your reference:

            AAAChart.Calculate();
            int xPositionOfChartArea = AAAChart.PlotArea.X + AAAChart.PlotArea.Width / 2;
            int yPositionOfChartArea = AAAChart.PlotArea.Y + AAAChart.PlotArea.Height / 2;
            stream = System.IO.File.Open(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + "\\Colored_Rectangles\\black_100.png", System.IO.FileMode.Open);
            Size imgSize = Image.FromStream(stream).Size;
            xPositionOfChartArea -= (int)(imgSize.Width / 2.0 / AAAChart.ChartObject.Width * 4000);
            yPositionOfChartArea -= (int)(imgSize.Height / 2.0 / AAAChart.ChartObject.Height * 4000);
            AAAChart.Shapes.AddPictureInChart(yPositionOfChartArea, xPositionOfChartArea, stream, 100, 100);
            stream.Close();

            stream = System.IO.File.Open(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + "\\Colored_Rectangles\\red_50.png", System.IO.FileMode.Open);
            xPositionOfChartArea = AAAChart.PlotArea.X + AAAChart.PlotArea.Width / 2;
            yPositionOfChartArea = AAAChart.PlotArea.Y + AAAChart.PlotArea.Height / 2;
            AAAChart.Shapes.AddPictureInChart(yPositionOfChartArea, xPositionOfChartArea, stream, 100, 100);
            stream.Close();

Hello Eric.wang

thank you for your response, it helped with finding the center of the plotArea.
Could you tell me, where the value 4000 comes from in your example? It works, but I do not understand why.
So in our application we want to place a textfield, not a colored rectangle (where the text varies). I therefore changed the (from my point of view miraculous) value from 4000 to 550 to hit the center. In this context it would help to know where the value 4000 comes from.
With the following code you can comprehend what I mean.

BBBChart.Calculate();
			int textboxWidth = 600;
			int textboxHeight = 600;
			int miraculousValue = 550;
			xPositionOfChartArea = BBBChart.PlotArea.X + BBBChart.PlotArea.Width / 2;
			yPositionOfChartArea = BBBChart.PlotArea.Y + BBBChart.PlotArea.Height / 2;
			xPositionOfChartArea -= (int)(textboxWidth / 2.0 / BBBChart.ChartObject.Width * miraculousValue);
			yPositionOfChartArea -= (int)(textboxHeight / 2.0 / BBBChart.ChartObject.Height * miraculousValue);
			Aspose.Cells.Drawing.TextBox textBox = BBBChart.Shapes.AddTextBoxInChart(yPositionOfChartArea, xPositionOfChartArea, textboxWidth, textboxHeight);
			textBox.TextBody.Text = "Ist:48%\nPlan:56%";
			textBox.Font.Name = "Calibri";
			textBox.Font.Size = 13;
			textBox.Font.IsBold = true;
			textBox.TextVerticalAlignment = TextAlignmentType.Center;
			textBox.TextHorizontalAlignment = TextAlignmentType.Center;
			textBox.Fill.Transparency = 1;
			textBox.Line.FillType = Aspose.Cells.Drawing.FillType.None;
			stream.Close();

I have another question to add. I want the font to be bold for the whole textbox, but only the first line is bold. Is this a bug and do you have a solution how to resolve this?

Thank you very much for your support.

@comansoftware,
We are analyzing your requirement and will share our feedback soon.

@comansoftware,
The parameters left/top in AddPictureInChart are offset from the upper left corner in units of 1/4000 of the chart area, see API Reference. And you should use PlotArea.Width instead of ChartObject.Width like following code:


 xPositionOfChartArea -= (int)(textboxWidth / 2.0 / BBBChart.PlotArea.Width * 4000)

For the font bug, we are looking into it. Once we have an update on it, we will let you know

@comansoftware,
For your sencond question, we found it is not a bug. You should please set textBox.Font before setting textBox.TextBody.Text.
Please see the following code for your reference:

            textBox.Font.Name = "Calibri";
            textBox.Font.Size = 13;
            textBox.Font.IsBold = true;
            textBox.TextBody.Text = "Ist:48%\nPlan:56%";