Convert Chart to Image for Java

I have tried the following sample, but it not generating the image. Can you check what is wrong in the code?

import java.lang.reflect.Array;
import java.io.*;
import com.aspose.cells.*;
import com.aspose.slides.*;

public class ChartMaker {

public static void main(String args[]) {

try {
String[] cellsName = new String[] { "A1", "A2", "A3", "A4",
"B1", "B2", "B3", "B4",
"C1", "C2", "C3", "C4",
"D1", "D2", "D3", "D4",
"E1", "E2", "E3", "E4" };

//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().addSheet();
String dataSheetName = "DataSheet";
dataSheet.setName(dataSheetName);

//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(SheetType.CHART);
chartSheet.setName("ChartSheet");
int chartSheetIdx = chartSheet.getIndex();

//Add a chart in ChartSheet with data series from DataSheet
int chartRows = 55, chartCols = 25;
Chart chart = chartSheet.getCharts().addChart(ChartType.COLUMN_CLUSTERED, 0, chartRows, 0, chartCols);
chart.getNSeries().add(dataSheetName + "!A1:E1", false);
chart.getNSeries().add(dataSheetName + "!A2:E2", false);
chart.getNSeries().add(dataSheetName + "!A3:E3", false);
chart.getNSeries().add(dataSheetName + "!A4:E4", false);
//Set ChartSheet an active sheet

//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, chartRows, 0, chartCols);

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

Presentation pres = new Presentation();
Slide sld = pres.addEmptySlide();

int slideWidth = (int) pres.getSlideSize().getX() - 1500;

int slideHeight = (int) pres.getSlideSize().getY();
int x = 1500 / 2;
OleObjectFrame oof = sld.getShapes().addOleObjectFrame(x, 0, slideWidth, slideHeight, "Excel.Sheet.8", bout.toByteArray());

com.aspose.slides.Picture pic = new com.aspose.slides.Picture(pres, new FileInputStream("D:\\Temp\\chart.jpeg"));
int picId = pres.getPictures().add(pic);
oof.setPictureId(picId);

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

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

}

It generates chart.png, but no data, file size is 0. so when I try to add it to slide, it fails.

Hi,

Thank you for considering Aspose.

Well, I checked you code and the files are generated fine. You need to modify your code a bit as mentioned below:

import java.lang.reflect.Array;

import java.io.*;

import com.aspose.cells.*;

import com.aspose.slides.*;

public class ChartMaker {

public static void main(String args[]) {

try {

String[] cellsName = new String[] { “A1”, “A2”, “A3”, “A4”,

“B1”, “B2”, “B3”, “B4”,

“C1”, “C2”, “C3”, “C4”,

“D1”, “D2”, “D3”, “D4”,

“E1”, “E2”, “E3”, “E4” };

//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().addSheet();

String dataSheetName = “DataSheet”;

dataSheet.setName(dataSheetName);

//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(SheetType.CHART );

chartSheet.setName(“ChartSheet”);

int chartSheetIdx = chartSheet.getIndex();

//Add a chart in ChartSheet with data series from DataSheet

int chartRows = 55, chartCols = 25;

Chart chart = chartSheet.getCharts().addChart(ChartType.COLUMN_CLUSTERED , 0, chartRows, 0, chartCols);

chart.getNSeries().add(dataSheetName + “!A1:E1”, false );

chart.getNSeries().add(dataSheetName + “!A2:E2”, false );

chart.getNSeries().add(dataSheetName + “!A3:E3”, false );

chart.getNSeries().add(dataSheetName + “!A4:E4”, false );

//Set ChartSheet an active sheet

//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, chartRows, 0, chartCols);

//Save the workbook to stream

ByteArrayOutputStream bout = new ByteArrayOutputStream();

wb.save(bout);

//write method is deprecated, use save method instead

wb.save( new FileOutputStream( “D:\Temp\output.xls” ));

Presentation pres = new Presentation();

Slide sld = pres.addEmptySlide();

int slideWidth = (int ) pres.getSlideSize().getX() - 1500;

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

int x = 1500 / 2;

OleObjectFrame oof = sld.getShapes().addOleObjectFrame(x, 0, slideWidth, slideHeight, “Excel.Sheet.8”, bout.toByteArray());

//File Extension should be PNG instead of JPEG

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);

//Write the presentation on disk

pres.write(new FileOutputStream(“D:\Temp\output.ppt”));

System.out .println(“successfully completed!!!”);

}

catch (Exception e) {

e.printStackTrace();

}

}

}

Thank You & Best Regards,

I copied the code you have updated.

My issue is the file “D:\Temp\chart.png” generated using the toImage method is empty, size 0. Please let me know is there anything wrong in the code.

Chart chart = chartSheet.getCharts().addChart(ChartType.COLUMN_CLUSTERED , 0, chartRows, 0, chartCols);

chart.getNSeries().add(dataSheetName + “!A1:E1”, false );

chart.getNSeries().add(dataSheetName + “!A2:E2”, false );

chart.getNSeries().add(dataSheetName + “!A3:E3”, false );

chart.getNSeries().add(dataSheetName + “!A4:E4”, false );

//Set ChartSheet an active sheet

//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);

sorry...

its working fine with the version posted in <A href="</A>. </P> <P>I was trying with Aspose.CellsV2.1.1.10b, which was failing.</P> <P>Thanks</P> <P>Muhammed</P>

Hello again,

I see through other posts that there is a newer beta version than I currently tried with this feature. I’ll download that to see how it is working. My reason for the new post really is to see if there is any information I can gather on to when the release jar (not beta) with this feature will be available. We do own a license and I’m trying to to figure out when we can remove our other implementation of Excel workbooks. We want to use yours exclusively, but can’t until this feature is available.

Thanks in advance,
Frank

Hi Frank,

We are scheduled to release the next official version (Aspose.Cells for Java) that will support Chart -to- Image feature very soon.

However, you can use the attached version/fix v2.1.1.27, it 's an intermediate kind of build (not beta version) but will behave like an official release that supports Chart -to- Image feature with some enhancements and you can use it as long as you wish for you need in your project as it works fine. It’s not been so long time since we introduced the first beta version for Chart -to- Image feature for the users but it is getting mature and enhanced a bit now.


Thank you.

The issues you have found earlier (filed as CELLSJAVA-12243, CELLSJAVA-12880) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.

Hi,

<!–[if gte mso 10]>

/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:"Times New Roman"; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;}

<![endif]–>

This official release v2.1.2 has included the Chart2Image feature, so please try

Aspose.Cells for Java (Latest Version)

Thank you.

Aspose.Cells now supports to convert the Excel Charts to images and PDF formats with enhancements. The feature has become more robust and efficient, so you can convert all types of MS Excel charts to images and PDF (directly).

See the following sample code for your reference:
e.g
Sample code:

// Instantiating a Workbook object
Workbook workbook = new Workbook();

// Obtaining the reference of the first worksheet
WorksheetCollection worksheets = workbook.getWorksheets();
Worksheet sheet = worksheets.get(0);

// Adding some sample value to cells
Cells cells = sheet.getCells();
Cell cell = cells.get("A1");
cell.setValue(50);
cell = cells.get("A2");
cell.setValue(100);
cell = cells.get("A3");
cell.setValue(150);
cell = cells.get("B1");
cell.setValue(4);
cell = cells.get("B2");
cell.setValue(20);
cell = cells.get("B3");
cell.setValue(50);

ChartCollection charts = sheet.getCharts();

// Adding a chart to the worksheet
int chartIndex = charts.add(ChartType.PYRAMID, 5, 0, 15, 5);
Chart chart = charts.get(chartIndex);

// Adding NSeries (chart data source) to the chart ranging from "A1"
// cell to "B3"
SeriesCollection serieses = chart.getNSeries();
serieses.add("A1:B3", true);

// Create an instance of ImageOrPrintOptions and set a few properties
ImageOrPrintOptions options = new ImageOrPrintOptions();
options.setVerticalResolution(300);
options.setHorizontalResolution(300);
options.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
options.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

// Convert chart to image with additional settings
chart.toImage("chart.png", options);

See the document for your further reference:

You may also download the up-to-date examples/demos here