今天用JFreeChar整了一下报表,感觉还不错,先整理如下:
具体效果见上传的图片;
代码如下:
1 前台页面:
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> 请选择要生成的图形报表<br> <select id='charid' onchange="makeChar()"> <option value="0">请选择一种图表</option> <option value="Pie">PieChart</option> <option value="Bar">BarChart3D</option> <option value="Line">LineChart</option> </select> <div id="displayDiv"></div> <script type="text/javascript" language="javascript"> function makeChar(){ var charType=document.getElementById("charid").value; if(charType==0){ return; }else{ var imgdiv=document.getElementById("displayDiv"); imgdiv.innerHTML=""; var img=document.createElement("img"); // img.setAttribute("src","servlet/ChartServlet?chartType="+charType); img.setAttribute("src","myCharServlet?charType="+charType); //imageZone.setAttribute('src',"servlet/ChartServlet?chartType="+cType); imgdiv.appendChild(img); } } </script>
后台代码:
package cn.netjava.jFreeChart; import java.io.IOException; import java.io.OutputStream; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.data.general.DefaultPieDataset; public class MyCharServlet extends HttpServlet { protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //super.service(arg0, arg1); System.out.println("到了myCharServlet了"); //response.setContentType("image/png;charset=UTF-8"); response.setHeader("Cache-Control", "no-cache"); response.setContentType("image/png"); String chartType=request.getParameter("charType").trim(); JFreeChart jFreeChart=getCharByType(chartType); if(jFreeChart!=null){ OutputStream os=response.getOutputStream(); ChartUtilities.writeChartAsPNG(os, jFreeChart, 500, 300); os.flush(); os.close(); } } public JFreeChart getCharByType(String charType){ JFreeChart jfChart=null; if(charType.equalsIgnoreCase("pie")){ //饼图 DefaultPieDataset dataSet=new DefaultPieDataset(); dataSet.setValue("java", new Integer(55)); dataSet.setValue("c++", new Integer(30)); dataSet.setValue("delphi", new Integer(15)); //DefaultPieDataset dataSet= getPieDataSet(); jfChart=ChartFactory.createPieChart("各语言使用人数统计表", dataSet, true, false, false); }else if(charType.equalsIgnoreCase("line")){ DefaultCategoryDataset dataSet=new DefaultCategoryDataset(); String groups[]={"曼联","利物浦","阿森纳"}; String round[]={"第一轮","第二轮","第三轮","第四轮","第五轮","第六轮"}; for (int i = 0; i < round.length; i++) { for (int j = 0; j < groups.length; j++) { dataSet.setValue(new java.util.Random().nextInt(5),groups[j] , round[i]); } } jfChart=ChartFactory.createLineChart("08/09赛季英超球队积分榜(前六轮)", "球队", "排名", dataSet, PlotOrientation.VERTICAL, true, false, false); }else if(charType.equalsIgnoreCase("bar")){ String citys[]={"北京","上海","广州"}; String months[]={"一月","二月","三月"}; DefaultCategoryDataset dataset=new DefaultCategoryDataset(); for (int i = 0; i < months.length; i++) { for (int j = 0; j < citys.length; j++) { dataset.setValue(new java.util.Random().nextInt(1000), citys[j], months[i]); } } jfChart=ChartFactory.createBarChart3D("2009年中国三大城市降水量表(第一季度)", "城市", "降水量", dataset, PlotOrientation.VERTICAL, true, false, false); } return jfChart; } public static DefaultPieDataset getPieDataSet() { // 用来填充pie图的数据集 DefaultPieDataset dataset = new DefaultPieDataset(); // 饼图分块名字 String[] seriesNames = { "芙蓉路", "八一路", "五一路", "大学路", }; for (int i = 0; i < seriesNames.length; i++) { int value = new java.util.Random().nextInt(1000) + 500; // 设置pie数据集中的数据对 dataset.setValue(seriesNames[i], value); } return dataset; } }