如何在Play框架中呈现JFreeChart

如何在Play框架中呈现JFreeChart

本文介绍了如何在Play框架中呈现JFreeChart的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

问题:不允许我renderBinary.类型为Controller的方法renderBinary(InputStream)不适用于参数(JFreeChart).

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

我有这个控制器:

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

}

推荐答案

您需要创建一个Img才能返回. JfreeChart不是图像.

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.

因此,您将拥有一个像renderChart(String chartId){....这样的控制器. }

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

此处的代码可从JFreeChart创建图像(来自 http://www. jfree.org/phpBB2/viewtopic.php?t=113 )

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

这篇关于如何在Play框架中呈现JFreeChart的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 22:58