当前位置:   article > 正文

VUE+Echarts+SpringBoot:后端获取数据库中的值传给前端图表展示_echarts后端怎么向前端传数据

echarts后端怎么向前端传数据

一、后端部分

由于逻辑比较简单,没有写service层。

数据库中的数据和最后的结果如下所示:

项目框架如下用到下面四个文件

1.实体类Echarts

  1. package com.example.analysis.bean;
  2. public class Echarts{
  3. private Integer id;
  4. private String name;
  5. private Integer num;
  6. public Echarts(String name, Integer num) {
  7. this.name = name;
  8. this.num = num;
  9. }
  10. public Integer getId() {
  11. return id;
  12. }
  13. public void setId(Integer id) {
  14. this.id = id;
  15. }
  16. public String getName() {
  17. return name;
  18. }
  19. public void setName(String name) {
  20. this.name = name;
  21. }
  22. public Integer getNum() {
  23. return num;
  24. }
  25. public void setNum(Integer num) {
  26. this.num = num;
  27. }
  28. @Override
  29. public String toString() {
  30. return "Echarts{" +
  31. "id=" + id +
  32. ", name='" + name + '\'' +
  33. ", num=" + num +
  34. '}';
  35. }
  36. }

2.controller层:EchartsController

  1. package com.example.analysis.controller;
  2. import com.alibaba.fastjson.JSON;
  3. import com.example.analysis.bean.Disease;
  4. import com.example.analysis.bean.Echarts;
  5. import com.example.analysis.dao.EchartsDao;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RestController;
  9. import java.util.ArrayList;
  10. import java.util.HashMap;
  11. import java.util.List;
  12. @RestController
  13. public class EchartsController {
  14. @Autowired
  15. private EchartsDao echartsDao;
  16. @RequestMapping("/echarts")
  17. public String getEcharts(){
  18. List<Echarts> echarts = echartsDao.getAllEcharts();
  19. //System.out.println(echarts);
  20. HashMap<String, Object> res = new HashMap<>();
  21. List<String> name = new ArrayList<>();
  22. List<Integer> num = new ArrayList<>();
  23. for( int i = 0; i < echarts.size(); i++ ){
  24. name.add(echarts.get(i).getName());
  25. num.add(echarts.get(i).getNum());
  26. }
  27. res.put("name",name);
  28. res.put("num",num);
  29. System.out.println(res);
  30. String res_string = JSON.toJSONString(res);
  31. return res_string;
  32. }
  33. }

3.dao层

  1. package com.example.analysis.dao;
  2. import com.example.analysis.bean.Echarts;
  3. import com.example.analysis.bean.User;
  4. import org.apache.ibatis.annotations.Param;
  5. import org.springframework.stereotype.Repository;
  6. import java.util.List;
  7. @Repository
  8. public interface EchartsDao {
  9. public List<Echarts> getAllEcharts();
  10. }

4.mapper层

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <mapper namespace="com.example.analysis.dao.EchartsDao">
  5. <select id="getAllEcharts" resultType="com.example.analysis.bean.Echarts">
  6. SELECT name,num FROM echarts
  7. </select>
  8. </mapper>

二、前端部分:Vue+Echarts显示图表

这部分花费了非常多的时间,看了网上超级多的博客,感觉写的都不好,这里建议先看下echarts的使用方式以后,获取到后台传来的数据后在看。

  1. <template>
  2. <div id="app">
  3. <div id="main" style="width:600px;height:400px"></div>
  4. </div>
  5. </template>
  6. <script>
  7. import echarts from "echarts";
  8. export default {
  9. name: "App",
  10. created() {
  11. this.getAllEcharts();
  12. },
  13. data() {
  14. return {
  15. name: [],
  16. num: [],
  17. };
  18. },
  19. methods: {
  20. async getAllEcharts() {
  21. const {data: res1} = await this.$http.get("echarts");
  22. console.log(res1);
  23. this.name = res1.name;
  24. this.num = res1.num;
  25. this.setChart();
  26. },
  27. setChart() {
  28. // 基于准备好的dom,初始化echarts实例
  29. this.chart = echarts.init(document.getElementById('main'))
  30. // console.log(this.chart)
  31. // 指定图表的配置项和数据
  32. var option = {
  33. title: {
  34. text: "疾病人数分布"
  35. },
  36. tooltip: {},
  37. legend: {
  38. data: ["人数"]
  39. },
  40. xAxis: {
  41. data: this.name,
  42. },
  43. yAxis: {},
  44. series: [
  45. {
  46. name: "人数",
  47. type: "bar",
  48. data: this.num,
  49. }
  50. ],
  51. }
  52. this.chart.setOption(option);
  53. },
  54. mounted() {
  55. this.$nextTick(function () {
  56. this.drawPid("main");
  57. });
  58. }
  59. }
  60. }
  61. </script>

2.main.js

引入echarts组件,具体echarts的安装方式网上很多,安装即可。

  1. import echarts from "echarts"
  2. Vue.prototype.$echarts = echarts

然后在in.dex里面引入前端映射的路径即可。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/276629
推荐阅读
相关标签
  

闽ICP备14008679号