[英]JFREECHART with servlet
我正在嘗試讓JFREECHART為圖表中的一些硬編碼數據創建png。 我完全沒有錯誤,我在創建部分中運行的程序也沒有,但我沒有得到png。
誰能看到問題出在哪里?
我的servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("Action");
if(action.equals("Home"))
getServletContext().getRequestDispatcher(home).forward(request, response);
else{
session = request.getSession(true);
user = (User) session.getAttribute("User");
eventList = rh.getEventList(user);
request.setAttribute("EventList", eventList);
getServletContext().getRequestDispatcher(result).forward(request, response);
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.setValue(30, "Girls","SCIENCE CLASS");
dataset.setValue(30, "Boys","SCIENCE CLASS");
dataset.setValue(10, "Girls","ECONOMICS CLASS");
dataset.setValue(50, "Boys","ECONOMICS CLASS");
dataset.setValue(5, "Girls","LANGUAGE CLASS");
dataset.setValue(55, "Boys","LANGUAGE CLASS");
JFreeChart chart = ChartFactory.createLineChart(
"Comparison between Girls and Boys in Science," + "Economics and Language classes",
"Students Comparisons", "No of Students",
dataset,
PlotOrientation.VERTICAL,
true,
true,
false);
try {
final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
final File file1 = new File(getServletContext().getRealPath(".") + "/images/lineChart.png");
ChartUtilities.saveChartAsPNG(file1, chart, 600, 400, info);
} catch (Exception e) {
System.out.println(e);
}
}
}
和我的jsp
<div>
<img src="path/images/lineChart.png"
width="600" height="400" border="0" usemap="#chart" />
</div>
為此,必須在呈現JSP之前調用Servlet。 但是,您采用的方法還有其他缺陷。
我建議不要將生成的圖表寫到文件中,而應將其直接流到Servlet的響應流中:
try {
final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
ChartUtilities.writeChartAsPNG(response, chart, 600, 400, info);
} catch (Exception e) {
System.out.println(e);
}
然后,只需將<img/>
標記指向Servlet:
<div>
<img src="pathToMyServlet" width="600" height="400" border="0" usemap="#chart" />
</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.