How to render JFreeChart in Play framework

有些话、适合烂在心里 提交于 2020-01-03 03:55:27

问题


Problem: does not allow me to renderBinary. The method renderBinary(InputStream) in the type Controller is not applicable for the arguments (JFreeChart).

I have this controller:

public static void index() {

    // create a dataset...Default

    DefaultPieDataset dataset = new DefaultPieDataset();

    dataset.setValue("Category 1", 43.2);
    dataset.setValue("Category 2", 27.9);
    dataset.setValue("Category 3", 79.5);

    // create a chart...
    JFreeChart chart = ChartFactory.createPieChart(
            "Sample",
            dataset,
            true,
            true,
            false
    );

    List<User> usersA = Department.getUsersA();
    List<User> usersB = Department.getUsersB();


    render(usersA, usersB, chart);

}

回答1:


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


来源:https://stackoverflow.com/questions/10028579/how-to-render-jfreechart-in-play-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!