建立一個web 工程,發布到tomcat
建立一個Servlet,配置自動。
Servlet代碼:
import java.awt.Color;import java.awt.Font;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import javax.servlet.ServletException;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.axis.CategoryAxis;import org.jfree.chart.axis.ValueAxis;import org.jfree.chart.labels.ItemLabelAnchor;import org.jfree.chart.labels.ItemLabelPosition;import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;import org.jfree.chart.plot.CategoryPlot;import org.jfree.chart.plot.PlotOrientation;import org.jfree.chart.renderer.category.BarRenderer3D;import org.jfree.data.category.CategoryDataset;import org.jfree.data.general.DatasetUtilities;import org.jfree.ui.TextAnchor;/** * Servlet implementation class EmployeeCommServlet */public class EmployeeCommServlet extends HttpServlet {private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doPost(request, response);}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.setCharacterEncoding("utf-8");try{double[][] data = new double[][] {{1320}, {720}, {830}, {400}, {1000}, {1500}}; String[] rowKeys = {"201201", "201202", "201203", "201204", "201205", "201206"}; String[] columnKeys = {""}; CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data); JFreeChart chart = ChartFactory.createBarChart3D("2012上半年傭金", "年月","傭金",dataset, PlotOrientation.VERTICAL, true, false, false); //設定標題字型,可以處理亂碼問題chart.getTitle().setFont(new Font("宋體", Font.BOLD,12));CategoryPlot plot = chart.getCategoryPlot(); //X軸CategoryAxis domainAxis = plot.getDomainAxis();//設定設定Y軸上的文字 domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));//設定Y軸的標題文字domainAxis.setLabelFont(new Font("宋體", Font.PLAIN, 12)); //Y軸ValueAxis numberaxis = plot.getRangeAxis();//設定設定Y軸上的文字 numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12)); //設定Y軸的標題文字 numberaxis.setLabelFont(new Font("黑體", Font.PLAIN, 12)); //這句代碼解決了底部漢字亂碼的問題 chart.getLegend().setItemFont(new Font("宋體", Font.PLAIN, 12)); //設定網格背景顏色 plot.setBackgroundPaint(Color.white); //設定網格豎線顏色 plot.setDomainGridlinePaint(Color.pink); //設定網格橫線顏色 plot.setRangeGridlinePaint(Color.pink); //顯示每個柱的數值,並修改該數值的字型屬性 BarRenderer3D renderer = new BarRenderer3D(); renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); renderer.setBaseItemLabelsVisible(true); //預設的數字顯示在柱子中,通過如下兩句可調整數位顯示 //注意:此句很關鍵,若無此句,那數位顯示會被覆蓋,給人數字沒有顯示出來的問題 renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT)); renderer.setItemLabelAnchorOffset(10D); //設定每個月份所包含的多種傭金柱的之間距離,如果每個月需要分別顯示多種傭金的話 //renderer.setItemMargin(0.4); plot.setRenderer(renderer); int width = 600;int height= 400;String fileName = "comm.jpeg";String url = request.getSession().getServletContext().getRealPath("\\");System.out.println(url); OutputStream os = new FileOutputStream(url+fileName); ChartUtilities.writeChartAsJPEG(os, chart, width, height); os.flush(); if(os != null) os.close(); request.setAttribute("pic", fileName); request.setAttribute("width", width); request.setAttribute("height", height); request.getRequestDispatcher("/employee.jsp").forward(request, response); }catch(Exception e){e.printStackTrace();}}}
JSP:
<%@ page language="java" contentType="text/html; charset=gbk" pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><title>Employee Commission</title></head><body><img src="<%=request.getAttribute("pic") %>" width="<%=request.getAttribute("width") %>" height="<%=request.getAttribute("height") %>" ><p><a href="http://lapulande.iteye.com/blog/847961" target="_blank">參考文檔一</a><br><br><a href="http://www.jfree.org/jfreechart/" target="">JFree官方學習網</a><br><a href="http://www.jfree.org/jfreechart/api/javadoc/index.html" target="">JFree官方API</a><br><a href="http://www.jfree.org/jfreechart/" target="">JFree官方學習網</a><br>不懂多上網查</p><br><a href="CommDetailServlet" >詳細圖</a></body></html>
完事了。