一.什么是报表向上级报告情况的表格。简单的说:报表就是用表格、图表等格式来动态显示数据,可以用公式表示为:“报表 = 多样的格式 + 动态的数据”表格:详细数据图表: 直观二.表格数据展示2.1 准备了一 vo报表中需的数据(准备的类)public class
PurchasebillitemVO {
//编号
private Long id;
//供应商
private String supplierName;
//采购员
private String buyerName;
//产品
private String productName;
//产品类型
private String productTypeName;
//日期
private Date vdate;
//数量
private BigDecimal num;
//单价
private BigDecimal price;
//小计
private BigDecimal amount;
//状态
private Integer status;
//分组字段
private String groupField;
public PurchasebillitemVO() {}
//创建时设置值
public PurchasebillitemVO(Purchasebillitem item,Integer groupBy) {
this.id = item.getId();
this.supplierName = item.getBill().getSupplier().getName();
this.buyerName = item.getBill().getBuyer().getUsername();
this.productName = item.getProduct().getName();
this.productTypeName = item.getProduct().getTypes().getName();
this.vdate = item.getBill().getVdate();
this.num = item.getNum();
this.price = item.getPrice();
this.amount = item.getAmount();
this.status = item.getBill().getStatus();
//确定分组字段
switch (groupBy){
case 1:{
this.groupField = this.buyerName;
break;
}
case 2:{
this.groupField = (DateUtils.toCalendar(this.vdate).get(Calendar.MONTH)+1) +"月份";
break;
}
default:
this.groupField =this.supplierName;
}
}
...
}
2.2 前端展示easyui的一个扩展控制:groupview2.2.1 引入相应的js支持
<script src="/easyui/plugins/datagrid-groupview.js"></script>2.2.2 高级查询字段<div id="tb" style="padding:5px;height:auto">
<!-- 这里就是一个日历 -->
<div id="cc" class="easyui-calendar"></div>
<form id="searchForm" method="post" action="">
采购时间: <input name="beginDate" class="easyui-datebox" data-options="sharedCalendar:'#cc'"
style="width:120px">
- <input name="endDate" class="easyui-datebox" data-options="sharedCalendar:'#cc'" style="width:120px">
状态:<select class="easyui-combobox" panelHeight="auto" name="status" style="width: 100px">
<option value="">--请选择--</option>
<option value="0">未审</option>
<option value="1">已审</option>
<option value="-1">作废</option>
</select>
<!-- 分组的条件 -->
<select class="easyui-combobox" panelHeight="auto" name="groupBy" style="width: 100px">
<option value="0">供应商</option>
<option value="1">采购员</option>
<option value="2">月份</option>
</select>
<a href="javascript:;" data-method="search" class="easyui-linkbutton" iconCls="icon-search">查询</a>
<a href="javascript:;" data-method="show3d" class="easyui-linkbutton" iconCls="icon-search">3D</a>
<a href="javascript:;" data-method="show2d" class="easyui-linkbutton" iconCls="icon-search">2D</a>
</form>
</div>
<table id="itemsGrid"></table>
2.2.3 案例进行修改//创建分组grid
itemsGrid.datagrid({
//title:'',
// width:500,
// height:250,
fit:true,
rownumbers:true,
remoteSort:false,
nowrap:false,
fitColumns:true,
//url:'datagrid_data.json',
toolbar:"#tb",
url:'/purchasebillitem/findItems',
columns:[[
{field:'id',title:'编号',width:100,sortable:true},
{field:'supplierName',title:'供应商',width:100,sortable:true},
{field:'buyerName',title:'采购员',width:100,sortable:true},
{field:'productName',title:'产品',width:100,sortable:true},
{field:'productTypeName',title:'产品类型',width:100,sortable:true},
{field:'vdate',title:'采购日期',width:100,sortable:true},
{field:'num',title:'数量',width:100,sortable:true},
{field:'price',title:'价格',width:100,sortable:true},
{field:'amount',title:'小计',width:100,sortable:true},
{field:'status',title:'状态',width:100,sortable:true,formatter:statusFormat}
]],
groupField:'groupField', //指示要被分组的字段
view: groupview,
groupFormatter:function(value, rows){ //组格式化
let num = 0,amount = 0;
for(let r of rows){
num += r.num;
amount += r.amount;
}
return value + ' - ' + rows.length + '条数据 <span style="color: green;">共'+num+'条数据</span> <span style="color: red;">总金额:'+amount +"</span>";
}
});
三.图表展示两种技术:flash(actionscript),h5
(画布)flash缺点:不安全,容易崩溃IE的话只能是flash的方式两个框架:highchart(收费,支持IE),echart(百度,开源免费)3.1 前端使用3.1.1 引入相应的js
3.1.2 弹出div进行展示准备弹出来的
<!-- 一个弹出框,里面要装一个form表单 -->
<div id="chartDialog" class="easyui-dialog" title="图表展示"
data-options="height:400,width:600,closed:true,modal:true">
<div id="container" style="height: 320px"></div>
</div>
点击3
D按钮弹出图表show3d(){
chartDialog.dialog("center").dialog("open");
//拿到表单中的所有数据
var params = searchForm.serializeObject();
//通过Ajax到后台拿到相应的值[{name:xxx,y:10},]
$.post("/purchasebillitem/findCharts",params,function (data) {
//注意这里有一个异步问题
var chart = Highcharts.chart('container', {
chart: {
type: 'pie', //饼图
options3d: {
enabled: true,
alpha: 45,
beta: 0
}
},
title: {
text: '我是一个头'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
depth: 35,
dataLabels: {
enabled: true,
format: '{point.name}'
}
}
},
series: [{
type: 'pie',
name: '浏览器有',
data: data
}]
});
})
}
3.2 后台获取参数1.修改query中的方法
// where o.bill.status = ? and o.xxx =? ...
//接收参数的变量
private List params = new ArrayList();
//准备一个方法,返回JPQL的查询条件
public String createWhereJPQL(){
StringBuilder jpql = new StringBuilder();
//开始时间
if(beginDate!=null){
jpql.append(" and o.bill.vdate >= ? ");
params.add(beginDate);
}
//结束时间
if(endDate!=null){
jpql.append(" and o.bill.vdate < ? ");
params.add(DateUtils.addDays(endDate, 1));
}
//状态
if(status!=null){
jpql.append(" and o.bill.status = ? ");
params.add(status);
}
//第一个条件必需是where开头
return jpql.toString().replaceFirst("and", "where");
}
//创建分组的JPQL
public String createGroupBy(){
String groupStr = "o.bill.supplier.name";
switch (groupBy){
case 1:{
groupStr="o.bill.buyer.username";
break;
}
case 2:{
groupStr="MONTH(o.bill.vdate)";
break;
}
}
return groupStr;
}
2.PurchasebillitemServiceImpl/**
* 查询图表需要的数据
* @param query
*/
@Override
public List<Map> findCharts(PurchasebillitemQuery query){
List<Map> mapList = new ArrayList<>();
//拿到条件JPQL
String whereJPQL = query.createWhereJPQL();
//拿到条件对应的参数
List params = query.getParams();
//准备分组的条件
String groupBy = query.createGroupBy();
//根据供应商分组拿到的数据
String jpql = "select "+groupBy+",sum(o.amount) from Purchasebillitem o "+whereJPQL+" group by "+groupBy;
List<Object[]> list = findByJpql(jpql,params.toArray());
//需要把一个List<Object[]> -> List<Map>
for (Object[] objects : list) {
Map map = new HashMap();
map.put("name", objects[0]);
map.put("y", objects[1]);
mapList.add(map);
}
return mapList;
}