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

Free Support Forum - aspose.com

Chart.toImage generated image has less gap from chart to title compared to actual chart in excel

Hi,
Please run the following sample and please check the below mentioned issue.
1. When I add Chart Title, and create image using Chart.toImage, the generated image's chart size differs from actual chart in excel, especially the gap in between, top of the chart and title.

You can experience the difference by opening the generated ppt, and clicking on one of the chart.

Run the example by commenting title code and see the differences.

import java.io.*;
import java.lang.reflect.Array;

import com.aspose.cells.*;
import com.aspose.slides.*;


public class ChartMaker {

public static void main(String args[]) {

try {
String[] cellsName = new String[] { "B1", "B2", "B3", "B4",
"C1", "C2", "C3", "C4",
"D1", "D2", "D3", "D4",
"E1", "E2", "E3", "E4",
"F1", "F2", "F3", "F4"
};

//Array of cell data
int[] cellsValue = new int[] { 67, 86, 68, 91,
44, 64, 89, 48,
46, 97, 78, 60,
43, 29, 69, 26,
24, 40, 38, 25 };

Workbook wb = new Workbook();
Worksheet dataSheet = wb.getWorksheets().getSheet(0);
String dataSheetName = "DataSheet";
dataSheet.setName(dataSheetName);

dataSheet.getCells().getCell("A1").setValue("IN");
dataSheet.getCells().getCell("A2").setValue("US");
dataSheet.getCells().getCell("A3").setValue("UK");
dataSheet.getCells().getCell("A4").setValue("JP");

//Populate DataSheet with data
int size = Array.getLength(cellsName);
for (int i = 0; i < size; i++) {
String cellName = cellsName[i];
int cellValue = cellsValue[i];
dataSheet.getCells().getCell(cellName).setValue(cellValue);
}

//Add a chart sheet
Worksheet chartSheet = wb.getWorksheets().addSheet();
chartSheet.setName("ChartSheet");
int chartSheetIdx = chartSheet.getIndex();

//Add a chart in ChartSheet with data series from DataSheet
int chartWidth = 300;
int chartHeight = 200;

int chartHeightInRows = 0;
int chartWidthInCols = 0;
int cellWidth = dataSheet.getCells().getColumnWidthPixel(0);
int cellHeight = dataSheet.getCells().getRowHeightPixel(0);
chartWidthInCols = (int)(chartWidth / cellWidth);
chartHeightInRows = (int)(chartHeight / cellHeight);
chartWidth = chartWidthInCols * cellWidth;
chartHeight = chartHeightInRows * cellHeight;

System.out.println("OLE SIZE = Cols: " + chartWidthInCols + ", Rows: " + chartHeightInRows);
System.out.println("CHART SIZE = chartWidth: " + chartWidth + ", chartHeight: " + chartHeight);

Chart chart = chartSheet.getCharts().addChart(ChartType.COLUMN_CLUSTERED, 0, 0, 0, 0, chartWidth, chartHeight);
chart.getNSeries().add(dataSheetName + "!B1:F4", false);

String categoryAreaRef = dataSheet.getName() + "!A1:A4";
chart.getNSeries().setCategoryData(categoryAreaRef);

chart.getTitle().setText("Aspose.Cells");

//Get Chart as image.
ImageOptions imgOpts = new ImageOptions();
imgOpts.setImageFormat(ImageFormat.PNG);
imgOpts.setFashion(FileFormatType.EXCEL2003);

chart.toImage(new FileOutputStream("D:\\Temp\\chart.png"), imgOpts);

wb.getWorksheets().setActiveSheet(chartSheetIdx);
wb.setOleSize(0, chartHeightInRows - 1, 0, chartWidthInCols - 1);

//Save the workbook to stream
ByteArrayOutputStream bout = new ByteArrayOutputStream();
wb.save(bout);
wb.save(new FileOutputStream("D:\\Temp\\output.xls"));

Presentation pres = new Presentation();
Slide sld = pres.getSlideByPosition(1);

int slideWidth = (int) pres.getSlideSize().getX();
int slideHeight = (int) pres.getSlideSize().getY();

int x = (slideWidth - chartWidth * 8) / 2;
int y = (slideHeight - chartHeight * 8) / 2;

OleObjectFrame oof = sld.getShapes().addOleObjectFrame(800, 500, chartWidth * 8, chartHeight * 8, "Excel.Sheet.8", bout.toByteArray());
com.aspose.slides.Picture pic = new com.aspose.slides.Picture(pres, new FileInputStream("D:\\Temp\\chart.png"));
int picId = pres.getPictures().add(pic);
oof.setPictureId(picId);

OleObjectFrame oof2 = sld.getShapes().addOleObjectFrame(900 + chartWidth * 8, 500, chartWidth * 8, chartHeight * 8, "Excel.Sheet.8", bout.toByteArray());
com.aspose.slides.Picture pic2 = new com.aspose.slides.Picture(pres, new FileInputStream("D:\\Temp\\chart.png"));
int picId2 = pres.getPictures().add(pic2);
oof2.setPictureId(picId2);

OleObjectFrame oof3 = sld.getShapes().addOleObjectFrame(800, 600 + chartHeight * 8, chartWidth * 8, chartHeight * 8, "Excel.Sheet.8", bout.toByteArray());
com.aspose.slides.Picture pic3 = new com.aspose.slides.Picture(pres, new FileInputStream("D:\\Temp\\chart.png"));
int picId3 = pres.getPictures().add(pic3);
oof3.setPictureId(picId3);

OleObjectFrame oof4 = sld.getShapes().addOleObjectFrame(900 + chartWidth * 8, 600 + chartHeight * 8, chartWidth * 8, chartHeight * 8, "Excel.Sheet.8", bout.toByteArray());
com.aspose.slides.Picture pic4 = new com.aspose.slides.Picture(pres, new FileInputStream("D:\\Temp\\chart.png"));
int picId4 = pres.getPictures().add(pic4);
oof4.setPictureId(picId4);

//Write the presentation on disk
pres.write(new FileOutputStream("D:\\Temp\\output.ppt"));
System.out.println("successfully completed!!!");

}
catch (Exception e) {
e.printStackTrace();
}
}
}

Muhammed

Hi,

Thanks for providing us sample code.

After an initial test, we found the issue regarding the gap/distance b/w the chart title and the plot area. We will figure it out soon.

Your issue has been logged into our issue tracking system with an issue id: CELLSJAVA-14255. We will inform you when it is sorted out.

Thank you.

Hi,

This issue has been pending for quite some time. We need this urgently. Can you provide me an ETA on this?

Thanks,

Hi,



We will try to provide you a fix in 2-3 days.



Thank you

Hi,

Please try the attached version. We have changed the gap from the chart to title.

Thank you.

Hi,

Its working fine.

Thanks
Muhammed

The issues you have found earlier (filed as 14255) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.