[英]Updating JFreeChart displayed as GraphicImage with PrimeFaces
我正在使用PrimeFaces開發Webapp。 我非常接近解決方案,但是在計算新數據時一直嘗試更新圖表。 我有3個標簽:“輸入”,“輸出”和“圖表”。 在輸入選項卡上,我有文本框和提交按鈕。 在“輸出”選項卡上,我有一個數據圖表,用於顯示輸入輸入並單擊提交后計算出的數據。 我已驗證數據已正確填充。 在“圖表”選項卡上,我有一個圖表(以后還會有多個),該圖表也將顯示來自計算的數據。
這是我的相關代碼:
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);
}
}
}
另外,在我的index.html中:
<p:tab title="Charts">
<h:panelGrid columns="2" cellpadding="10">
<p:graphicImage id="outputChart1" value="#{dataChart.chartContent}" />
</h:panelGrid>
</p:tab>
為了完整起見,這來自DataChart.java:
public StreamedContent getChartContent() {
if (chartData != null && !chartData.isEmpty() && calculateValuesServlet.isNewChartNeeded()) {
createNewChart();
}
return chartContent;
}
我已經開始將圖表保存到Desktop \\ Temp,以便可以查看圖表。 在我的Temp文件夾中創建的文件是正確的,但在Webapp上顯示的文件是錯誤的。 我還嘗試在graphicImage中設置cache =“ FALSE”,但隨后得到的是殘破的圖像圖標,而不是圖形。
我意識到這告訴我Webapp沒有得到最新圖像,但是為什么呢?
我在圖片上也遇到問題,無法刷新。
嘗試將范圍更改為@RequestScoped並在p:graphicImage上添加cache =“ false”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.