Position of data label frame

Hello,


I’m attempting to put a shape around certain data labels if the value meets certain criteria.

I can add the shape easily enough:

DataLabels d = point.DataLabels;
Shape shape = c.Shapes.AddAutoShapeInChart(
AutoShapeType.Oval, d.X, d.Y, 500, 500);

The problem is, from what I can tell, DataLabels.X and DataLabels.Y are always 0 (same for DataLabel.Width and DataLabel.Height).

Is there a way to get the position of a data label within the chart area?

Hi,


Please use Chart.Calculate() method before retrieving the position of the points’ datalabels. see the Sample code withe the attached template file.

Sample code:

string path = @“e:\test2”;
Workbook workbook = new Workbook(path + “book1.xlsx”);
Chart c = workbook.Worksheets[0].Charts[0];

c.Calculate();

for (int i = 0; i < c.NSeries.Count; i++)
{
Series series = c.NSeries[i];
int pointCount = series.Points.Count;
for (int j = 0; j < pointCount; j++)
{
Console.WriteLine(“Point” + j + " Datalabel Postion: "
+ " X = " + series.Points[j].DataLabels.X
+ " Y = " + series.Points[j].DataLabels.Y
+ " Width = " + series.Points[j].DataLabels.Width
+ " Height = " + series.Points[j].DataLabels.Height);
}
}

Thank you.

It doesn’t work when you create a chart in code:


Workbook wb = new Workbook();
Worksheet ws = wb.Worksheets[0];

ws.Cells[“A1”].PutValue(50);
ws.Cells[“A2”].PutValue(100);
ws.Cells[“A3”].PutValue(150);
ws.Cells[“B1”].PutValue(4);
ws.Cells[“B2”].PutValue(20);
ws.Cells[“B3”].PutValue(50);
int chartIndex = ws.Charts.Add(Aspose.Cells.Charts.ChartType.Pyramid, 5, 0, 15, 5);

Chart c = ws.Charts[chartIndex];
c.NSeries.Add(“A1:B3”, true);
c.NSeries[0].DataLabels.ShowValue = true;
c.NSeries[1].DataLabels.ShowValue = true;
c.Calculate();

for (int i = 0; i < c.NSeries.Count; i++)
{
Series series = c.NSeries[i];
int pointCount = series.Points.Count;
for (int j = 0; j < pointCount; j++)
{
Console.WriteLine(“Point” + j + " Datalabel Postion: "
+ " X = " + series.Points[j].DataLabels.X
+ " Y = " + series.Points[j].DataLabels.Y
+ " Width = " + series.Points[j].DataLabels.Width
+ " Height = " + series.Points[j].DataLabels.Height);
}
}

Any more advice?

Hi Shaun,

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

Please add the following line of code in the beginning.

Workbook wb = new Workbook(FileFormatType.Xlsx);

It will give correct results. Please see the output of the code below for your reference.

C#


Workbook wb = new Workbook(FileFormatType.Xlsx);

Worksheet ws = wb.Worksheets[0];


ws.Cells[“A1”].PutValue(50);

ws.Cells[“A2”].PutValue(100);

ws.Cells[“A3”].PutValue(150);

ws.Cells[“B1”].PutValue(4);

ws.Cells[“B2”].PutValue(20);

ws.Cells[“B3”].PutValue(50);

int chartIndex = ws.Charts.Add(Aspose.Cells.Charts.ChartType.Pyramid, 5, 0, 15, 5);


Chart c = ws.Charts[chartIndex];

c.NSeries.Add(“A1:B3”, true);

c.NSeries[0].DataLabels.ShowValue = true;

c.NSeries[1].DataLabels.ShowValue = true;

c.Calculate();


for (int i = 0; i < c.NSeries.Count; i++)

{

Series series = c.NSeries[i];

int pointCount = series.Points.Count;

for (int j = 0; j < pointCount; j++)

{

Console.WriteLine(“Point” + j + " Datalabel Postion: "

+ " X = " + series.Points[j].DataLabels.X

+ " Y = " + series.Points[j].DataLabels.Y

+ " Width = " + series.Points[j].DataLabels.Width

+ " Height = " + series.Points[j].DataLabels.Height);

}

}


Output:
Point0 Datalabel Postion: X = 813 Y = 1576 Width = 250 Height = 494
Point1 Datalabel Postion: X = 1475 Y = 729 Width = 350 Height = 494
Point2 Datalabel Postion: X = 2175 Y = -46 Width = 350 Height = 494
Point0 Datalabel Postion: X = 1063 Y = 2329 Width = 163 Height = 494
Point1 Datalabel Postion: X = 1725 Y = 2071 Width = 250 Height = 494
Point2 Datalabel Postion: X = 2438 Y = 1576 Width = 250 Height = 494