?? reportaction.java
字號:
package com.yuanchung.sales.struts.report.action;
import java.awt.Color;
import java.awt.Font;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
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.BarRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.renderer.category.StandardBarPainter;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.TextAnchor;
import com.yuanchung.sales.service.resport.ReportMgr;
/**
* 報表Action
* @author gzq
*
*/
public class ReportAction extends DispatchAction {
private static final Log log = LogFactory.getLog(ReportCategoryAction.class);
private ReportMgr reportMgr;
/**
* 呈現方式枚舉
* @author gzq
*
*/
private enum Type {
REPORT(1),//報表
DASHBOARD(2);//儀表板
private final int value;
Type(int value){
this.value = value;
}
/**
* 獲得特定枚舉類型的值
* @return 枚舉類型的值
*/
public int getValue(){
return this.value;
}
}
public void setReportMgr(ReportMgr reportMgr) {
this.reportMgr = reportMgr;
}
/**
* 查看新增客戶數報表或儀表板
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward viewNewCreatedCustomer(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String startDate = request.getParameter("startDate");
String endDate = request.getParameter("endDate");
String dateCondition = request.getParameter("dateCondition");
String strWidth = request.getParameter("width");
String strHeight = request.getParameter("height");
int width = 388;
int height = 290;
if(strWidth != null && !"".equals(strWidth)){
width = Integer.parseInt(strWidth);
}
if(strHeight != null && !"".equals(strHeight)){
height = Integer.parseInt(strHeight);
}
List lst = reportMgr.getNewCreatedCustomerCount(startDate, endDate);
int recordsTotal = 0;
if(lst != null && lst.size() > 0){
for(Object obj : lst.toArray()){
Object[] objs = (Object[])obj;
recordsTotal += Integer.parseInt(objs[1].toString());
}
}
String strType = request.getParameter("type");
int type = 1;
if(strType != null){
type = Integer.parseInt(strType);
}
if(type == Type.REPORT.getValue()){
request.setAttribute("newCreatedCustomers", lst);
request.setAttribute("newCreatedCustomerCount", recordsTotal);
request.setAttribute("dateCondition", dateCondition);
return mapping.findForward("toReportMain");
}else if(type == Type.DASHBOARD.getValue()){
Font font = new Font("黑體", Font.PLAIN, 13);
DefaultCategoryDataset categoryDS = new DefaultCategoryDataset();
if(lst != null && lst.size() > 0){
for(Object obj : lst.toArray()){
Object[] objs = (Object[])obj;
categoryDS.addValue(Integer.parseInt(objs[1].toString()),"客戶數量",objs[0].toString());
}
}
JFreeChart jfc = getBarChart(categoryDS, new Color(42,170,255), "新增客戶數", "月份", "客戶數量");
response.setContentType("image/png");
ChartUtilities.writeChartAsPNG(response.getOutputStream(), jfc, width, height);
return null;
}else{
return null;
}
}
/**
* 查看已忽視的客戶數報表或儀表板
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward viewIgnoredCustomer(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String startDate = request.getParameter("startDate");
String endDate = request.getParameter("endDate");
String dateCondition = request.getParameter("dateCondition");
String strWidth = request.getParameter("width");
String strHeight = request.getParameter("height");
int width = 388;
int height = 290;
if(strWidth != null && !"".equals(strWidth)){
width = Integer.parseInt(strWidth);
}
if(strHeight != null && !"".equals(strHeight)){
height = Integer.parseInt(strHeight);
}
List lst = reportMgr.getIgnoredCustomerCount(startDate, endDate);
int recordsTotal = 0;
if(lst != null && lst.size() > 0){
for(Object obj : lst.toArray()){
Object[] objs = (Object[])obj;
recordsTotal += Integer.parseInt(objs[1].toString());
}
}
String strType = request.getParameter("type");
int type = 1;
if(strType != null){
type = Integer.parseInt(strType);
}
if(type == Type.REPORT.getValue()){
request.setAttribute("ignoredCustomers", lst);
request.setAttribute("ignoredCustomerCount", recordsTotal);
request.setAttribute("dateCondition", dateCondition);
return mapping.findForward("toViewIgnoredCustomers");
}else if(type == Type.DASHBOARD.getValue()){
DefaultCategoryDataset categoryDS = new DefaultCategoryDataset();
if(lst != null && lst.size() > 0){
for(Object obj : lst.toArray()){
Object[] objs = (Object[])obj;
categoryDS.addValue(Integer.parseInt(objs[1].toString()),"客戶數量",objs[0].toString());
}
}
JFreeChart jfc = getBarChart(categoryDS, Color.orange, "已忽視的客戶數", "上次活動月份", "客戶數量");
response.setContentType("image/png");
ChartUtilities.writeChartAsPNG(response.getOutputStream(), jfc, width, height);
return null;
}else{
return null;
}
}
/**
* 查看最近聯系的客戶數報表或儀表板
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward viewRecentlyContactCustomer(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String startDate = request.getParameter("startDate");
String endDate = request.getParameter("endDate");
String dateCondition = request.getParameter("dateCondition");
String strWidth = request.getParameter("width");
String strHeight = request.getParameter("height");
int width = 388;
int height = 290;
if(strWidth != null && !"".equals(strWidth)){
width = Integer.parseInt(strWidth);
}
if(strHeight != null && !"".equals(strHeight)){
height = Integer.parseInt(strHeight);
}
List lst = reportMgr.getRecentlyContactCustomerCount(startDate, endDate);
int recordsTotal = 0;
if(lst != null && lst.size() > 0){
for(Object obj : lst.toArray()){
Object[] objs = (Object[])obj;
recordsTotal += Integer.parseInt(objs[1].toString());
}
}
String strType = request.getParameter("type");
int type = 1;
if(strType != null){
type = Integer.parseInt(strType);
}
if(type == Type.REPORT.getValue()){
request.setAttribute("customers", lst);
request.setAttribute("customerCount", recordsTotal);
request.setAttribute("dateCondition", dateCondition);
return mapping.findForward("toViewRecentlyContactCustomers");
}else if(type == Type.DASHBOARD.getValue()){
DefaultCategoryDataset categoryDS = new DefaultCategoryDataset();
if(lst != null && lst.size() > 0){
for(Object obj : lst.toArray()){
Object[] objs = (Object[])obj;
categoryDS.addValue(Integer.parseInt(objs[1].toString()),"客戶數量",objs[0].toString());
}
}
JFreeChart jfc = getLineChart(categoryDS, Color.yellow, "最近聯系的客戶數", "上次活動月份", "客戶數量");
response.setContentType("image/png");
ChartUtilities.writeChartAsPNG(response.getOutputStream(), jfc, width, height);
return null;
}else{
return null;
}
}
/**
* 獲得柱狀圖
* @param ds 柱狀圖數據集
* @param barColor 柱子的顏色
* @param title 柱狀圖標題
* @param xAxisLabel X軸標簽
* @param yAxisLabel Y軸標簽
* @return 柱狀圖實例
*/
public JFreeChart getBarChart(CategoryDataset ds, Color barColor, String title, String xAxisLabel, String yAxisLabel){
Font font = new Font("黑體", Font.PLAIN, 13);
JFreeChart jfc = ChartFactory.createBarChart(title, xAxisLabel, yAxisLabel, ds, PlotOrientation.VERTICAL, true, true, false);
jfc.getTitle().setFont(new Font("黑體", Font.PLAIN, 14));
jfc.getLegend().setItemFont(font);
CategoryPlot cp = (CategoryPlot)jfc.getPlot();
cp.getDomainAxis().setLabelFont(font);//x軸標簽的字體
cp.getDomainAxis().setTickLabelFont(font);//x軸上的刻度的標簽的字體
BarRenderer br = (BarRenderer)cp.getRenderer();
br.setBarPainter(new StandardBarPainter());//設置用普通樣式繪制柱子(非高亮)
br.setSeriesPaint(0, barColor != null ? barColor : Color.orange);//設置柱子的顏色
br.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());//設置柱子的標簽生成器
br.setBaseItemLabelFont(font);//設置柱子的標簽的字體
br.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.HALF_ASCENT_CENTER));//設置柱子的標簽的位置
br.setBaseItemLabelsVisible(true);//顯示柱子的標簽
br.setShadowVisible(false);//隱藏陰影
NumberAxis na = (NumberAxis)cp.getRangeAxis();
na.setLabelFont(font);//y軸標簽的字體
na.setStandardTickUnits(NumberAxis.createIntegerTickUnits());//y軸的數字以整數的形式顯示(人數不允許帶小數點)
return jfc;
}
/**
* 獲得線圖
* @param ds 數據集
* @param lineColor 線的顏色
* @param title 線圖標題
* @param xAxisLabel X軸標簽
* @param yAxisLabel Y軸標簽
* @return 線圖實例
*/
public JFreeChart getLineChart(CategoryDataset ds, Color lineColor, String title, String xAxisLabel, String yAxisLabel){
Font font = new Font("黑體", Font.PLAIN, 13);
JFreeChart jfc = ChartFactory.createLineChart(title, xAxisLabel, yAxisLabel, ds, PlotOrientation.VERTICAL, true, true, false);
jfc.getTitle().setFont(new Font("黑體", Font.PLAIN, 14));
jfc.getLegend().setItemFont(font);
CategoryPlot cp = (CategoryPlot)jfc.getPlot();
cp.getDomainAxis().setLabelFont(font);//x軸標簽的字體
cp.getDomainAxis().setTickLabelFont(font);//x軸上的刻度的標簽的字體
LineAndShapeRenderer rd = (LineAndShapeRenderer)cp.getRenderer();
rd.setSeriesPaint(0, lineColor != null ? lineColor : Color.blue);
rd.setBaseShapesVisible(true);
rd.setBaseItemLabelsVisible(true);
rd.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
rd.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE11, TextAnchor.HALF_ASCENT_CENTER));//設置線的節點的標簽的位置
NumberAxis na = (NumberAxis)cp.getRangeAxis();
na.setLabelFont(font);//y軸標簽的字體
na.setStandardTickUnits(NumberAxis.createIntegerTickUnits());//y軸的數字以整數的形式顯示(人數不允許帶小數點)
return jfc;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -