《苍穹外卖》Day11部分知识点记录(数据统计——图像报表)

本文主要是介绍《苍穹外卖》Day11部分知识点记录(数据统计——图像报表),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、Apache ECharts

介绍

Apache ECharts是一款基于javascript的数据可视化图标库,提供直观、生动、可交互、可个性化定制的数据可视化图表。

官网地址:https://echarts.apache.org/zh/index.html

效果展示

  • 柱形图
  • 饼图
  • 折线图

入门案例

1. 在 echarts CDN by jsDelivr - A CDN for npm and GitHub 选择 dist/echarts.js,点击并保存为 echarts.js 文件

2. 编写入门案列

<!DOCTYPE html>
<html><head><meta charset="utf-8" /><title>ECharts</title><!-- 引入刚刚下载的 ECharts 文件 --><script src="echarts.js"></script></head><body><!-- 为 ECharts 准备一个定义了宽高的 DOM --><div id="main" style="width: 600px;height:400px;"></div><script type="text/javascript">// 基于准备好的dom,初始化echarts实例var myChart = echarts.init(document.getElementById('main'));// 指定图表的配置项和数据var option = {title: {text: 'ECharts 入门示例'},tooltip: {},legend: {data: ['销量']},xAxis: {data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']},yAxis: {},series: [{name: '销量',type: 'bar',data: [5, 20, 36, 10, 10, 20]}]};// 使用刚指定的配置项和数据显示图表。myChart.setOption(option);</script></body>
</html>

效果:

总结:使用ECharts,重点在于研究当前图标所需的数据格式。通常是需要后端提供符合格式要求的动态数据,然后响应给前端来展示图表。

二、营业额统计

需求分析和设计

业务规则:

  • 营业额指订单状态为已完成的订单金额统计
  • 基于可视化报表的折线图展示营业额数据,x轴为日期,y轴为营业额
  • 根据时间选择区间,展示每天都营业额数据

接口设计

根据接口定义设计对应的VO:

代码开发

1. 创建一个新的Controller,即admin/ReportController

package com.sky.controller.admin;import com.sky.result.Result;
import com.sky.service.ReportService;
import com.sky.vo.TurnoverReportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.time.LocalDate;/*** 数据统计相关接口*/
@RestController
@RequestMapping("/admin/report")
@Api(tags = "数据统计相关接口")
@Slf4j
public class ReportController {@Autowiredprivate ReportService reportService;/*** 营业额统计* @param begin* @param end* @return*/@GetMapping("/turnoverStatistics")@ApiOperation("营业额统计")public Result<TurnoverReportVO> turnoverStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {log.info("营业额数据统计:{}, {}", begin, end);return Result.success(reportService.getTurnoverStatistics(begin, end));}
}

2. ReportService

package com.sky.service;import com.sky.vo.TurnoverReportVO;import java.time.LocalDate;public interface ReportService {/*** 统计指定时间区间内的营业额* @param begin* @param end* @return*/TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end);
}

3. ReportServiceImpl

注意StringUtils导的是import org.apache.commons.lang3.StringUtils;

package com.sky.service.impl;import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.service.ReportService;
import com.sky.vo.TurnoverReportVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;@Service
@Slf4j
public class ReportServiceImpl implements ReportService {@Autowiredprivate OrderMapper orderMapper;/*** 统计指定时间区间内的营业额* @param begin* @param end* @return*/@Overridepublic TurnoverReportVO getTurnoverStatistics(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);// select sum(amount) from orders where order_time > beginTime and order_time < endTime and status = 5Map 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();}
}

4. OrderMapper

    /*** 根据动态条件统计营业额数据* @param map* @return*/Double sumByMap(Map map);

5. OrderMapper.xml

    <select id="sumByMap" resultType="java.lang.Double">select sum(amount) from orders<where><if test="begin != null">and order_time &gt; #{begin}</if><if test="end != null">and order_time &lt; #{end}</if><if test="status != null">and status = #{status}</if></where></select>

在XML文件中,有一些特殊字符需要使用实体编码表示,以避免与XML语法冲突,如:

  • &lt; :小于号(<)
  • &gt; : 大于号(>)
  • &amp; : 与号(&)
  • &quot; : 双引号(")
  • &apos : 单引号(')
  • &nbsp;:空格
  • &copy;:版权符号(©)
  • &reg;:注册商标符号(®)
  • &euro;:欧元符号(€)
  • &yen;:日元符号(¥)
  • &pound;:英镑符号(£)

功能测试

三、用户统计

需求分析和设计

产品原型

业务规则

  • 基于可视化报表的折线图展示用户数据,x轴为日期,y轴为用户数
  • 根据时间选择区间,展示每天的用户总量和新增用户数据

接口设计

代码开发

1. ReportController

    /*** 用户统计* @param begin* @param end* @return*/@GetMapping("/userStatistics")@ApiOperation("用户统计")public Result<UserReportVO> userStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {log.info("用户数据统计:{}, {}", begin, end);return Result.success(reportService.getUserStatistics(begin, end));}

2. ReportService

    /*** 统计指定时间区间内的用户数据* @param begin* @param end* @return*/UserReportVO getUserStatistics(LocalDate begin, LocalDate end);

3. ReportServiceImpl

    @Autowiredprivate UserMapper userMapper;/*** 统计指定时间区间内的用户数据* @param begin* @param end* @return*/@Overridepublic UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {// 当前集合用于存放从begin到end范围内的每天的日期List<LocalDate> dateList = new ArrayList<>();dateList.add(begin);while(!begin.equals(end)) {// 日期计算,计算指定日期的后踢腿对应的日期// LocalDate类的plusDays方法并不会修改原始的LocalDate对象,而是返回一个新的LocalDate对象begin = begin.plusDays(1);dateList.add(begin);}// 存放每天的新增用户数量// select count(id) from user where create_time < ? and create_time > ?List<Integer> newUserList = new ArrayList<>();// 存放每天的总用户数量// select count(id) from user where create_time < ?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();}

4. UserMapper

    /*** 根据动态条件统计用户数量* @return*/Integer countByMap(Map map);

5. UserMapper.xml

    <select id="countByMap" resultType="java.lang.Integer">select count(id) from orders<where><if test="begin != null">and order_time &gt; #{begin}</if><if test="end != null">and order_time &lt; #{end}</if><if test="status != null">and status = #{status}</if></where></select>

功能测试

四、订单统计

需求分析和设计

产品原型

业务规则

  • 有效订单指状态为”已完成“的订单
  • 基于可视化报表的折线图展示订单数据,x轴为日期,y轴为订单数量
  • 根据时间选择区间,展示每天的订单总数和有效订单数
  • 展示所选时间区间内的有效订单数、总订单数、订单完成率,订单完成率 = 有效订单数 / 总订单数 * 100%。

接口设计

代码开发

1. ReportController

注意不要写错路径

    /*** 订单统计* @param begin* @param end* @return*/@GetMapping("/ordersStatistics")@ApiOperation("订单统计")public Result<OrderReportVO> orderStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {log.info("订单数据统计:{}, {}", begin, end);return Result.success(reportService.getOrderStatistics(begin, end));}

2. ReportService

    /*** 统计指定时间区间内的订单数据* @param begin* @param end* @return*/OrderReportVO getOrderStatistics(LocalDate begin, LocalDate end);

3. ReportServiceImpl

    /*** 统计指定时间区间内的订单数据* @param begin* @param end* @return*/@Overridepublic OrderReportVO getOrderStatistics(LocalDate begin, LocalDate end) {// 当前集合用于存放从begin到end范围内的每天的日期List<LocalDate> dateList = new ArrayList<>();dateList.add(begin);while(!begin.equals(end)) {// 日期计算,计算指定日期的后踢腿对应的日期// LocalDate类的plusDays方法并不会修改原始的LocalDate对象,而是返回一个新的LocalDate对象begin = begin.plusDays(1);dateList.add(begin);}// 存放每天的订单总数List<Integer> orderCountList = new ArrayList<>();// 存放每天的有效订单总数List<Integer> validOrderCountList = new ArrayList<>();// 遍历dateList集合,查询每天的有效订单数和订单总数for (LocalDate date : dateList) {// 一天的开始时间LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);// 一天的结束时间LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);// 查询每天的订单总数// select count(id) from orders where order_time > ? and order_time < ?Integer orderCount = getOrderCount(beginTime, endTime, null);// 查询每天的有效订单总数// select count(id) from orders where order_time > ? and order_time < ? and status = 5Integer validOrderCount = getOrderCount(beginTime, endTime, Orders.COMPLETED);orderCountList.add(orderCount);validOrderCountList.add(validOrderCount);}// 计算时间区间内的订单总数量Integer totalOrderCount = orderCountList.stream().reduce(Integer::sum).get();// 计算时间区间内的有效订单总数量Integer validOrderCount = validOrderCountList.stream().reduce(Integer::sum).get();// 计算订单完成率Double orderCompletionRate = 0.0;if(totalOrderCount != 0) {orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;}// 封装返回结果return OrderReportVO.builder().dateList(StringUtils.join(dateList, ",")).orderCountList(StringUtils.join(orderCountList, ",")).validOrderCountList(StringUtils.join(validOrderCountList, ",")).totalOrderCount(totalOrderCount).validOrderCount(validOrderCount).orderCompletionRate(orderCompletionRate).build();}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);}

4. OrderMapper

    /*** 根据动态条件统计订单数量* @param map* @return*/Integer countByMap(Map map);

5. OrderMapper.xml

    <select id="countByMap" resultType="java.lang.Integer">select count(id) from orders<where><if test="begin != null">and order_time &gt; #{begin}</if><if test="end != null">and order_time &lt; #{end}</if><if test="status != null">and status = #{status}</if></where></select>

功能测试

五、销量排名Top10

需求分析和设计

产品原型

业务规则

  • 根据时间选择区间,展示销量前10的商品(包括菜品和套餐)
  • 基于可视化报表的柱状图展示商品销量
  • 此处的销量为商品销售的份数

接口设计

代码开发

1. ReportController

    /*** 销量排名top10* @param begin* @param end* @return*/@GetMapping("/top10")@ApiOperation("销量排名top10")public Result<SalesTop10ReportVO> top10(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {log.info("销量排名top10:{}, {}", begin, end);return Result.success(reportService.getSalesTop10(begin, end));}

2. ReportService

    /*** 统计指定时间区间内的销量排名top10* @param begin* @param end* @return*/SalesTop10ReportVO getSalesTop10(LocalDate begin, LocalDate end);

3. ReportServiceImpl

    /*** 统计指定时间区间内的销量排名top10* @param begin* @param end* @return*/@Overridepublic SalesTop10ReportVO getSalesTop10(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> numbers = salesTop10.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());String numberList = StringUtils.join(numbers, ",");// 封装返回结果数据return SalesTop10ReportVO.builder().nameList(nameList).numberList(numberList).build();}

4. OrderMapper

    /*** 统计指定时间区间内的销量排名前10* @param begin* @param end* @return*/List<GoodsSalesDTO> getSalesTop10(LocalDateTime begin, LocalDateTime end);

5. OrderMapper.xml

    <select id="getSalesTop10" resultType="com.sky.dto.GoodsSalesDTO">select od.name, sum(od.number) numberfrom order_detail od, orders owhere od.order_id = o.id and o.status = 5<if test="begin != null">and o.order_time &gt; #{begin}</if><if test="end != null">and o.order_time &lt; #{end}</if>group by od.nameorder by number desclimit 0,10</select>

功能测试

这篇关于《苍穹外卖》Day11部分知识点记录(数据统计——图像报表)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/942591

相关文章

SpringBoot使用GZIP压缩反回数据问题

《SpringBoot使用GZIP压缩反回数据问题》:本文主要介绍SpringBoot使用GZIP压缩反回数据问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot使用GZIP压缩反回数据1、初识gzip2、gzip是什么,可以干什么?3、Spr

Java使用SLF4J记录不同级别日志的示例详解

《Java使用SLF4J记录不同级别日志的示例详解》SLF4J是一个简单的日志门面,它允许在运行时选择不同的日志实现,这篇文章主要为大家详细介绍了如何使用SLF4J记录不同级别日志,感兴趣的可以了解下... 目录一、SLF4J简介二、添加依赖三、配置Logback四、记录不同级别的日志五、总结一、SLF4J

一文详解如何在Python中从字符串中提取部分内容

《一文详解如何在Python中从字符串中提取部分内容》:本文主要介绍如何在Python中从字符串中提取部分内容的相关资料,包括使用正则表达式、Pyparsing库、AST(抽象语法树)、字符串操作... 目录前言解决方案方法一:使用正则表达式方法二:使用 Pyparsing方法三:使用 AST方法四:使用字

在Spring Boot中浅尝内存泄漏的实战记录

《在SpringBoot中浅尝内存泄漏的实战记录》本文给大家分享在SpringBoot中浅尝内存泄漏的实战记录,结合实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录使用静态集合持有对象引用,阻止GC回收关键点:可执行代码:验证:1,运行程序(启动时添加JVM参数限制堆大小):2,访问 htt

SpringBoot集成Milvus实现数据增删改查功能

《SpringBoot集成Milvus实现数据增删改查功能》milvus支持的语言比较多,支持python,Java,Go,node等开发语言,本文主要介绍如何使用Java语言,采用springboo... 目录1、Milvus基本概念2、添加maven依赖3、配置yml文件4、创建MilvusClient

SpringValidation数据校验之约束注解与分组校验方式

《SpringValidation数据校验之约束注解与分组校验方式》本文将深入探讨SpringValidation的核心功能,帮助开发者掌握约束注解的使用技巧和分组校验的高级应用,从而构建更加健壮和可... 目录引言一、Spring Validation基础架构1.1 jsR-380标准与Spring整合1

OpenCV图像形态学的实现

《OpenCV图像形态学的实现》本文主要介绍了OpenCV图像形态学的实现,包括腐蚀、膨胀、开运算、闭运算、梯度运算、顶帽运算和黑帽运算,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起... 目录一、图像形态学简介二、腐蚀(Erosion)1. 原理2. OpenCV 实现三、膨胀China编程(

MySQL 中查询 VARCHAR 类型 JSON 数据的问题记录

《MySQL中查询VARCHAR类型JSON数据的问题记录》在数据库设计中,有时我们会将JSON数据存储在VARCHAR或TEXT类型字段中,本文将详细介绍如何在MySQL中有效查询存储为V... 目录一、问题背景二、mysql jsON 函数2.1 常用 JSON 函数三、查询示例3.1 基本查询3.2

SpringBatch数据写入实现

《SpringBatch数据写入实现》SpringBatch通过ItemWriter接口及其丰富的实现,提供了强大的数据写入能力,本文主要介绍了SpringBatch数据写入实现,具有一定的参考价值,... 目录python引言一、ItemWriter核心概念二、数据库写入实现三、文件写入实现四、多目标写入

使用Python将JSON,XML和YAML数据写入Excel文件

《使用Python将JSON,XML和YAML数据写入Excel文件》JSON、XML和YAML作为主流结构化数据格式,因其层次化表达能力和跨平台兼容性,已成为系统间数据交换的通用载体,本文将介绍如何... 目录如何使用python写入数据到Excel工作表用Python导入jsON数据到Excel工作表用