How to render JFreeChart in Play framework

谁说我不能喝 提交于 2019-12-08 23:09:25

You need to create an Img to return. JfreeChart is not an image.

Also, you need to create a single controller to return the image. You cannot return the image in a normal render result.

So you'd have a controller like, renderChart(String chartId){ .... make the chart ... renderBinary(img); }

Here code that creates an image from JFreeChart (from http://www.jfree.org/phpBB2/viewtopic.php?t=113 )

JFreeChart chart = createChart(); 
... 
BufferedImage img = draw( chart, width, height ); 
OutputStream out = response.getOutputStream(); 
JPEGImageEncoder encoder = 
JPEGCodec.createJPEGEncoder(out); 
JPEGEncodeParam param = 
encoder.getDefaultJPEGEncodeParam(img); 
param.setQuality(1.0f,true); 
encoder.encode(img,param); 
out.close(); 

protected BufferedImage draw(JFreeChart chart, int width, int height) 
{ 
BufferedImage img = 
new BufferedImage(width , height, 
BufferedImage.TYPE_INT_RGB); 
Graphics2D g2 = img.createGraphics(); 
chart.draw(g2, new Rectangle2D.Double(0, 0, width, height)); 
g2.dispose(); 
return img; 
} 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!