JFreeChart spring web開發

在web.xml中加入

  <!-- jfreechart報表配置 -->
    <servlet>
        <servlet-name>DisplayChart</servlet-name>
        <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>DisplayChart</servlet-name>
        <url-pattern>/img</url-pattern>
</servlet-mapping>


在方法中寫:

@RequestMapping(value = "/test", produces = "text/html;charset=GBK;")

public String beanToXmlMethod(Model model, HttpServletResponse response,
HttpServletRequest request) throws IOException {

DefaultPieDataset dpd = new DefaultPieDataset(); // 建立一個默認的餅圖
dpd.setValue("程序員", 25); // 輸入數據
dpd.setValue("初級工程師", 30);
dpd.setValue("中級工程師", 35);
dpd.setValue("高級工程師", 40);
dpd.setValue("架構師", 50);
//設置字體爲中文
StandardChartTheme standardChartTheme = new StandardChartTheme("CN");
// 設置標題字體
standardChartTheme.setExtraLargeFont(new Font("隸書", Font.BOLD, 20));
// 設置圖例的字體
standardChartTheme.setRegularFont(new Font("宋書", Font.PLAIN, 15));
// 設置軸向的字體
standardChartTheme.setLargeFont(new Font("宋書", Font.PLAIN, 15));
ChartFactory.setChartTheme(standardChartTheme);
// 可以查具體的API文檔,第一個參數是標題,第二個參數是一個數據集,第三個參數表示是否顯示Legend,第四個參數表示是否顯示提示,第五個參數表示圖中是否存在URL
JFreeChart chart = ChartFactory.createPieChart("開發層次結構圖", dpd, true, true, false);
String fileName = ServletUtilities.saveChartAsJPEG(chart, 600, 500, request.getSession());
String chartURL = request.getContextPath() + "/chart?filename=" + fileName;

model.addAttribute("img",charURL);

return "index";

}



頁面:<img src="${img}" />   //前面設置過圖片大小了,也可以自己設置

發佈了50 篇原創文章 · 獲贊 23 · 訪問量 19萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章