📄 elec_jfreechart.java
字号:
package imis_elec;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.servlet.ServletUtilities;
public class Elec_JFreeChart extends HttpServlet {
/**
* Constructor of the object.
*/
public Elec_JFreeChart() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String displayType = request.getParameter("displayType");
String userId = request.getParameter("userId");
String startDate = request.getParameter("startDate");
String endDate = request.getParameter("endDate");
String type = request.getParameter("type");
String title = "";
String y = ""; // y轴标题
String url = "../imis_elec/elec_JFree.jsp";
String organidof = request.getParameter("OrganOf");
//System.out.println(organidof);
if(type.equals("1")) {
title = "电量构成";
y = "电量";
} else {
title = "价格构成";
y = "价格";
}
String filename = "";
//HashMap table = UsedByJFree.getTongjiTu(userId, startDate, endDate, type);
HashMap table = UsedByJFree.getTongjiTu(userId, startDate, endDate, type , organidof);
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
try {
if(displayType.equals("pie")) { //面积比例饼图 //500是图片长度,300是图片高度
filename = ServletUtilities.saveChartAsPNG(UsedByJFree.getchart(table,1,title,y, null, null, null), 500, 300, info, request.getSession());
} else if(displayType.equals("bar")) { //柱状图
filename = ServletUtilities.saveChartAsPNG(UsedByJFree.getchart(table,2,title,y, null, null, null), 500, 300, null, request.getSession());
} else if(displayType.equals("line")) {
filename = ServletUtilities.saveChartAsPNG(UsedByJFree.getchart(table,3,title,y, null, null, null), 500, 300, null, request.getSession());
}
else if(displayType.equals("duibi")) {
startDate = startDate.split("-")[0] + "-1";
endDate = String.valueOf(Integer.parseInt(endDate.split("-")[0]) + 1) + "-1";
//HashMap lastYear = UsedByJFree.getTongjiTuLastYear(userId, startDate, endDate, type);
HashMap lastYear = UsedByJFree.getTongjiTuLastYear(userId, startDate, endDate, type , organidof);
filename = ServletUtilities.saveChartAsPNG(UsedByJFree.getchart(table,2,title,y, lastYear, startDate, endDate), 800, 400, null, request.getSession());
url = "../imis_elec/elec_JFreeDisplay.jsp";
}
} catch (Exception e) {
e.printStackTrace();
}
String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;
request.setAttribute("graphURL", graphURL);
request.getRequestDispatcher(url).forward(request, response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -