【Vue3 ts】echars图表展示统计的月份数据

图片展示

此处内容为展示24年各个月份产品的创建数量。在后端统计24年各个月份产品数量后,以数组的格式发送给前端,前端负责展示。

后端

entity层:

java 复制代码
@Data
@Schema(description = "月份统计")

public class MonthCount {
    private String month;
    private Integer count;
}

service层:

java 复制代码
@Override
    public List<MonthCount> getCreateMonth() {
        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("MM");
        // 查询 2024 年每个月的产品创建日期
        List<PlmProductEntity> productList = baseMapper.selectList(new QueryWrapper<PlmProductEntity>()
                .apply("YEAR(create_date) = 2024"));

        // 统计每个月份的产品数量
        Map<String, Long> countMap = productList.stream()
                .collect(Collectors.groupingBy(
                        product -> YearMonth.from(product.getCreateDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate()).format(dateFormat),
                        Collectors.counting()
                ));

        // 将统计结果转换为 MonthCount 对象列表
        List<MonthCount> monthCounts = new ArrayList<>();
        countMap.forEach((month, count) -> {
            MonthCount monthCount = new MonthCount();
            monthCount.setMonth(month);
            monthCount.setCount(count.intValue()); // 将 Long 类型的 count 转换为 int
            monthCounts.add(monthCount);
        });

        return monthCounts;
    }

controller层:

java 复制代码
@GetMapping("/getCreateMonth")
    @Operation(summary = "得到创建月份")
    public Result<List<MonthCount>> getCreateMonth(){
        List<MonthCount> month = plmProductService.getCreateMonth();
        for (MonthCount monthCount :month){
            System.out.println(monthCount.getMonth());
        }
        return R2.ok(month);
    }

前端得到的数据响应格式为:

{

"type": "success",

"result": [

{

"month": "04",

"count": 1

},

{

"month": "05",

"count": 1

}

],

"code": 200,

"message": "success",

"timestamp": "2024-07-14 14:20:39"

}

可以看到成功包装为数组。

前端

typescript 复制代码
export const getCreateMonth = () => defHttp.get({ url: Api.GetCreateMonth });

此处defHttp为自己写的发送信息的方法,各位将其替换为axios发送的方式即可。

typescript 复制代码
onMounted(async () => {
    const response = await getCreateMonth();
    console.log(response);
    try {
      // 构建月份数组和对应的产品创建数量数组
      const monthNames = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];
      const productCounts = monthNames.map((month) => {
        const monthData = response.find((item) => item.month === month);
        return monthData ? monthData.count : 0; // 如果没有数据,默认为0
      });
      // 设置图表的选项
      setOptions({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
          lineStyle: {
            width: 1,
            color: '#019680',
          },
        },
      },
      xAxis: {
        type: 'category',
        boundaryGap: false,
        data: monthNames.map((month) => `${month}月`), // 添加月份单位
      },
      yAxis: {
        type: 'value',
      },
      grid: { left: '1%', right: '1%', top: '2%', bottom: 0, containLabel: true },
      series: [
        {
          data: productCounts,
          type: 'line',
          areaStyle: {},
        },
      ],
    });
    } catch (error) {
      console.error('Error fetching data:', error);
    }
  });

如果看到盒子被撑开但是一片空白,那么说明引入成功但数据格式输入不符上述代码的处理,这个时候就输出来看看得到的是什么样子的格式console.log(response);,再修改得到最终图形。

相关推荐
U.2 SSD27 分钟前
ECharts漏斗图示例
前端·javascript·echarts
知识分享小能手10 小时前
微信小程序入门学习教程,从入门到精通,微信小程序核心 API 详解与案例(13)
前端·javascript·学习·react.js·微信小程序·小程序·vue
小灰灰的可爱无人可替代11 小时前
记录一次使用docker和docker-compose更新vue前端项目问题
nginx·docker·vue
java水泥工19 小时前
基于Echarts+HTML5可视化数据大屏展示-车辆综合管控平台
前端·echarts·html5·大屏模版
拜无忧1 天前
【案例】可视化模板,驾驶舱模板,3x3,兼容移动
前端·echarts·数据可视化
U.2 SSD1 天前
Echarts单轴坐标系散点图
前端·javascript·echarts
java水泥工2 天前
酒店客房管理系统|基于SpringBoot和Vue的酒店客房管理系统(源码+数据库+文档)
spring boot·vue·酒店管理系统·酒店客房管理系统
爱看书的小沐3 天前
【小沐学WebGIS】基于Three.JS绘制飞行轨迹Flight Tracker(Three.JS/ vue / react / WebGL)
javascript·vue·webgl·three.js·航班·航迹·飞行轨迹
U.2 SSD3 天前
ECharts 日历坐标示例
前端·javascript·echarts
paopaokaka_luck4 天前
基于SpringBoot+Vue的志行交通法规在线模拟考试(AI问答、WebSocket即时通讯、Echarts图形化分析、随机测评)
vue.js·人工智能·spring boot·后端·websocket·echarts