当前位置:   article > 正文

springboot+echarts +mysql制作数据可视化大屏(四图)_mysql echarts可视化大屏 开发

mysql echarts可视化大屏 开发

作者水平低,如有错误,恳请指正!谢谢!!!!!

项目简单,适合大学生参考

分类专栏还有其它的可视化博客哦!

专栏地址:https://blog.csdn.net/qq_55906442/category_11906804.html?spm=1001.2014.3001.5482

成果展示:

 一、数据源

1)可以使用自己的MySQL数据库;

2)使用我提供的数据。(要数据私信/留言——>留下邮箱即可)

二、所需工具

MySQL、IDEA、jdk1.8、Maven等等,总之编写工具要准备好,环境要搭建好

三、项目框架搭建

参考我博客的项目框架搭建,从3.1看到4.3即可springboot+mybatis+echarts +mysql制作数据可视化大屏_spring + 可视化大屏_一个人的牛牛的博客-CSDN博客

四、代码编写

代码简单,后端代码都写在一起了,没有区分controller等等,前端也是一样,没有单独写js等等。

4.1 区域销量统计条形图

4.1.1 后端

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.jdbc.core.JdbcTemplate;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. @RestController
  10. public class Big1 {
  11. private final JdbcTemplate jdbcTemplate;
  12. @Autowired
  13. public Big1(JdbcTemplate jdbcTemplate) {
  14. this.jdbcTemplate = jdbcTemplate;
  15. }
  16. @GetMapping("/chart-data")
  17. public Map<String, Object> getChartData() {
  18. String query = "SELECT region_name, COUNT(*) AS count FROM ads_area_topic GROUP BY region_name";
  19. List<Map<String, Object>> result = jdbcTemplate.queryForList(query);
  20. List<String> labels = new ArrayList<>();
  21. List<Integer> values = new ArrayList<>();
  22. for (Map<String, Object> row : result) {
  23. String regionName = (String) row.get("region_name");
  24. Integer count = ((Number) row.get("count")).intValue();
  25. labels.add(regionName);
  26. values.add(count);
  27. }
  28. Map<String, Object> data = new HashMap<>();
  29. data.put("labels", labels);
  30. data.put("values", values);
  31. return data;
  32. }
  33. }

验证接口:运行项目,浏览器访问http://localhost:8080/chart-data

4.1.2 前端

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>区域销量统计</title>
  5. <meta charset="UTF-8">
  6. <script src="https://cdn.jsdelivr.net/npm/echarts@5.2.2/dist/echarts.min.js"></script>
  7. <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  8. <!-- <script src="static/js/echarts.min.js"></script>-->
  9. <!-- <script src="static/js/jquery-3.6.0.min.js"></script>-->
  10. <style>
  11. html, body {
  12. height: 100%;
  13. margin: 0;
  14. padding: 0;
  15. }
  16. #chart {
  17. width: 100%;
  18. height: 100%;
  19. min-height: 280px; /* 设置最小高度,防止内容过小时显示异常 */
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <div id="chart"></div>
  25. <script type="text/javascript">
  26. var chart = echarts.init(document.getElementById('chart'));
  27. // 监听窗口大小变化事件
  28. window.addEventListener('resize', function() {
  29. chart.resize(); // 调整图表大小
  30. });
  31. // 使用Ajax异步获取数据
  32. $.ajax({
  33. url: '/chart-data',
  34. type: 'GET',
  35. dataType: 'json',
  36. success: function(data) {
  37. var option = {
  38. title: {
  39. text: '区域销量统计',
  40. textStyle: {
  41. textAlign: 'center'
  42. }
  43. },
  44. tooltip: {},
  45. xAxis: {
  46. data: data.labels
  47. },
  48. yAxis: {},
  49. series: [{
  50. name: 'Count',
  51. type: 'bar',
  52. data: data.values,
  53. itemStyle: {
  54. color: function(params) {
  55. // 设置背景颜色
  56. var colorList = ['#C1232B', '#B5C334', '#FCCE10', '#E87C25', '#27727B', '#FE8463', '#9BCA63', '#FAD860', '#F3A43B', '#60C0DD'];
  57. return colorList[params.dataIndex];
  58. }
  59. }
  60. }]
  61. };
  62. chart.setOption(option);
  63. }
  64. });
  65. </script>
  66. </body>
  67. </html>

验证页面:运行项目,浏览器访问http://localhost:8080/big1.html

4.2  订单金额折线图

4.2.1 后端

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.jdbc.core.JdbcTemplate;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. @RestController
  10. public class Big2Controller {
  11. private final JdbcTemplate jdbcTemplate;
  12. @Autowired
  13. public Big2Controller(JdbcTemplate jdbcTemplate) {
  14. this.jdbcTemplate = jdbcTemplate;
  15. }
  16. @GetMapping("/chart-data2")
  17. public Map<String, Object> getChartData() {
  18. String query = "SELECT dt, order_amount FROM ads_order_daycount";
  19. List<Map<String, Object>> result = jdbcTemplate.queryForList(query);
  20. List<String> labels = new ArrayList<>();
  21. List<Integer> values = new ArrayList<>();
  22. for (Map<String, Object> row : result) {
  23. String dt = row.get("dt").toString();
  24. Integer orderAmount = ((Number) row.get("order_amount")).intValue();
  25. labels.add(dt);
  26. values.add(orderAmount);
  27. }
  28. Map<String, Object> data = new HashMap<>();
  29. data.put("labels", labels);
  30. data.put("values", values);
  31. return data;
  32. }
  33. }

 验证接口:运行项目,浏览器访问​​​​​​​http://localhost:8080/chart-data2

4.2.2 前端

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>折线图</title>
  6. <style>
  7. html, body {
  8. height: 100%;
  9. margin: 0;
  10. padding: 0;
  11. }
  12. #chart-container {
  13. width: 100%;
  14. height: 100%;
  15. min-height: 300px; /* 设置最小高度,防止内容过小时显示异常 */
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <div id="chart-container"></div>
  21. <script src="https://cdn.jsdelivr.net/npm/echarts@5.2.2/dist/echarts.min.js"></script>
  22. <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  23. <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
  24. <script>
  25. // 使用 Axios 发送 AJAX 请求
  26. axios.get('/chart-data2')
  27. .then(function (response) {
  28. // 处理从后端返回的数据
  29. const data = response.data;
  30. // 提取 X 轴和 Y 轴数据
  31. const xData = data.labels;
  32. const yData = data.values;
  33. // 创建图表
  34. const chartContainer = document.getElementById('chart-container');
  35. const chart = echarts.init(chartContainer);
  36. // 监听窗口大小变化事件
  37. window.addEventListener('resize', function() {
  38. chart.resize(); // 调整图表大小
  39. });
  40. const option = {
  41. title: {
  42. text: '订单金额折线图'
  43. },
  44. xAxis: {
  45. type: 'category',
  46. data: xData
  47. },
  48. yAxis: {
  49. type: 'value'
  50. },
  51. series: [{
  52. type: 'line',
  53. data: yData,
  54. areaStyle: {
  55. color: 'rgba(0, 128, 255, 0.3)' // 设置背景颜色
  56. }
  57. }]
  58. };
  59. // 渲染图表
  60. chart.setOption(option);
  61. })
  62. .catch(function (error) {
  63. console.log(error);
  64. });
  65. </script>
  66. </body>
  67. </html>

验证页面:运行项目,浏览器访问​​​​​​​​​​​​​​http://localhost:8080/big2.html

4.3  平均支付时间统计

4.3.1 后端

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.jdbc.core.JdbcTemplate;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. @RestController
  10. public class Big3Controller {
  11. private final JdbcTemplate jdbcTemplate;
  12. @Autowired
  13. public Big3Controller(JdbcTemplate jdbcTemplate) {
  14. this.jdbcTemplate = jdbcTemplate;
  15. }
  16. @GetMapping("/chart-data3")
  17. public Map<String, Object> getChartData() {
  18. String query = "SELECT dt, payment_avg_time FROM ads_payment_daycount";
  19. List<Map<String, Object>> result = jdbcTemplate.queryForList(query);
  20. List<String> labels = new ArrayList<>();
  21. List<Integer> values = new ArrayList<>();
  22. for (Map<String, Object> row : result) {
  23. String dt = row.get("dt").toString();
  24. Integer paymentAvgTime = ((Number) row.get("payment_avg_time")).intValue();
  25. labels.add(dt);
  26. values.add(paymentAvgTime);
  27. }
  28. Map<String, Object> data = new HashMap<>();
  29. data.put("labels", labels);
  30. data.put("values", values);
  31. return data;
  32. }
  33. }

 验证接口:运行项目,浏览器访问​​​​​​​​​​​​​​http://localhost:8080/chart-data3

4.3.2 前端

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>平均支付时间统计</title>
  5. <meta charset="UTF-8">
  6. <script src="https://cdn.jsdelivr.net/npm/echarts@5.2.2/dist/echarts.min.js"></script>
  7. <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  8. <style>
  9. html, body {
  10. height: 100%;
  11. margin: 0;
  12. padding: 0;
  13. }
  14. #chart {
  15. width: 100%;
  16. height: 100%;
  17. min-height: 300px; /* 设置最小高度,防止内容过小时显示异常 */
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div id="chart"></div>
  23. <script type="text/javascript">
  24. var chart = echarts.init(document.getElementById('chart'));
  25. // 监听窗口大小变化事件
  26. window.addEventListener('resize', function() {
  27. chart.resize(); // 调整图表大小
  28. });
  29. // 使用Ajax异步获取数据
  30. $.ajax({
  31. url: '/chart-data3',
  32. type: 'GET',
  33. dataType: 'json',
  34. success: function(data) {
  35. var option = {
  36. title: {
  37. text: '平均支付时间统计',
  38. textStyle: {
  39. textAlign: 'center'
  40. }
  41. },
  42. tooltip: {},
  43. angleAxis: {},
  44. radiusAxis: {
  45. type: 'category',
  46. data: data.labels
  47. },
  48. polar: {},
  49. series: [{
  50. name: 'Avg Time',
  51. type: 'bar',
  52. data: data.values,
  53. coordinateSystem: 'polar',
  54. itemStyle: {
  55. color: function(params) {
  56. // 设置背景颜色
  57. var colorList = ['#C1232B', '#B5C334', '#FCCE10', '#E87C25', '#27727B', '#FE8463', '#9BCA63', '#FAD860', '#F3A43B', '#60C0DD'];
  58. return colorList[params.dataIndex % colorList.length];
  59. }
  60. }
  61. }]
  62. };
  63. chart.setOption(option);
  64. }
  65. });
  66. </script>
  67. </body>
  68. </html>

 验证页面:运行项目,浏览器访问​​​​​​​http://localhost:8080/big3.html

4.4  订单金额统计

4.4.1 后端

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.jdbc.core.JdbcTemplate;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. @RestController
  10. public class Big4Controller {
  11. private final JdbcTemplate jdbcTemplate;
  12. @Autowired
  13. public Big4Controller(JdbcTemplate jdbcTemplate) {
  14. this.jdbcTemplate = jdbcTemplate;
  15. }
  16. @GetMapping("/chart-data4")
  17. public Map<String, Object> getChartData() {
  18. String query = "SELECT dt, order_amount FROM ads_payment_daycount";
  19. List<Map<String, Object>> result = jdbcTemplate.queryForList(query);
  20. List<String> labels = new ArrayList<>();
  21. List<Integer> values = new ArrayList<>();
  22. for (Map<String, Object> row : result) {
  23. String dt = row.get("dt").toString();
  24. Integer orderAmount = ((Number) row.get("order_amount")).intValue();
  25. labels.add(dt);
  26. values.add(orderAmount);
  27. }
  28. Map<String, Object> data = new HashMap<>();
  29. data.put("labels", labels);
  30. data.put("values", values);
  31. return data;
  32. }
  33. }

 验证接口:运行项目,浏览器访问​​​​​​​​​​​​​​http://localhost:8080/chart-data4

4.4.2 前端

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>订单金额统计</title>
  5. <meta charset="UTF-8">
  6. <script src="https://cdn.jsdelivr.net/npm/echarts@5.2.2/dist/echarts.min.js"></script>
  7. <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  8. <style>
  9. html, body {
  10. height: 100%;
  11. margin: 0;
  12. padding: 0;
  13. }
  14. #chart {
  15. width: 100%;
  16. height: 100%;
  17. min-height: 300px;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div id="chart"></div>
  23. <script type="text/javascript">
  24. var chart = echarts.init(document.getElementById('chart'));
  25. // 监听窗口大小变化事件
  26. window.addEventListener('resize', function() {
  27. chart.resize(); // 调整图表大小
  28. });
  29. // 使用Ajax异步获取数据
  30. $.ajax({
  31. url: '/chart-data4',
  32. type: 'GET',
  33. dataType: 'json',
  34. success: function(data) {
  35. var option = {
  36. title: {
  37. text: '订单金额统计',
  38. textStyle: {
  39. textAlign: 'center'
  40. }
  41. },
  42. tooltip: {
  43. trigger: 'item',
  44. formatter: '{b}: {c} ({d}%)'
  45. },
  46. series: [{
  47. name: '订单金额',
  48. type: 'pie',
  49. radius: '50%',
  50. data: data.values.map(function(value, index) {
  51. return {
  52. name: data.labels[index],
  53. value: value
  54. };
  55. })
  56. }]
  57. };
  58. chart.setOption(option);
  59. }
  60. });
  61. </script>
  62. </body>
  63. </html>

验证页面:运行项目,浏览器访问​​​​​​​http://localhost:8080/big4.html

五、大屏编写

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>大屏可视化</title>
  6. <style>
  7. /* CSS样式 */
  8. body, html {
  9. height: 100%;
  10. width: 100%;
  11. margin: 0;
  12. padding: 0;
  13. }
  14. #header {
  15. width: 100%;
  16. height: 8%;
  17. display: flex;
  18. justify-content: center;
  19. align-items: center;
  20. background-color: #f0f0f0;
  21. font-size: 20px;
  22. font-weight: bold;
  23. }
  24. #content {
  25. width: 100%;
  26. height: 92%;
  27. display: flex;
  28. flex-wrap: wrap;
  29. justify-content: space-around;
  30. align-items: center;
  31. padding: 20px;
  32. }
  33. .chart-container {
  34. width: 49%;
  35. height: 48%;
  36. background-color: #ffffff;
  37. margin-bottom: 20px;
  38. box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.2);
  39. }
  40. .chart-container:not(:last-child) {
  41. margin-right: 5px;
  42. }
  43. </style>
  44. </head>
  45. <body style="background-color: bisque">
  46. <div id="header"><h2>销售数据平面一览大屏</h2></div>
  47. <div id="content">
  48. <div class="chart-container" id="chart1">
  49. <iframe src="big1.html" style="width: 100%; height: 100%; border: none;"></iframe>
  50. </div>
  51. <div class="chart-container" id="chart2">
  52. <iframe src="big2.html" style="width: 100%; height: 100%; border: none;"></iframe>
  53. </div>
  54. <div class="chart-container" id="chart3">
  55. <iframe src="big3.html" style="width: 100%; height: 100%; border: none;"></iframe>
  56. </div>
  57. <div class="chart-container" id="chart4">
  58. <iframe src="big4.html" style="width: 100%; height: 100%; border: none;"></iframe>
  59. </div>
  60. </div>
  61. <script src="https://cdn.jsdelivr.net/npm/d3@7.0.0/dist/d3.min.js"></script>
  62. <script>
  63. </script>
  64. </body>
  65. </html>

运行项目,浏览器访问​​​​​​​http://localhost:8080/large%20screen.html

注:http://localhost:8080/加上HTML的文件名都能够查看相应的图!

要码源的私聊/评论留下邮箱号,压缩包包括项目源码、数据sql文件,readme.txt。

声明:作品仅可作学习使用​​​​​​​。

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

闽ICP备14008679号