当前位置:   article > 正文

SpringBoot框架之SpringBoot+Echarts使用案例入门_springboot echarts

springboot echarts

为能够正确运行本次案例,希望大家认真看,首次学习务必按照本次教程的包名跟类名一样,防止出现不必要的bug哈!!!

一、基础需要:

        SpringBoot基础知识

        javaWeb基础知识

        SpringBoot创建Web基础

二、下载echarts

        1.在Github下载echarts所需的js文件:https://github.com/apache/echarts/tree/5.1.2

      2.如图进入下载好后,进入dist目录下

         3.在resourse的static文件创建js文件夹,(因为springboot默认static跟templates是同级,所有js自动和static合并),然后把echart.min.js文件放到static.js下

三、添加Echarts官方示例的html文件

         1.方便大家使用,可以直接把下面的test.html文件复制(需要在下面注意echarts.min.js的路径)

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>ECharts</title>
  6. <!-- 引入 echarts.js -->
  7. <script type="text/javascript" src="js/echarts.min.js"></script>
  8. </head>
  9. <body>
  10. <!-- 为ECharts准备一个具备大小(宽高)的Dom -->
  11. <div id="main" style="width: 1000px;height:400px;"></div>
  12. <script type="text/javascript">
  13. //初始化echarts实例
  14. var myChart = echarts.init(document.getElementById('main'));
  15. // 指定图表的配置项和数据
  16. var option = {
  17. title: {
  18. text: 'ECharts 入门示例'
  19. },
  20. tooltip: {},
  21. legend: {
  22. data:['销量']
  23. },
  24. xAxis: {
  25. data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
  26. },
  27. yAxis: {},
  28. series: [{
  29. name: '销量',
  30. type: 'bar',
  31. data: [15, 20, 36, 10, 10, 20]
  32. }]
  33. };
  34. // 使用刚指定的配置项和数据显示图表。
  35. myChart.setOption(option);
  36. </script>
  37. </body>
  38. </html>

        2.把test.html文件必须放在resources下面的templates下:

 

四、添加thymeleaf模板和创建Controller

        1.为了能够网页显示html文件,必须使用thymeleaf模板,在pom文件中添加如下依赖:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  4. </dependency>

         2.在Application所在包下创建一个Echarts包

          3.创建一个名字为EchartsTest的 Controller

  1. package springboot.demo.Echarts;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RequestMethod;
  4. import org.springframework.web.bind.annotation.RestController;
  5. import org.springframework.web.servlet.ModelAndView;
  6. /**
  7. * @Author 海龟
  8. * @Date 2021/8/20 22:10
  9. * @Desc Echarts入门案例
  10. */
  11. @RestController
  12. public class EchartsTest {
  13. @RequestMapping(value = "/echarts",method = RequestMethod.GET)
  14. public ModelAndView FirtstEchartsTest(){
  15. //test为在为你的html文件名字,SpringBoot会自动找到这个html文件
  16. return new ModelAndView("test");
  17. }
  18. }

 五、运行SpringBoot

 六、打开浏览器输入:localhost:8080/echarts

SpringBoot+Echarts使用案例入门就到这里结束了,本次教程只是告诉大家怎么简单在SpringBoot搭建Echarts,更多好玩复杂的Echarts需要大家自己在此基础上慢慢探索哈,感谢大家!!

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

闽ICP备14008679号