简体   繁体   English

使用PrimeFaces更新显示为GraphicImage的JFreeChart

[英]Updating JFreeChart displayed as GraphicImage with PrimeFaces

I am working on a Webapp using PrimeFaces. 我正在使用PrimeFaces开发Webapp。 I am very close to a solution, but am stuck with trying to update the chart as new data is calculated. 我非常接近解决方案,但是在计算新数据时一直尝试更新图表。 I have 3 tabs: Inputs, Outputs & Charts. 我有3个标签:“输入”,“输出”和“图表”。 On the Inputs Tab, I have textboxes and a Submit button. 在输入选项卡上,我有文本框和提交按钮。 On the Outputs Tab, I have a datachart to display the data that is calculated after inputs are entered and submit is clicked. 在“输出”选项卡上,我有一个数据图表,用于显示输入输入并单击提交后计算出的数据。 I have verified that data is populated correctly. 我已验证数据已正确填充。 On the Charts Tab, I have a chart (will be multiple later) that is to display data from the calculation as well. 在“图表”选项卡上,我有一个图表(以后还会有多个),该图表也将显示来自计算的数据。

Here is my relevant code: 这是我的相关代码:

public void createNewChart() {

    FileInputStream fis = null;
    try {
        //Graphic Text
        BufferedImage bufferedImg = new BufferedImage(100, 25, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = bufferedImg.createGraphics();
        g2.drawString("This is a text", 0, 10);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(bufferedImg, "png", os);
        graphicText = new DefaultStreamedContent(new ByteArrayInputStream(os.toByteArray()), "image/png");

        //Chart
        File chartFile = new File("C:\\Desktop\\Temp", "Chart" + Math.round(Math.random() * 1000000) + ".png");
        chartData = calculateValuesServlet.getChartData();
        calculateDataSets();

        XYSeriesCollection xyDataset = new XYSeriesCollection();
        xyDataset.addSeries(minumumLine);
        xyDataset.addSeries(maximumLine);
        xyDataset.addSeries(optimumLine);
        xyDataset.addSeries(ratingPoint);
        chart = ChartFactory.createXYLineChart("Chart Title", xAxis.getAxisLabel(), yAxis.getAxisLabel(), xyDataset, PlotOrientation.VERTICAL, true, false, false);
        fixRenderings();

        ChartUtilities.saveChartAsPNG(chartFile, chart, 375, 300);
        fis = new FileInputStream(chartFile);
        chartContent = new DefaultStreamedContent(fis, "image/png");
        calculateValuesServlet.setNewChartNeeded(false);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
        } catch (IOException ex) {
            Logger.getLogger(DataChart.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

Also, in my index.html: 另外,在我的index.html中:

<p:tab title="Charts">
    <h:panelGrid columns="2" cellpadding="10">
        <p:graphicImage  id="outputChart1" value="#{dataChart.chartContent}" />
    </h:panelGrid>
</p:tab>

And for completeness, this is from DataChart.java: 为了完整起见,这来自DataChart.java:

public StreamedContent getChartContent() {
    if (chartData != null && !chartData.isEmpty() && calculateValuesServlet.isNewChartNeeded()) {
        createNewChart();
    }
    return chartContent;
}

I have started saving the charts to Desktop\\Temp so that I can view the charts. 我已经开始将图表保存到Desktop \\ Temp,以便可以查看图表。 The ones that are created in my Temp folder are correct, but the one that appears on the webapp is not. 在我的Temp文件夹中创建的文件是正确的,但在Webapp上显示的文件是错误的。 I also tried setting cache="FALSE" in the graphicImage, but then I get a broken image icon instead of the graph. 我还尝试在graphicImage中设置cache =“ FALSE”,但随后得到的是残破的图像图标,而不是图形。

I realize this tells me that the webapp is not getting that latest image, but why? 我意识到这告诉我Webapp没有得到最新图像,但是为什么呢?

I'm facing problem on image not refresh too. 我在图片上也遇到问题,无法刷新。

Try to change the scope to @RequestScoped and add in cache="false" on p:graphicImage 尝试将范围更改为@RequestScoped并在p:graphicImage上添加cache =“ false”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM