Apache ECharts
简介
Apache ECharts 是一款基于 Javascript 的数据可视化图表库,提供直观,生动,可交互,可个性化定制的数据可视化图表。
使用步骤
1). 引入echarts.js 文件
2). 为 ECharts 准备一个设置宽高的 DOM
3). 初始化echarts实例
4). 指定图表的配置项和数据
5). 使用指定的配置项和数据显示图表
业务功能
营业额统计
service层代码
@Override
public TurnoverReportVO getTurnoverReportStatistics(LocalDate begin, LocalDate end) {
// 当前集合用于存放从begin到end范围内的每天的日期,无需查询数据库
List<LocalDate> dateList = new ArrayList<>();
dateList.add(begin);
while (!begin.equals(end)) {
// 计算日期
begin = begin.plusDays(1);
dateList.add(begin);
}
// 存放每天的营业额
List<Double> turnoverList = new ArrayList<>();
for (LocalDate date : dateList) {
// 查询date日期对应的营业额数据(即已完成订单的金额合计数据)
LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
Map map = new HashMap();
map.put("begin", beginTime);
map.put("end", endTime);
map.put("status", Orders.COMPLETED);
Double turnover = orderMapper.sumByMap(map);
turnover = turnover == null ? 0.0 : turnover;
turnoverList.add(turnover);
}
// 封装返回结果
return TurnoverReportVO
.builder()
.dateList(StringUtils.join(dateList, ","))
.turnoverList(StringUtils.join(turnoverList, ","))
.build();
}
用户统计
service层代码
@Override
public UserReportVO getUserReportStatistics(LocalDate begin, LocalDate end) {
// 存放从begin到end之间的每天对应的日期
List<LocalDate> dateList = new ArrayList<>();
dateList.add(begin);
while (!begin.equals(end)) {
begin = begin.plusDays(1);
dateList.add(begin);
}
// 存放每天的新增用户数量
List<Integer> newUserList = new ArrayList<>();
// 存放每天的总用户数量
List<Integer> totalUserList = new ArrayList<>();
for (LocalDate date : dateList) {
LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
Map map = new HashMap();
map.put("end", endTime);
// 总用户数量
Integer totalUser = userMapper.countByMap(map);
map.put("begin", beginTime);
// 新增用户数量
Integer newUser = userMapper.countByMap(map);
totalUserList.add(totalUser);
newUserList.add(newUser);
}
// 封装结果数据
return UserReportVO
.builder()
.dateList(StringUtils.join(dateList, ","))
.totalUserList(StringUtils.join(totalUserList, ","))
.newUserList(StringUtils.join(newUserList, ","))
.build();
}
订单统计
service层代码
@Override
public OrderReportVO getOrderReportStatistics(LocalDate begin, LocalDate end) {
// 存放从begin到end之间的每天对应的日期
List<LocalDate> dateList = new ArrayList<>();
dateList.add(begin);
while (!begin.equals(end)) {
begin = begin.plusDays(1);
dateList.add(begin);
}
// 存放每天的订单总数
List<Integer> orderCountList = new ArrayList<>();
// 存放每天的有效订单数
List<Integer> validOrderCountList = new ArrayList<>();
// 遍历集合,查询每天的有效订单数和订单总数
for (LocalDate date : dateList) {
// 每天的订单总数
LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
Integer orderCount = getOrderCount(beginTime, endTime, null);
// 每天的有效订单数
Integer validOrder = getOrderCount(beginTime, endTime, Orders.COMPLETED);
orderCountList.add(orderCount);
validOrderCountList.add(validOrder);
}
// 计算时间区间内的订单总数量
Integer totalOrderCount = orderCountList.stream().reduce(Integer::sum).get();
// 计算时间区间内的有效订单数
Integer validOrderCount = validOrderCountList.stream().reduce(Integer::sum).get();
// 计算订单完成率
Double orderCompletionRate = 0.0; // 初始化orderCompletionRate值为0.0,避免出现分母为0的情况。
if (totalOrderCount != 0) {
orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
}
return OrderReportVO
.builder()
.dateList(StringUtils.join(dateList, ","))
.orderCountList(StringUtils.join(orderCountList, ","))
.validOrderCountList(StringUtils.join(validOrderCount, ","))
.totalOrderCount(totalOrderCount)
.validOrderCount(validOrderCount)
.orderCompletionRate(orderCompletionRate)
.build();
}
/**
* 根据条件统计订单数量
* @param begin
* @param end
* @param status
* @return
*/
private Integer getOrderCount(LocalDateTime begin, LocalDateTime end, Integer status) {
Map map = new HashMap();
map.put("begin", begin);
map.put("end", end);
map.put("status", status);
return orderMapper.countByMap(map);
}
销量排行top10
service层代码
@Override
public SalesTop10ReportVO getTop10ReportStatistics(LocalDate begin, LocalDate end) {
LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);
List<GoodsSalesDTO> salesTop10 = orderMapper.getSalesTop10(beginTime, endTime);
List<String> names = salesTop10.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
String nameList = StringUtils.join(names, ",");
List<Integer> number = salesTop10.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());
String numberList = StringUtils.join(number, ",");
// 封装返回结果数据
return SalesTop10ReportVO
.builder()
.nameList(nameList)
.numberList(numberList)
.build();
}
学习补充
(1)LocalDateTime.of()方法
LocalDateTime.of(@NotNull java.time.LocalDate date,@NotNull java.time.LocalTime time),方法有两个参数,传入LocalTime对象。
第一个参数传入一个LocalDate类型的时间对象date,第二个参数传入LocalTime.MIN/LocalTime.MAX,利用该方法可以获取到date该天的时间最小值(00:00:00)或最大值(23:59:59.999999999),并将其转为LocalDateTime类型。LocalDate类型对象只能精确到天,而LocalDateTime类型对象能够精确到秒。
(2)list.stream().map().collect(Collectors.toList())的用法
stream()为list集合创建串行流,map()方法用于映射每个元素到对应的结果。对象::getter()方法,取得对象的该属性。Collectors.toList() 将流中的所有元素导出到一个list列表中。
List<Integer> number = salesTop10.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());该表达式就是将salesTop10(list对象)转换为流对象,取得每个元素的number值,再组成一个新的list对象,封装到number(list对象)中。最后效果等同于遍历循环salesTop10列表,获取每个GoodsSalesDTO对象的number值,添加到一个number的新列表中。
(3) StringUtils.join()方法
StringUtils是org.apache.commons.lang3包下的一个工具类。join()方法可将一个字符串数组或集合中的元素按照指定的连接符连接成一个字符串。
(4)list.stream().reduce().get()的用法
实现了对list中的元素累加操作。
(5)plusDays()/minusDays()方法
基于当前日期增加/减少指定的天日数。