protected String createAndSaveImage(DesignOptions options, JFreeChart chart, ChartRenderingInfo renderingInfo) throws GeneratorException { int width = options.getWidth(); int height = options.getHeight(); BufferedImage image = chart.createBufferedImage(width, height, renderingInfo); Graphics2D chartGraphics = image.createGraphics(); chartGraphics.setColor(Color.white); chartGraphics.fillRect(0, 0, width, height); chart.draw(chartGraphics, new Rectangle2D.Float(0, 0, width, height)); try { return ServletUtilities.saveChartAsPNG(chart, width, height, renderingInfo, null); } catch (IOException e) { throw new GeneratorException("Could not save PNG!", e); } }
@GET @Path("/charts") @Produces("image/png") @ApiOperation(value = "", notes = "") @ApiResponses(value = { @ApiResponse(code = 500, message = "Internal Server Error") }) public Response doGetChart(@Context HttpSession session) throws IOException { DefaultPieDataset dataset = new DefaultPieDataset(); dataset.setValue("Linux", 29); dataset.setValue("Mac", 20); dataset.setValue("Windows", 51); JFreeChart chart = ChartFactory.createPieChart3D("hello world", // chart title dataset, // data true, // include legend true, false); PiePlot3D plot = (PiePlot3D) chart.getPlot(); plot.setStartAngle(290); plot.setDirection(Rotation.CLOCKWISE); plot.setForegroundAlpha(0.5f); // Write the chart image to the temporary directory ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection()); String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, info, session); byte[] imageData = FileUtils.readFileToByteArray(new File("/tmp/" + filename)); //BufferedImage image = ...; /*ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(image, "png", baos); byte[] imageData = baos.toByteArray();*/ // uncomment line below to send non-streamed // return Response.ok(imageData).build(); // uncomment line below to send streamed return Response.ok(new ByteArrayInputStream(imageData)).build(); }