0


Echarts实现动态折线图的定时刷新

在做项目的过程中,遇到一个需求:要从后台读取数据,并对echarts进行实时更新。先来看下实现的效果图:

首先来看一下没有和后台交互的echarts动态折线图如何实现,代码如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title></title>
  6. <link rel="stylesheet" href="bootstrap/css/bootstrap.css" />
  7. <script src="js/jquery-3.5.1.js"></script>
  8. <script src="js/echarts.min.js"></script>
  9. <script src="bootstrap/js/bootstrap.min.js"></script>
  10. <style>
  11. body{
  12. background-color: #02102c;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div>
  18. <div class="row" style="margin:20px 20px">
  19. <div class="col-md-5 col-md-offset-3" style="box-shadow: 2px 2px 5px #bbb;padding:10px 10px;border-radius:5px">
  20. <div style="height:500px;" id="area" class="card-box"></div>
  21. </div>
  22. </div>
  23. </div>
  24. </div>
  25. </body>
  26. <script>
  27. $(function(){
  28. var showTime = [];
  29. var showValue = [];
  30. var showArea = document.getElementById("area");
  31. var showChart = echarts.init(showArea);
  32. //先默认添加10个元素
  33. for(var i=0; i < 10; i++){
  34. showTime.push(getTime());
  35. showValue.push(Math.random());
  36. }
  37. var showOption = {
  38. xAxis: {
  39. type: 'category',
  40. data: showTime,
  41. axixLine:{
  42. show:true,
  43. lineStyle:{
  44. color:"#FFFFFF"
  45. }
  46. },
  47. axisLabel:{
  48. show:true,
  49. textStyle:{
  50. color:"#FFFFFF",
  51. fontSize:16
  52. }
  53. },
  54. },
  55. yAxis: {
  56. type: 'value',
  57. axixLine:{
  58. show:true,
  59. lineStyle:{
  60. color:"#FFFFFF",
  61. wdith:5,
  62. type:"solid"
  63. }
  64. },
  65. axisLabel:{
  66. show:true,
  67. textStyle:{
  68. color:"#FFFFFF",
  69. fontSize:16
  70. }
  71. },
  72. },
  73. series: [
  74. {
  75. data: showValue,
  76. type: 'line',
  77. symbolSize: 10,
  78. lineStyle: {
  79. color: '#FFFFFF',
  80. width: 4
  81. }
  82. }
  83. ]
  84. };
  85. showChart.setOption(showOption);
  86. //获取时间
  87. function getTime(){
  88. let time = new Date();
  89. return time.toLocaleString();
  90. }
  91. //定时更新
  92. setInterval(function(){
  93. //当长度大于10时,去除数组首元素
  94. if(showTime.length > 10){
  95. showTime.shift();showValue.shift();
  96. }
  97. //将新值添加到数组中
  98. showTime.push(getTime());
  99. showValue.push(Math.random());
  100. //重新绘制
  101. showChart.setOption({
  102. xAxis: {
  103. type: 'category',
  104. data: showTime,
  105. axixLine:{
  106. show:true,
  107. lineStyle:{
  108. color:"#FFFFFF"
  109. }
  110. },
  111. axisLabel:{
  112. show:true,
  113. textStyle:{
  114. color:"#FFFFFF",
  115. fontSize:16
  116. }
  117. },
  118. },
  119. yAxis: {
  120. type: 'value',
  121. axixLine:{
  122. show:true,
  123. lineStyle:{
  124. color:"#FFFFFF",
  125. wdith:5,
  126. type:"solid"
  127. }
  128. },
  129. axisLabel:{
  130. show:true,
  131. textStyle:{
  132. color:"#FFFFFF",
  133. fontSize:16
  134. }
  135. },
  136. },
  137. series: [
  138. {
  139. data: showValue,
  140. type: 'line',
  141. symbolSize: 10,
  142. lineStyle: {
  143. color: '#FFFFFF',
  144. width: 4
  145. }
  146. }
  147. ]
  148. });
  149. }, 2000);
  150. });
  151. </script>
  152. </html>

前面实现的是动态折现图并没有和后台交互,接下来看一下和后台交互的代码是如何实现的:

(1)首先是前端页面的修改

  1. setInterval(function(){
  2. $.ajax({
  3. url:"后台的URL",
  4. data:{},
  5. async: true,
  6. dataType:"json",
  7. // 回调函数
  8. success:function (result) {
  9. showTime.shift();
  10. showValue.shift();
  11. showTime.push(getTime());
  12. showValue.push(result[0]);
  13. drawSituation();
  14. },error:function(){
  15. console.log("error");
  16. }
  17. });
  18. }, 1000);
  19. function drawSituation(){
  20. showChart.setOption({
  21. xAxis: {
  22. data: showTime,
  23. },
  24. series: [
  25. {
  26. data: showValue,
  27. }
  28. ]
  29. });

(2)后台代码的实现

  1. @PrivilegeANTT(desc="测试")
  2. public void test() throws Exception {
  3. List<Double> list = new ArrayList<>();
  4. list.add(Math.random());
  5. this.renderJSON(list);
  6. }

后台只需要将数据以JSON方式返回即可。


本文转载自: https://blog.csdn.net/weixin_47382783/article/details/128603551
版权归原作者 给你两窝窝 所有, 如有侵权,请联系我们删除。

“Echarts实现动态折线图的定时刷新”的评论:

还没有评论