bar-echarts.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <template>
  2. <view class="charts-box">
  3. <qiun-data-charts type="column" :eopts="opts" :chartData="chartData" :ontouch="true" :onzoom="true"
  4. :echartsH5="true" />
  5. </view>
  6. </template>
  7. <script>
  8. import {
  9. nowDate,
  10. timeToDate
  11. } from '../../utils/date.js'
  12. export default {
  13. props: {
  14. echartsData: Object
  15. },
  16. data() {
  17. return {
  18. chartData: {},
  19. //您可以通过修改 config-ucharts.js 文件中下标为 ['column'] 的节点来配置全局默认参数,如都是默认参数,此处可以不传 opts 。实际应用过程中 opts 只需传入与全局默认参数中不一致的【某一个属性】即可实现同类型的图表显示不同的样式,达到页面简洁的需求。
  20. opts: {
  21. color: ["#1890FF", "#91CB74", "#FAC858", "#EE6666", "#73C0DE", "#3CA272", "#FC8452", "#9A60B4",
  22. "#ea7ccc"
  23. ],
  24. padding: [15, 15, 0, 5],
  25. enableScroll: true,
  26. legend: {
  27. type: 'scroll',
  28. pageIconSize: 18,
  29. margin: 20,
  30. itemGap: 20,
  31. lineHeight: 20,
  32. top: 0
  33. },
  34. grid: {
  35. bottom: '25%',
  36. top: "50px",
  37. left: "45px",
  38. right: "30px"
  39. },
  40. xAxis: {
  41. disableGrid: true,
  42. scrollShow: false,
  43. itemCount: 2,
  44. scrollAlign: 'right'
  45. // rotateLabel: true,
  46. // rotateAngle: -20
  47. },
  48. yAxis: {
  49. data: [{
  50. min: 0
  51. }]
  52. },
  53. extra: {
  54. column: {
  55. type: "group",
  56. width: 30,
  57. activeBgColor: "#000000",
  58. activeBgOpacity: 0.08,
  59. seriesGap: 10
  60. }
  61. },
  62. tooltip: {
  63. trigger: 'axis',
  64. position:'inside'
  65. // axisPointer: {
  66. // type: 'cross',
  67. // label: {
  68. // backgroundColor: '#6a7985'
  69. // }
  70. // }
  71. },
  72. dataZoom: [{
  73. type: 'slider',
  74. show: true,
  75. xAxisIndex: [0],
  76. start: 90,
  77. end: 100
  78. },
  79. {
  80. type: 'slider',
  81. show: true,
  82. yAxisIndex: [0],
  83. left: '93%',
  84. start: 60,
  85. end: 100
  86. },
  87. {
  88. type: 'inside',
  89. xAxisIndex: [0],
  90. start: 90,
  91. end: 100
  92. },
  93. {
  94. type: 'inside',
  95. yAxisIndex: [0],
  96. start: 60,
  97. end: 100
  98. }
  99. ],
  100. },
  101. timeNum: 10,
  102. barTime: null
  103. };
  104. },
  105. mounted() {
  106. this.getEmit();
  107. },
  108. beforeDestroy() {
  109. clearInterval(this.barTime)
  110. this.barTime = null
  111. },
  112. methods: {
  113. getServerData() {
  114. const chartData = this.echartsData.chartItemList
  115. const timeData = chartData[0].valueTimeList
  116. const time = timeData.map(item => {
  117. return item.substring(0, 19)
  118. })
  119. const series = chartData.map(item => {
  120. return {
  121. name: item.describe ? item.describe : item.itemName,
  122. data: item.valueList
  123. }
  124. })
  125. this.opts.xAxis.data = time
  126. let res = {
  127. categories: time,
  128. series: series
  129. };
  130. this.chartData = JSON.parse(JSON.stringify(res));
  131. },
  132. getEmit() {
  133. const {
  134. bucketType,
  135. bucketValue
  136. } = this.echartsData
  137. let time = null
  138. switch (bucketType) {
  139. case 0:
  140. time = 86400000 * bucketValue
  141. this.timeNum = 4
  142. break;
  143. case 1:
  144. time = 3600000 * bucketValue
  145. this.timeNum = 13
  146. break;
  147. case 2:
  148. time = 60000 * bucketValue
  149. this.timeNum = 16
  150. break;
  151. case 3:
  152. time = 1000 * bucketValue
  153. this.timeNum = 19
  154. break;
  155. }
  156. this.getServerData()
  157. this.barTime = setInterval(() => {
  158. let chartParams = {
  159. id: this.echartsData.id,
  160. startTime: timeToDate(new Date().getTime() - 3600000),
  161. endTime: nowDate()
  162. }
  163. uni.$http.get('/chart/getChartById', chartParams).then(res => {
  164. const data = res.data
  165. if (data.code === 200) {
  166. let chartItemList = data.chartItemList;
  167. chartItemList.forEach((item, index) => {
  168. if (item.itemId === this.echartsData[
  169. index].itemId) {
  170. if (item.valueList.length !== 0) {
  171. //执行将新数据添加进去,旧数据去除一个
  172. this.echartsData[index].valueList =
  173. this.echartsData[index].valueList
  174. .concat(item.valueList)
  175. this.echartsData[
  176. index]
  177. .valueList.splice(0, item
  178. .valueList.length)
  179. this.echartsData[index]
  180. .valueTimeList = this.echartsData[
  181. index]
  182. .valueTimeList.concat(item
  183. .valueTimeList)
  184. this.echartsData[
  185. index]
  186. .valueTimeList.splice(0,
  187. item
  188. .valueTimeList.length)
  189. }
  190. }
  191. })
  192. this.getServerData()
  193. }
  194. })
  195. }, time)
  196. }
  197. }
  198. };
  199. </script>
  200. <style scoped>
  201. /* 请根据实际需求修改父元素尺寸,组件自动识别宽高 */
  202. .charts-box {
  203. width: 100%;
  204. height: 540rpx;
  205. }
  206. </style>