pie-echarts.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <template>
  2. <view class="charts-box">
  3. <qiun-data-charts type="pie" :eopts="opts" :chartData="chartData" :echartsH5="true" />
  4. </view>
  5. </template>
  6. <script>
  7. import {
  8. nowDate,
  9. timeToDate
  10. } from '../../utils/date.js'
  11. export default {
  12. props: {
  13. echartsData: Object
  14. },
  15. data() {
  16. return {
  17. chartData: {},
  18. //您可以通过修改 config-ucharts.js 文件中下标为 ['ring'] 的节点来配置全局默认参数,如都是默认参数,此处可以不传 opts 。实际应用过程中 opts 只需传入与全局默认参数中不一致的【某一个属性】即可实现同类型的图表显示不同的样式,达到页面简洁的需求。
  19. opts: {
  20. tooltip: {
  21. trigger: 'item'
  22. },
  23. legend: {
  24. type: 'scroll',
  25. orient: 'horizontal',
  26. top: 'bottom'
  27. }
  28. },
  29. timeNum: 10,
  30. pieTime: null
  31. };
  32. },
  33. mounted() {
  34. this.getEmit()
  35. },
  36. beforeDestroy() {
  37. clearInterval(this.pieTime)
  38. this.pieTime = null
  39. },
  40. methods: {
  41. getServerData() {
  42. const chartData = this.echartsData.chartItemList[0]
  43. chartData.valueTimeList = chartData.valueTimeList.map(item => {
  44. return item.substring(0, this.timeNum)
  45. })
  46. const {
  47. valueList,
  48. valueTimeList
  49. } = chartData
  50. let data = valueTimeList.map((item, i) => {
  51. return {
  52. name: item,
  53. value: valueList[i]
  54. }
  55. })
  56. let chartDataPie = {
  57. "series": [{
  58. 'type': 'pie',
  59. radius: '55%',
  60. label: {
  61. show: false
  62. },
  63. data: data,
  64. center: ['50%', '30%']
  65. }]
  66. }
  67. this.chartData = chartDataPie
  68. },
  69. getEmit() {
  70. const {
  71. bucketType,
  72. bucketValue
  73. } = this.echartsData
  74. let time = null
  75. switch (bucketType) {
  76. case 0:
  77. time = 86400000 * bucketValue
  78. this.timeNum = 4
  79. break;
  80. case 1:
  81. time = 3600000 * bucketValue
  82. this.timeNum = 13
  83. break;
  84. case 2:
  85. time = 60000 * bucketValue
  86. this.timeNum = 16
  87. break;
  88. case 3:
  89. time = 1000 * bucketValue
  90. this.timeNum = 19
  91. break;
  92. }
  93. this.getServerData();
  94. this.pieTime = setInterval(() => {
  95. let chartParams = {
  96. id: this.echartsData.id,
  97. startTime: timeToDate(new Date().getTime() - 3600000),
  98. endTime: nowDate()
  99. }
  100. uni.$http.get('/chart/getChartById', chartParams).then(res => {
  101. const data = res.data
  102. if (data.code === 200) {
  103. let chartItemList = data.chartItemList;
  104. chartItemList.forEach((item, index) => {
  105. if (item.itemId === this.echartsData[
  106. index].itemId) {
  107. if (item.valueList.length !== 0) {
  108. //执行将新数据添加进去,旧数据去除一个
  109. this.echartsData[index].valueList =
  110. this.echartsData[index].valueList
  111. .concat(item.valueList)
  112. this.echartsData[
  113. index]
  114. .valueList.splice(0, item
  115. .valueList.length)
  116. this.echartsData[index]
  117. .valueTimeList = this.echartsData[
  118. index]
  119. .valueTimeList.concat(item
  120. .valueTimeList)
  121. this.echartsData[
  122. index]
  123. .valueTimeList.splice(0,
  124. item
  125. .valueTimeList.length)
  126. }
  127. }
  128. })
  129. this.getServerData()
  130. }
  131. })
  132. }, time)
  133. }
  134. }
  135. };
  136. </script>
  137. <style scoped>
  138. /* 请根据实际需求修改父元素尺寸,组件自动识别宽高 */
  139. .charts-box {
  140. width: 100%;
  141. height: 540rpx;
  142. }
  143. </style>