在支持bean中生成Primefaces Charts的png/jpeg图像?

Wen*_*ing 2 charts primefaces jsf-2 backing-beans

我在我的应用程序中使用Primefaces 3.1.1图表,在JSF页面中生成图表没有问题,但我试图找出是否可以为图表生成图像(png或jpeg)以便我可以插入这些图像到java中的Excel文件(Apache POI).

我知道最新的Primefaces版本3.4.1具有导出图表功能,但生成的图像仅发生在客户端(它是jqPlot).但我需要在服务器端.

目前我们在支持bean中使用jFreeChart用于此目的,因此浏览器中的图表与Excel中的图表看起来非常不同.我们试图找出升级到Primefaces 3.4.1是否可以让我们选择在浏览器中制作图表并且Excel中的图表看起来是一样的?还是有另一种方法吗?

如果这是一个问题,使用mojarra-2.1.3-FCS.

Wen*_*ing 6

正如Daniel所接受的答案一样,Primefaces的图表在服务器端不可用.我在这里添加答案只是为了显示可能的解决方法.

在客户端,我们将base64 PNG编码的字符串分配给隐藏字段值,这是从Primefaces导出图表的源代码修改的示例:

<h:form id="hform">
    <p:lineChart value="#{testBean.linearModel}" legendPosition="e"
        zoom="true" title="Linear Chart" minY="0" maxY="10"
        style="width:500px;height:300px" widgetVar="chart" />
    <p:commandButton id="exp" value="Export" icon="ui-icon-extlink"
        onclick="exportChart();"
        actionListener="#{testBean.submittedBase64Str}" />
    <h:inputHidden id="b64" value="#{testBean.base64Str}" />
    <script type="text/javascript">
        function exportChart() {
        // exportAsImage() will return a base64 png encoded string
        img = chart.exportAsImage();
        document.getElementById('hform:b64').value = img.src;
        }
    </script>
</h:form>
Run Code Online (Sandbox Code Playgroud)

在支持bean,我们需要解码字符串,一个简单的例子如下:

public void submittedBase64Str(ActionEvent event){
    // You probably want to have a more comprehensive check here. 
    // In this example I only use a simple check
    if(base64Str.split(",").length > 1){
        String encoded = base64Str.split(",")[1];
        byte[] decoded = org.apache.commons.codec.binary.Base64.decodeBase64(encoded);
        // Write to a .png file
        try {
            RenderedImage renderedImage = ImageIO.read(new ByteArrayInputStream(decoded));
            ImageIO.write(renderedImage, "png", new File("C:\\out.png")); // use a proper path & file name here.
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

PNG文件现在存储在服务器中,您可以继续在代码的其他部分使用该文件.