index2.html 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>温度监控</title>
  6. <script type="text/javascript" src="./static/echarts.min.js"></script>
  7. </head>
  8. <body>
  9. <div id="charts" class="chart-container" style="width:900px; height:500px;"></div>
  10. <script>
  11. // init echarts
  12. const dom = document.getElementById('charts')
  13. const chart = echarts.init(dom)
  14. const Arr = []
  15. const option = {
  16. backgroundColor: 'white',
  17. grid: {
  18. top: '30%',
  19. left: '20%',
  20. right: '20%',
  21. bottom: '30%',
  22. containLabel: true,
  23. },
  24. tooltip: {
  25. trigger: "axis",
  26. },
  27. xAxis: {
  28. data: [],
  29. axisLine: {
  30. show: true,
  31. lineStyle: {
  32. color: '#E5E6EB',
  33. }
  34. },
  35. axisLabel: {
  36. color: '#86909C',
  37. fontSize: 12,
  38. },
  39. },
  40. yAxis: [{
  41. type: 'value',
  42. nameTextStyle: {
  43. color: "#000",
  44. fontSize: 14,
  45. padding: [0, 0, 0, 30]
  46. },
  47. axisLabel: {
  48. color: '#4E5969',
  49. fontSize: 12
  50. },
  51. splitLine: { //网格线
  52. "show": false,
  53. }
  54. }],
  55. series: [
  56. {
  57. type: 'line',
  58. smooth: true,
  59. showSymbol: false,
  60. barWidth: 20,
  61. data: [],
  62. itemStyle: {
  63. normal: {
  64. color: "#4080FF",
  65. width: 2
  66. },
  67. },
  68. }
  69. ]
  70. }
  71. function getData() {
  72. fetch('http://127.0.0.1:5673/data', { method: 'GET', Headers: { 'Content-Type': 'application/json' } }).then((res => res.json())).then(res => {
  73. if (Arr.length > 300) Arr.shift()
  74. Arr.push(res)
  75. const series = Arr.map(el=>el.value)
  76. const xAxis = Arr.map(el => el.time)
  77. option.xAxis.data = xAxis
  78. option.series[0].data = series
  79. chart.setOption(option)
  80. console.log('服务器回来的数据',res,Arr);
  81. })
  82. }
  83. getData()
  84. // 每3秒请求一次数据
  85. setInterval(() => {
  86. getData()
  87. }, 3000);
  88. </script>
  89. </body>
  90. </html>