// Create a png file stream FileOutputStream outputStream = null; try { outputStream = new FileOutputStream("ConvertChartToImage.png"); } catch (FileNotFoundException e) { e.printStackTrace(); } // Create a new workbook Workbook workbook = new Workbook(); IWorksheet worksheet = workbook.getWorksheets().get(0); IShape chart = worksheet.getShapes().addChart(ChartType.Area, 20, 20, 360, 230); worksheet.getRange("A20:C32") .setValue(new Object[][] { { null, "Blue Series", "Orange Series" }, { "Jan", 0, 59.1883603948205 }, { "Feb", 44.6420211591501, 52.2280901938606 }, { "Mar", 45.2174930051225, 49.8093056416248 }, { "Apr", 62, 37.3065749226828 }, { "May", 53, 34.4312192530766 }, { "Jun", 31.8933622049831, 69.7834561753736 }, { "Jul", 41.7930895085093, 63.9418103906982 }, { "Aug", 73, 57.4049534494926 }, { "Sep", 49.8773891668518, 33 }, { "Oct", 50, 74 }, { "Nov", 54.7658428630216, 22.9587876597096 }, { "Dec", 32, 54 } }); chart.getChart().getSeriesCollection().add(worksheet.getRange("A20:C32"), RowCol.Columns); chart.getChart().getChartTitle().setText("Area Chart"); // Save the chart as image to a stream. chart.toImage(outputStream, ImageType.PNG); // Close the file stream try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); }
// Create a Image file stream FileOutputStream("ConvertChartToImage.png").use { val outputStream = it // Create a new workbook var workbook = Workbook() val worksheet: IWorksheet = workbook.getWorksheets().get(0) val chart: IShape = worksheet.getShapes().addChart(ChartType.Area, 20.0, 20.0, 360.0, 230.0) worksheet.getRange("A20:C32").setValue(arrayOf(arrayOf(null, "Blue Series", "Orange Series"), arrayOf("Jan", 0, 59.1883603948205), arrayOf("Feb", 44.6420211591501, 52.2280901938606), arrayOf("Mar", 45.2174930051225, 49.8093056416248), arrayOf("Apr", 62, 37.3065749226828), arrayOf("May", 53, 34.4312192530766), arrayOf("Jun", 31.8933622049831, 69.7834561753736), arrayOf("Jul", 41.7930895085093, 63.9418103906982), arrayOf("Aug", 73, 57.4049534494926), arrayOf("Sep", 49.8773891668518, 33), arrayOf("Oct", 50, 74), arrayOf("Nov", 54.7658428630216, 22.9587876597096), arrayOf("Dec", 32, 54))) chart.getChart().getSeriesCollection().add(worksheet.getRange("A20:C32"), RowCol.Columns) chart.getChart().getChartTitle().setText("Area Chart") // Save the chart as image to a stream. chart.toImage(outputStream, ImageType.PNG) // End using the file stream }