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

Free Support Forum - aspose.com

getX() returns 0 for chart data labels

Dear Aspose Support Team

The attached .xlsx file was created with Aspose Cells for Java (version 20.6.3).
LabelPositioning.zip (7.7 KB)

When using the below code snippet to retrieve the x-coordinates of the data labels, it returns 0 in all three cases. How can I retrieve the correct x-coordinates of the data labels?

Chart chart = worksheet.getCharts().get(0);
Series series = chart.getNSeries().get(0);
ChartPointCollection points = series.getPoints();
for (int i = 0; i < points.getCount(); i++) {
	ChartPoint point = points.get(i);
	int x = point.getDataLabels().getX();
	System.out.println(x);
}

Thank you for your support.

@awepler,

Please call Chart.calculate() before retrieving series point labels. See the updated sample code for your reference:
e.g
Sample code:

Chart chart = worksheet.getCharts().get(0);
chart.calculate();
Series series = chart.getNSeries().get(0);
ChartPointCollection points = series.getPoints();
for (int i = 0; i < points.getCount(); i++) {
ChartPoint point = points.get(i);
int x = point.getDataLabels().getX();
System.out.println(x);
}

Hope, this helps a bit.

It helped a bit, thank you. I am now able to retrieve the initial x-coordinates.

Now (extending the above example) I would like to change the position of the data labels from OUTSIDE_END to INSIDE_BASE. After that I recalculate the chart and write out the x-coordinates again. I get the same values as before. How can I retrieve the new x-coordinates of the data labels?

Chart chart = worksheet.getCharts().get(0);
chart.calculate();
Series series = chart.getNSeries().get(0);
ChartPointCollection points = series.getPoints();
for (int i = 0; i < points.getCount(); i++) {
	ChartPoint point = points.get(i);
	int x = point.getDataLabels().getX();
	System.out.println(x);
}
// change position
series.getDataLabels().setPosition(LabelPositionType.INSIDE_BASE);
chart.calculate();
series = chart.getNSeries().get(0);
points = series.getPoints();
for (int i = 0; i < points.getCount(); i++) {
	ChartPoint point = points.get(i);
	int x = point.getDataLabels().getX();
	System.out.println(x);
}

@awepler,

See the updated sample code, it will work for your needs:
e.g
Sample code:

.......
Chart chart = worksheet.getCharts().get(0);
		chart.calculate();
		Series series = chart.getNSeries().get(0);
		ChartPointCollection points = series.getPoints();
		for (int i = 0; i < points.getCount(); i++) {
			ChartPoint point = points.get(i);
			int x = point.getDataLabels().getX();
			System.out.println(x);
                       // change position
			point.getDataLabels().setPosition(LabelPositionType.INSIDE_BASE);
			
		} 

		chart.calculate();
		series = chart.getNSeries().get(0);
		points = series.getPoints();
		for (int i = 0; i < points.getCount(); i++) {
			ChartPoint point = points.get(i);
			int x = point.getDataLabels().getX();
			System.out.println(x);
		}
......

Hope, this helps a bit.

Thank you for your fast reply. This again helped a bit ;-).

Now (extending the example one step further) I would like to explicitly set the x-coordinate of the data label for the topmost bar. Doing this seems to affect the y-coordinate as well. Please find attached LabelPositioningOut.xlsx where the data category label (Abcd) and the data label (-4%) for the topmost bar are no longer properly aligned (LabelPositioningOut.zip (7.9 KB)). How can I keep them aligned?

Chart chart = worksheet.getCharts().get(0);
chart.calculate();
Series series = chart.getNSeries().get(0);
ChartPointCollection points = series.getPoints();
for (int i = 0; i < points.getCount(); i++) {
	ChartPoint point = points.get(i);
	int x = point.getDataLabels().getX();
	System.out.println(x);
	point.getDataLabels().setPosition(LabelPositionType.INSIDE_BASE);
}
// change position
chart.calculate();
series = chart.getNSeries().get(0);
points = series.getPoints();
int max = 0;
for (int i = 0; i < points.getCount(); i++) {
	ChartPoint point = points.get(i);
	int x = point.getDataLabels().getX();
	System.out.println(x);
	if (x > max) max = x;
	point.getDataLabels().setPosition(LabelPositionType.OUTSIDE_END);
}
// adjust x-coordinate of label for topmost bar
chart.calculate();
chart.getNSeries().get(0).getPoints().get(2).getDataLabels().setX(max);

workbook.save("src\\support\\labelpositioning\\LabelPositioningOut.xlsx");

@awepler,

I checked DataLabels.getY() does not give correct value, so you need to adjust it a bit. See the sample code for your rererence:
e.g
Sample code:

int delta = 70;
 int pY = chart.getNSeries().get(0).getPoints().get(2).getDataLabels().getY() + delta;
		
		//adjust x-coordinate and y-coordinate of label for topmost bar
		chart.calculate();
		chart.getNSeries().get(0).getPoints().get(2).getDataLabels().setX(max);
		chart.getNSeries().get(0).getPoints().get(2).getDataLabels().setY(pY);

Thank you for your feedback.

Will the delta always be 70? We have a generic framework, that should be able to create different kinds of charts depending on input. The above chart is just one specific example. Hence, adapting the y-coordinate by a fixed delta seems not a very good solution for us. Is the wrong y-coordinate something you plan to fix in one of your next releases?

@awepler,

No, setting delta value “70” won’t work for every case. As we already found the issue you described by using your sample code with your template file, so I have logged an investigation ticket with an id “CELLSJAVA-43247” for your issue. Hopefully, either we will devise a generic way (we may share a code segment) or fix the issue (internally).

Once we have an update on it, we will let you know.