Chart area does not get set or change

Hi,


I have a excel file with multiple tabs in it and each tab has only one graph in it.

I have to (for each graph in the various tabs):

a) set the font for the categoryaxis, valueAxis,legend to size 8.
b) resize the chart horizontally so that it fits within the print area of a US letter size paper (8.5 by 11 inches)

I was able to get point a) done. Point b) does not seem to get done.

Source code:

/******************************************************************************************************/
public static void main(String[] args)
{
Workbook workbook = null;
try {
workbook = new Workbook(“C:\Aspose_Test_Data.xlsx”);
}
catch (Exception ex) {
System.out.println(“Error message is :”+ ex);
}
Worksheet _worksheet = workbook.getWorksheets().get(0);
Chart chart = _worksheet.getCharts().get(0);
chart.getCategoryAxis().getTickLabels().getFont().setSize(8);
chart.getValueAxis().getTickLabels().getFont().setSize(8);
chart.getLegend().getFont().setSize(8);
System.out.println(“the Height of the chart is:” + chart.getChartArea().getHeight());
System.out.println(“the Width of the chart is:” + chart.getChartArea().getWidth());
chart.getChartArea().setWidth(3300);
System.out.println(“the set width of the chart now is:” + _worksheet.getCharts().get(0).getChartArea().getWidth());
try {
workbook.save(“C:\Aspose_Test_Data_new.xlsx”, FileFormatType.XLSX);
} catch (Exception ex) {
System.out.println(“Error message is :”+ ex);
}
/************************************************/
chart.getChartArea().getHeight() prints 0 and chart.getChartArea().getWidth() also prints 0.

There is no change in the size of the graph (even if I set it to different values). anything extra that should be done here?

Sample file attached too here.

Hi Chetan,

Thanks for your posting and using Aspose.Cells.

You need to use chart.getChartObject().setWidth() method to set the width of the chart. Please see the following code. It resizes your chart width to 3300. I have also attached the output file generated by this code.

Please also see the following article for your reference.

  • Change the Chart’s Position and Size

Java

String filePath = “F:\Shak-Data-RW\Downloads\Aspose_Test_Data.xlsx”;


Workbook workbook = new Workbook(filePath);


Worksheet _worksheet = workbook.getWorksheets().get(0);


Chart chart = _worksheet.getCharts().get(0);


chart.getChartObject().setWidth(3300);


workbook.save(filePath + “.out.xlsx”, SaveFormat.XLSX);