Spring mvc-jfreeChart柱图(2)

上一篇中生成的图是静态的,这篇将按条件进行搜索,并统计成图表,左面为统计图,右面显示搜索出的结果。
第一步:导包
第二步;配置web.xml(上一篇有代码)

建BarRenderer类用于柱子颜色

import java.awt.Color;
import java.awt.Paint;
import org.jfree.chart.renderer.category.BarRenderer3D;

public class BarRenderer extends BarRenderer3D {
private static final long serialVersionUID = 784630226449158436L; 
    private Paint[] colors; 
    //初始化柱子颜色 
    private String[] colorValues = { "#AAA8F8", "#F6BD0F", "#8BBA00", "#FF8E46", "#008E8E", "#D98628",
    "#ABD8F8", "#F9CD0F", "#1ABA00", "#BB8E46", "#009A8B", "#C08628" }; 
 
    public BarRenderer() { 
        colors = new Paint[colorValues.length]; 
        for (int i = 0; i < colorValues.length; i++) { 
            colors[i] = Color.decode(colorValues[i]); 
        } 
    } 
 
    //每根柱子以初始化的颜色不断轮循 
    public Paint getItemPaint(int i, int j) { 
        return colors[j % colors.length]; 
    } 
}

StatistickController类用以请求

@Controller
@RequestMapping(value="${adminPath}/statistick")
public class StatistickController {

private FrontUserService frontUserService=SpringContextHolder.getBean(FrontUserService.class);


/**
* 注册用户统计
*/
@RequestMapping(value="yuyueStatistick")
public String yuyueStatistick(@RequestParam Map<String, Object> paramMap,HttpServletRequest request,HttpServletResponse response,Model model) throws Exception {  
Page<FrontUser> page = frontUserService.findAllFrontUser(new Page<FrontUser>(request, response),paramMap);//进入条件搜索方法
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
long data=page.getCount();
dataset.addValue(data, "用户", "");
JFreeChart chart = ChartFactory.createBarChart3D(
         "注册用户统计图", //图表标题
         "注册用户", //目录轴的显示标签
         "人数", //数值轴的显示标签
         dataset, //数据集
         PlotOrientation.VERTICAL, //图表方向:水平、垂直
         true, //是否显示图例(对于简单的柱状图必须是false)
         false, //是否生成工具
         false //是否生成URL链接
     );
     CategoryPlot plot = chart.getCategoryPlot();
     BarRenderer renderer = new BarRenderer();
     plot.setRangeGridlinePaint(Color.blue);   //设置网格线颜色
     renderer.setBaseOutlinePaint(Color.RED); //边框颜色
     renderer.setDrawBarOutline(true);
     renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());//设置柱子显示数值
     renderer.setBaseItemLabelsVisible(true);
     renderer.setMaximumBarWidth(0.4);  //设置柱子的宽度
     renderer.setMinimumBarLength(0.4);
     renderer.setBaseItemLabelPaint(Color.RED);//设置数值颜色,默认黑色
     plot.setBackgroundAlpha(0.5f);  //设置背景色
     plot.setRenderer(renderer);
     setImageFont(chart);  //解决乱码问题
     String filename = ServletUtilities.saveChartAsPNG(chart, 400, 400, null, request.getSession());//生成图片
     ServletContext servlet = request.getSession().getServletContext();
     String imagePath_area = servlet.getContextPath() + "/servlet/DisplayChart?filename=" + filename;//得到功能路径    
model.addAttribute("page", page);
model.addAllAttributes(paramMap);
request.setAttribute("imagePath_area", imagePath_area);//把得到的图片放到request范围里面
        return "modules/statistick/yuyueStatistick"; //跳转到jsp页面
}
}

建FrontUserService 用于条件搜索并返回统计数值

@Service
@Transactional(readOnly = true)
public class FrontUserService  extends BaseService{


@Autowired
private FrontUserDao frontUserDao;
   /**
* 注册用户统计条件搜索
*/
public Page<FrontUser> findAllFrontUser(Page<FrontUser> page,
Map<String, Object> paramMap) {
DetachedCriteria dc = frontUserDao.createDetachedCriteria();

String name=ObjectUtils.toString(paramMap.get("name"));
if(StringUtils.isNotBlank(name)){
dc.add(Restrictions.like("name", "%"+name+"%"));
}

long mobile=StringUtils.toLong(paramMap.get("mobile"));
if(mobile>0){
dc.add(Restrictions.eq("mobile", mobile));
}

String activity=ObjectUtils.toString(paramMap.get("activity"));
if("first".equals(activity)){
dc.add(Restrictions.eq("activityId", 1L));
}else if("second".equals(activity)){
dc.add(Restrictions.eq("activityId", 2L));
}else if("third".equals(activity)){
dc.add(Restrictions.or(Restrictions.eq("activityId", 5L), Restrictions.eq("activityId", 6L)));
}
else if("four".equals(activity)){
dc.add(Restrictions.or(Restrictions.eq("activityId", 7L), Restrictions.eq("activityId", 8L)));
}
else if("five".equals(activity)){
dc.add(Restrictions.or(Restrictions.eq("activityId", 10L), Restrictions.eq("activityId", 11L)));
} else if("six".equals(activity)){
dc.add(Restrictions.eq("activityId", 13L));
}

String bank=ObjectUtils.toString(paramMap.get("bank"));
if("first".equals(bank)){
dc.add(Restrictions.eq("bank", 1L));
}else if("second".equals(bank)){
dc.add(Restrictions.eq("bank", 2L));
}

Date beginDate = DateUtils.parseDate(paramMap.get("beginDate"));
if (beginDate == null){
beginDate = DateUtils.setDays(new Date(), 1);
paramMap.put("beginDate", DateUtils.formatDate(beginDate, "yyyy-MM-dd"));
}

Date endDate = DateUtils.parseDate(paramMap.get("endDate"));
if (endDate == null){
endDate = DateUtils.addDays(DateUtils.addMonths(beginDate, 1), -1);
paramMap.put("endDate", DateUtils.formatDate(endDate, "yyyy-MM-dd"));
}

dc.add(Restrictions.between("createDate", beginDate, endDate));
return frontUserDao.find(page, dc);
}

建jsp页面

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
<title>注册用户</title>
<meta name="decorator" content="default"/>
<script type="text/javascript">
function page(n,s){
$("#pageNo").val(n);
$("#pageSize").val(s);
$("#searchForm").submit();
        return false;
        }
</script>
</head>
 
  <body>
  <form:form id="searchForm" action="${ctx}/statistick/yuyueStatistick" method="post" class="breadcrumb form-search">
<div>
<input id="pageNo" name="pageNo" type="hidden" value="${page.pageNo}"/>
<input id="pageSize" name="pageSize" type="hidden" value="${page.pageSize}"/>
</div>
<div >
<label>姓名:</label><input id="name" name="name" type="text" maxlength="50" class="input-medium" value="${name}"/>
<label>手机号:</label><input id="mobile" name="mobile" type="text" maxlength="50" class="input-medium" value="${mobile}"/>
<label>活动期数:</label>
<td class="input-medium"><select id="status" name="activity">
<option value="">请选择</option>
<option value="first"
<c:if test="${activity=='first'}">selected</c:if>>第一期</option>
<option value="second"
<c:if test="${activity=='second'}">selected</c:if>>第二期</option>
<option value="third"
<c:if test="${activity=='third'}">selected</c:if>>第三期</option>
<option value="four"
<c:if test="${activity=='four'}">selected</c:if>>第四期</option>
<option value="five"
<c:if test="${activity=='five'}">selected</c:if>>第五期</option>
<option value="six"
<c:if test="${activity=='six'}">selected</c:if>>第六期</option>
</select></td>
<label>登记银行:</label>
<td class="input-medium"><select id="status" name="bank">
<option value="">请选择</option>
<option value="first"
<c:if test="${bank=='first'}">selected</c:if>>光大银行</option>
<option value="second"
<c:if test="${bank=='second'}">selected</c:if>>浦发银行</option>
</select></td>
</div>
<div style="margin-top:8px;">
<label>登记日期:&nbsp;</label><input id="beginDate" name="beginDate" type="text" readonly="readonly" maxlength="20" class="input-medium Wdate"
value="${beginDate}" onclick="WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:false});"/>
<label>&nbsp;--&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label><input id="endDate" name="endDate" type="text" readonly="readonly" maxlength="20" class="input-medium Wdate"
value="${endDate}" onclick="WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:false});"/>&nbsp;&nbsp;
&nbsp;<input id="btnSubmit" class="btn btn-primary" type="submit" value="查询"/>&nbsp;&nbsp;
</div>
</form:form>
    <table width="100%" cellpadding="0" cellspacing="0" border="0" align="center" class="table  table-bordered table-condensed">
<tr>
<td width="30%">
<img alt="abc" src="${imagePath_area}">
</td>
<td>
<table id="contentTable" class="table table-striped table-bordered table-condensed">
<thead><tr><th class="sort loginName">姓名</th><th>手机号码</th><th>登记时间</th><th>登记银行</th><th>登记类型</th>
<tbody>
<c:forEach items="${page.list}" var="frontuser">
<tr>
<td>${frontuser.name}</td>
<td>${frontuser.mobile }</td>
<td><fmt:formatDate value="${frontuser.createDate}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
<td>${fns:getDictLabel(frontuser.bank, 'bank_name', '')}</td>
<td>${fns:getDictLabel(frontuser.status, 'appointment_status', '')}</td>
</tr>
</c:forEach>
</tbody>
</table><div class="pagination">${page}</div>
</td>
</tr>
</table>
  </body>
</html>

你可能感兴趣的:(jfreechart)