使用JFreeChart制作图形
第一步:下载相应的组件
JFreeChart下载:http://www.jfree.org/jfreechart/download.html
第二步:解压到相应的目录
F:\Program Files\jfreechart-1.0.14\jfreechart-1.0.14-demo.jar
第三步:引入相关的jar包
jcommon-1.0.17.jar
jfreechart-1.0.14.jar
WEB-INF――lib目录下引入上面的两个包
第四步:编写数据处理文件
package yuan.rui.love.servlet; import java.awt.Font; import java.io.FileOutputStream; import java.io.IOException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.ServletException; 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.StandardChartTheme; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.category.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.data.general.DefaultPieDataset; import yuan.rui.love.util.DB; /** * Servlet implementation class for Servlet: ShowProductSales * */ public class ShowProductSales extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { static final long serialVersionUID = 1L; /* (non-Java-doc) * @see javax.servlet.http.HttpServlet#HttpServlet() */ public ShowProductSales() { super(); } /* (non-Java-doc) * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { getDataSet(); JFreeChart category_chart = ChartFactory.createBarChart3D( "产品销量图", "产品名", "销量", categoryDataset, PlotOrientation.VERTICAL, true, false, false); JFreeChart pie_chart = ChartFactory.createPieChart( "产品销量图", pieDataset, true, true, false); FileOutputStream category_jpg = null; FileOutputStream pie_jpg = null; try { category_jpg = new FileOutputStream( "D:\\eclipse_java_ Programs\\Shopping\\WebContent\\admin\\images\\reports\\productsales.jpg"); ChartUtilities.writeChartAsJPEG(category_jpg,1.0f,category_chart,400,300,null); pie_jpg = new FileOutputStream( "D:\\eclipse_java_ Programs\\Shopping\\WebContent\\admin\\images\\reports\\productsales_pie.jpg"); ChartUtilities.writeChartAsJPEG(pie_jpg,1.0f,pie_chart,400,300,null); request.getRequestDispatcher("admin/showproductsales.jsp").forward(request, response); } finally { try { pie_jpg.close(); category_jpg.close(); } catch(Exception e) {} } } /* (non-Java-doc) * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } private DefaultCategoryDataset categoryDataset = null; private DefaultPieDataset pieDataset = null; public void getDataSet() { categoryDataset = new DefaultCategoryDataset(); pieDataset = new DefaultPieDataset(); //dataset.addValue(100, "北京", "苹果"); Connection conn = null; ResultSet rs = null; try { conn = DB.getConn(); String sql = "select p.name , sum(pcount) from product p join salesitem si on (p.id = si.productid) group by p.id"; rs = DB.getResultSet(conn, sql); 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); while(rs.next()) { categoryDataset.addValue(rs.getInt(2), "", rs.getString(1)); pieDataset.setValue(rs.getString(1),rs.getDouble(2)); } } catch (SQLException e) { e.printStackTrace(); } finally { DB.close(rs); DB.close(conn); } } }
第五步:应用以上程序进行数据处理,绘制图形,存储到相应路径,并在网页上展示
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>Insert title here