Hi,
I am using Aspose cells 7.0.0, when I tried to create a chart image using Chart.toImage I get java.awt.HeadlessException
Exception in thread “main” java.awt.HeadlessExceptionBut, this happens only when I tried to create multiple chart image in the same JVM context and the “java.awt.headless” system property is set to “true”. Otherwise this issue doesn’t occur.
at sun.awt.HeadlessToolkit.getScreenResolution(HeadlessToolkit.java:189)
by setting java.awt.headless to true
- If Chart.toImage is used once it works fine
- If Chart.toImage is used multiple times, it fails
Otherwise, if I disable the java.awt.headless system property then everything works fine…
System.setProperty(“java.awt.headless”, “true”);
int MAX_BOOKS = 2;
for (int i = 0; i < MAX_BOOKS; i++) {
System.out.println(“Creating workbook " + (i + 1));
Workbook workbook = new Workbook();
Worksheet dataSheet = workbook.getWorksheets().add(“Data”);
Cells cells = dataSheet.getCells();
cells.get(“A1”).setValue(”");
cells.get(“A2”).setValue(“America”);
cells.get(“A3”).setValue(“Asia”);
cells.get(“B1”).setValue(“Total”);
cells.get(“B2”).setValue(“100”);
cells.get(“B3”).setValue(“98”);
Worksheet chartSheet = workbook.getWorksheets().get(
workbook.getWorksheets().add(SheetType.CHART));
chartSheet.setName(“Chart”);
workbook.getWorksheets().setActiveSheetIndex(chartSheet.getIndex());
Chart chart = chartSheet.getCharts().get(
chartSheet.getCharts().add(ChartType.LINE, 0, 0, 50, 50));
int index = chart.getNSeries().add("=Data!A2:B3", true);
chart.getNSeries().get(index).setName("=Data!" + “B1”);
chart.setSizeWithWindow(false);
chart.setShowLegend(false);
ImageOrPrintOptions imageOptions = new ImageOrPrintOptions();
imageOptions.setImageFormat(ImageFormat.getJpeg());
ByteArrayOutputStream imageOutputStream = new ByteArrayOutputStream();
chart.toImage(imageOutputStream, imageOptions);
System.out.println(“image " + (i + 1) + " Created”);
}
I have also attached the code along with this post.
Kindly suggest, if I am missing anything here or is there any alternate for this issue?
Thanks
Sundhar
Aspose OEM License Holder