• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

使用Echarts+springBoot连接数据库实现树状图和饼状图

武飞扬头像
自闭少年怪咖
帮助3

一、效果展示

1.树状图

学新通

2.饼状图

学新通

二、数据库

学新通

 学新通

三、pom.xml文件

  1.  
    <dependencies>
  2.  
    <dependency>
  3.  
    <groupId>org.projectlombok</groupId>
  4.  
    <artifactId>lombok</artifactId>
  5.  
    <optional>true</optional>
  6.  
    </dependency>
  7.  
    <dependency>
  8.  
    <groupId>org.springframework.boot</groupId>
  9.  
    <artifactId>spring-boot-starter-data-jpa</artifactId>
  10.  
    </dependency>
  11.  
    <dependency>
  12.  
    <groupId>mysql</groupId>
  13.  
    <artifactId>mysql-connector-java</artifactId>
  14.  
    </dependency>
  15.  
    <dependency>
  16.  
    <groupId>org.springframework.boot</groupId>
  17.  
    <artifactId>spring-boot-starter-web</artifactId>
  18.  
    </dependency>
  19.  
     
  20.  
    <dependency>
  21.  
    <groupId>org.springframework.boot</groupId>
  22.  
    <artifactId>spring-boot-starter-test</artifactId>
  23.  
    <scope>test</scope>
  24.  
    <exclusions>
  25.  
    <exclusion>
  26.  
    <groupId>org.junit.vintage</groupId>
  27.  
    <artifactId>junit-vintage-engine</artifactId>
  28.  
    </exclusion>
  29.  
    </exclusions>
  30.  
    </dependency>
  31.  
    <dependency>
  32.  
    <groupId>org.springframework.boot</groupId>
  33.  
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
  34.  
    </dependency>
  35.  
    <dependency>
  36.  
    <groupId>org.projectlombok</groupId>
  37.  
    <artifactId>lombok</artifactId>
  38.  
    <version>1.18.24</version>
  39.  
    </dependency>
  40.  
    </dependencies>
学新通

四、后端代码

1.项目结构

学新通

 2.实体类

table1:

  1.  
     
  2.  
    @Data
  3.  
    @Entity
  4.  
    @Table(name = "table1")
  5.  
    public class Table1 implements Serializable {
  6.  
     
  7.  
    @Id
  8.  
    @GeneratedValue(strategy= GenerationType.IDENTITY)
  9.  
    @Column(name = "id")
  10.  
    private Integer id;
  11.  
     
  12.  
    @Column(name = "name")
  13.  
    private String name;
  14.  
     
  15.  
    @Column(name = "num")
  16.  
    private Integer num;
  17.  
     
  18.  
    }
学新通

table2类似上面一样的

3.controller层

table1Controller

  1.  
    @RestController
  2.  
    public class Table1Controller {
  3.  
    @Autowired
  4.  
    Table1Service table1Service;
  5.  
    @RequestMapping("/firstApi")
  6.  
    public List<Table1> findAll(){
  7.  
    return table1Service.findAll();
  8.  
    }
  9.  
    }

table2Controller      

  1.  
    @RestController
  2.  
    public class Table2Controller {
  3.  
    @Autowired
  4.  
    Table2Service table2Service;
  5.  
    @RequestMapping("/lastApi")
  6.  
    public List<Table2> findAll(){
  7.  
    return table2Service.findAll();
  8.  
    }
  9.  
    }
  10.  
     
  1.  
    @RestController
  2.  
    public class HelloSpringBoot {
  3.  
     
  4.  
    @RequestMapping(value="/hello",method = RequestMethod.GET)
  5.  
    public String sayHello(){
  6.  
     
  7.  
    return "Hello Spring Boot!";
  8.  
     
  9.  
    }
  10.  
     
  11.  
    @RequestMapping(value="/first",method = RequestMethod.GET)
  12.  
    public ModelAndView firstDemo(){
  13.  
    return new ModelAndView("test");
  14.  
    }
  15.  
     
  16.  
    @RequestMapping(value="/courseClickCount",method = RequestMethod.GET)
  17.  
    public ModelAndView courseClickCountStat(){
  18.  
    return new ModelAndView("demo");
  19.  
    }
  20.  
    }
学新通

4.service层

  1.  
    @Service
  2.  
    @Transactional
  3.  
    public class Table1Service {
  4.  
    @Autowired
  5.  
    Table1Dao table1Dao;
  6.  
    public List<Table1> findAll(){
  7.  
    return table1Dao.findAll();
  8.  
    }
  9.  
    }
  1.  
    @Service
  2.  
    @Transactional
  3.  
    public class Table2Service {
  4.  
    @Autowired
  5.  
    Table2Dao table2Dao;
  6.  
    public List<Table2> findAll(){
  7.  
    return table2Dao.findAll();
  8.  
    }
  9.  
    }
  10.  
     

5.dao层

都实现了jpa接口,第一个属性是实体类,第二个传入的是数据库主键的类型

学新通学新通

五、后端代码实现

树状图页面

  1.  
    <!DOCTYPE html>
  2.  
    <html lang="en">
  3.  
    <head>
  4.  
    <meta charset="UTF-8">
  5.  
    <title>Demo</title>
  6.  
    <!-- 引入jquery-->
  7.  
    <script src="https://cdn.staticfile.org/jquery/2.2.4/jquery.min.js"></script>
  8.  
    <!-- 引入 echarts.js -->
  9.  
    <script src="https://cdn.staticfile.org/echarts/4.3.0/echarts.min.js"></script>
  10.  
    </head>
  11.  
    <body>
  12.  
    <!-- 为 ECharts 准备一个具备大小(宽高)的 DOM -->
  13.  
    <div id="main" style="width: 600px;height:400px;position:absolute;top:50%;left: 50%;margin-top: -200px;margin-left: -300px;"></div>
  14.  
     
  15.  
    <script type="text/javascript">
  16.  
    var option;
  17.  
    var arr1=[]
  18.  
    var arr2=[]
  19.  
    // 基于准备好的dom,初始化echarts实例
  20.  
    var myChart = echarts.init(document.getElementById('main'));//main是<div id="main" style="width: 600px;height:400px;"></div>的id
  21.  
     
  22.  
    // 指定图表的配置项和数据
  23.  
    $.get("lastApi",function(result){
  24.  
    arr1=result;
  25.  
    $.each(result,function (index,item) {
  26.  
    arr2.push(item.name);
  27.  
    })
  28.  
    option={
  29.  
    title : {
  30.  
    text: '网络游戏热度统计',
  31.  
    subtext: '游戏获得的票数',
  32.  
    x:'center'
  33.  
    },
  34.  
    tooltip : {
  35.  
    trigger: 'item',
  36.  
    formatter: "{a} <br/>{b} : {c} ({d}%)"
  37.  
    },
  38.  
    legend: {
  39.  
    orient: 'vertical',
  40.  
    left: 'left',
  41.  
    data: arr2
  42.  
    },
  43.  
    series : [
  44.  
    {
  45.  
    name: '票数数',
  46.  
    type: 'pie',
  47.  
    radius : '55%',
  48.  
    center: ['50%', '60%'],
  49.  
    data:arr1,
  50.  
    itemStyle: {
  51.  
    emphasis: {
  52.  
    shadowBlur: 10,
  53.  
    shadowOffsetX: 0,
  54.  
    shadowColor: 'rgba(0, 0, 0, 0.5)'
  55.  
    }
  56.  
    }
  57.  
    }
  58.  
    ]
  59.  
    };
  60.  
    myChart.setOption(option);
  61.  
    })
  62.  
    // 使用刚指定的配置项和数据显示图表。
  63.  
     
  64.  
    </script>
  65.  
    </body>
  66.  
    </html>
学新通

饼状图页面

  1.  
    <!DOCTYPE html>
  2.  
    <html lang="en">
  3.  
    <head>
  4.  
    <meta charset="UTF-8">
  5.  
    <title>Title</title>
  6.  
    <script src="https://cdn.staticfile.org/jquery/2.2.4/jquery.min.js"></script>
  7.  
    <!-- 引入 echarts.js -->
  8.  
    <script src="https://cdn.staticfile.org/echarts/4.3.0/echarts.min.js"></script>
  9.  
    </head>
  10.  
    <body>
  11.  
    <!-- 为 ECharts 准备一个具备大小(宽高)的 DOM -->
  12.  
    <div id="main" style="width: 600px;height:400px;position:absolute;top:50%;left: 50%;margin-top: -200px;margin-left: -300px;"></div>
  13.  
     
  14.  
    <script type="text/javascript">
  15.  
    var option;
  16.  
    var arr1=[]
  17.  
    var arr2=[]
  18.  
    // 基于准备好的dom,初始化echarts实例
  19.  
    var myChart = echarts.init(document.getElementById('main'));//main是<div id="main" style="width: 600px;height:400px;"></div>的id
  20.  
    // 指定图表的配置项和数据
  21.  
    $.get("firstApi",function (result) {
  22.  
    $.each(result,function (index,item) {
  23.  
    arr1.push(item.name);
  24.  
    arr2.push(item.num);
  25.  
    })
  26.  
    option = {
  27.  
    title: {
  28.  
    text: '长沙市市长竞选榜'
  29.  
    },
  30.  
    tooltip: {},
  31.  
    legend: {
  32.  
    data:['票数']
  33.  
    },
  34.  
    xAxis: {
  35.  
    data: arr1
  36.  
    },
  37.  
    yAxis: {},
  38.  
    series: [{
  39.  
    name: '票数',
  40.  
    type: 'bar',
  41.  
    data: arr2
  42.  
    }]
  43.  
    };
  44.  
    // 使用刚指定的配置项和数据显示图表。
  45.  
    myChart.setOption(option);
  46.  
    })
  47.  
    </script>
  48.  
    </body>
  49.  
    </html>
学新通

到此springboot整合echarts完成!

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfhkgbg
系列文章
更多 icon
同类精品
更多 icon
继续加载