📄 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 + -