简体   繁体   中英

Updating JFreeChart displayed as GraphicImage with PrimeFaces

I am working on a Webapp using PrimeFaces. 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. 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:

<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:

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. The ones that are created in my Temp folder are correct, but the one that appears on the webapp is not. I also tried setting cache="FALSE" in the graphicImage, but then I get a broken image icon instead of the graph.

I realize this tells me that the webapp is not getting that latest image, but why?

I'm facing problem on image not refresh too.

Try to change the scope to @RequestScoped and add in cache="false" on p:graphicImage

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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