two.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <template>
  2. <div id="two" :style="{width: '100%', height: '100%'}"></div>
  3. </template>
  4. <script type="text/ecmascript-6">
  5. export default {
  6. data(){
  7. return {
  8. chartInstance: null,
  9. allData:null, // 服务器返回的数据
  10. currentPage: 1, //当前显示的页数, 当修改页数时, 数据动态刷新
  11. totalPage: 0, //一共多少也
  12. timerId:null //定时器
  13. }
  14. },
  15. mounted(){
  16. this.initChart() //函数调用
  17. this.getData()
  18. // 对窗口大小变化的事件进行监听
  19. // window.addEventListener('resize', this.screenAdapter)
  20. },
  21. // Vue生命周期 - 销毁时
  22. destroyed () {
  23. clearInterval(this.timerId)
  24. // 在组件销毁的时候, 需要将监听器取消掉
  25. // window.removeEventListener('resize', this.screenAdapter)
  26. },
  27. methods:{
  28. initChart(){
  29. this.chartInstance = this.$echarts.init(document.getElementById('two'))
  30. //对图表对象进行鼠标事件监听
  31. this.chartInstance.on('mousecver', () =>{
  32. this.clearInterval(this.timerId)
  33. })
  34. this.chartInstance.on('mouseout', () => {
  35. this.startInterval()
  36. })
  37. },
  38. async getData () {
  39. // 请求数据
  40. const { data: ret } = await this.$http.get('hbase/getJSON')
  41. this.allData = ret
  42. // 对数据排序
  43. this.allData.sort((a,b) => {{
  44. return a.value-b.value //从小到大排序
  45. }})
  46. // 每1个元素显示一页
  47. this.totalPage = this.allData.length % 1 ===0 ? this.allData.length /1 : this.allData.length / 1 +1
  48. // 获取完数据, 更新图标
  49. this.updateChart()
  50. // 启动定时器, 每隔三秒改变currentPage, 进而更新图表
  51. this.startInterval()
  52. },
  53. updateChart(){
  54. // 每一页显示1个数据, 获取1个数据
  55. const start = (this.currentPage - 1) * 1
  56. const end = this.currentPage * 1
  57. const showData = this.allData.slice(start, end)
  58. // const showValue = this.showData.map((item) =>{
  59. // return item
  60. // })
  61. const showValue = showData[0].jiedidianliu
  62. const option = {
  63. tooltip : {
  64. formatter: "{a} <br/>{c} {b}"
  65. },
  66. toolbox: {
  67. show: true,
  68. feature: {
  69. restore: {show: true},
  70. saveAsImage: {show: true}
  71. }
  72. },
  73. series : [
  74. {
  75. name: '截低电流:',
  76. type: 'gauge',
  77. z: 3,
  78. min: 0,
  79. max: 1200,
  80. splitNumber: 10,
  81. radius: '100%',
  82. axisLine: {
  83. lineStyle: {
  84. width: 10,
  85. color: [[0.1, 'green'], [0.2, 'yellow'],[0.3, 'orange'],[0.4,'#db555e'],[0.5,'#ba3779'],[1.1,'#881326'] ]
  86. }
  87. },
  88. axisTick: {
  89. length: 15,
  90. lineStyle: {
  91. color: 'auto'
  92. }
  93. },
  94. //刻度分割线样式
  95. splitLine: {
  96. length: 10,
  97. lineStyle: {
  98. color: 'white'
  99. }
  100. },
  101. //刻度数字样式
  102. axisLabel: {
  103. fontWeight:'bold',
  104. color: '#0085FF',
  105. fontSize:15
  106. },
  107. detail : {
  108. //说明数字大小
  109. formatter: function (value) {
  110. return value;
  111. },
  112. offsetCenter:['0%','80%'],
  113. fontWeight: 'bolder',
  114. borderRadius: 3,
  115. backgroundColor: '#0085FF',
  116. fontSize:30,
  117. width: 100,
  118. color: 'white',
  119. padding:[5,15,2,15]
  120. },
  121. data:[{
  122. value:showValue
  123. }]
  124. },
  125. ]
  126. }
  127. this.chartInstance.setOption(option)
  128. },
  129. startInterval () {
  130. // 每个3秒执行一次
  131. setInterval(() => {
  132. // 保险操作
  133. if(this.timeId){
  134. clearInterval(this.timerId)
  135. }
  136. this.currentPage++
  137. // 当页数超过总页数是, 从头开始
  138. if(this.currentPage > this.totalPage){
  139. this.currentPage = 1
  140. }
  141. this.updateChart()
  142. },3000)
  143. }
  144. }
  145. }
  146. </script>
  147. <style scoped>
  148. </style>