demo.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <template>
  2. <view class="charts-box">
  3. <qiun-data-charts ref="lineChart" type="line" :opts="opts" :chartData="chartData" :ontouch="true" canvas2d
  4. canvasId="canvasId" :disableScroll="true" :onzoom="true" />
  5. </view>
  6. </template>
  7. <script>
  8. import {
  9. nowDate,
  10. timeToDate
  11. } from '../../utils/date.js';
  12. import uCharts from '@/uni_modules/qiun-data-charts/js_sdk/u-charts/u-charts.js';
  13. export default {
  14. props: {
  15. echartsData: Object
  16. },
  17. data() {
  18. return {
  19. chartData: {},
  20. //您可以通过修改 config-ucharts.js 文件中下标为 ['line'] 的节点来配置全局默认参数,如都是默认参数,此处可以不传 opts 。实际应用过程中 opts 只需传入与全局默认参数中不一致的【某一个属性】即可实现同类型的图表显示不同的样式,达到页面简洁的需求。
  21. opts: {
  22. color: ["#1890FF", "#91CB74", "#FAC858", "#EE6666", "#73C0DE", "#3CA272", "#FC8452", "#9A60B4",
  23. "#ea7ccc"
  24. ],
  25. padding: [15, 10, 0, 15],
  26. enableScroll: true,
  27. legend: {},
  28. xAxis: {
  29. disableGrid: true,
  30. scrollShow: true,
  31. itemCount: 2,
  32. format:"yAxisDemo1"
  33. },
  34. yAxis: {
  35. gridType: "dash",
  36. dashLength: 2
  37. },
  38. extra: {
  39. line: {
  40. type: "straight",
  41. width: 2,
  42. activeType: "hollow"
  43. }
  44. }
  45. },
  46. time: [],
  47. series: []
  48. };
  49. },
  50. mounted() {
  51. this.getEmit()
  52. },
  53. methods: {
  54. getServerData() {
  55. const chartData = this.echartsData.chartItemList
  56. const timeData = chartData[0].valueTimeList
  57. const time = timeData.map(item => {
  58. return item.substring(0, 19).replaceAll('-', "")
  59. })
  60. this.time = time
  61. this.series = series
  62. const series = chartData.map(item => {
  63. return {
  64. name: item.describe ? item.describe : item.itemName,
  65. data: item.valueList
  66. }
  67. })
  68. let res = {
  69. categories: time,
  70. series: series
  71. };
  72. this.chartData = JSON.parse(JSON.stringify(res));
  73. },
  74. getEmit() {
  75. const {
  76. bucketType,
  77. bucketValue
  78. } = this.echartsData
  79. let time = null
  80. switch (bucketType) {
  81. case 0:
  82. time = 86400000 * bucketValue
  83. break;
  84. case 1:
  85. time = 3600000 * bucketValue
  86. break;
  87. case 2:
  88. time = 60000 * bucketValue
  89. break;
  90. case 3:
  91. time = 1000 * bucketValue
  92. break;
  93. }
  94. this.getServerData()
  95. this.lineTime = setInterval(() => {
  96. const currentTime = nowDate();
  97. let lastTime;
  98. uni.getStorage({
  99. key: 'sTime',
  100. success: (o) => {
  101. lastTime = o.data;
  102. }
  103. })
  104. let chartParams = {
  105. id: this.echartsData.id,
  106. startTime: lastTime,
  107. endTime: currentTime
  108. }
  109. uni.setStorage({
  110. key: 'sTime',
  111. data: currentTime
  112. })
  113. uni.$http.get('/chart/getChartById', chartParams).then(res => {
  114. let chartItemList = res.data.data.chartItemList;
  115. chartItemList.forEach((item, index) => {
  116. if (item.itemId === this.echartsData.chartItemList[index].itemId) {
  117. if (item.valueList.length !== 0) {
  118. //执行将新数据添加进去,旧数据去除一个
  119. this.echartsData.chartItemList[index].valueList.push(...item
  120. .valueList)
  121. this.echartsData.chartItemList[index].valueList.splice(0, item
  122. .valueList.length)
  123. // this.echartsData.chartItemList[index].valueList.slice(item
  124. // .valueList.length, this.echartsData.chartItemList[
  125. // index].valueList.length)
  126. this.echartsData.chartItemList[index].valueTimeList.push(...
  127. item
  128. .valueTimeList)
  129. this.echartsData.chartItemList[index].valueTimeList.splice(0,
  130. item
  131. .valueTimeList.length)
  132. // this.echartsData.chartItemList[index].valueTimeList.slice(
  133. // item
  134. // .valueTimeList.length, this.echartsData.chartItemList[
  135. // index].valueTimeList.length)
  136. }
  137. }
  138. })
  139. })
  140. }, time)
  141. }
  142. }
  143. };
  144. </script>
  145. <style scoped>
  146. /* 请根据实际需求修改父元素尺寸,组件自动识别宽高 */
  147. .charts-box {
  148. width: 100%;
  149. height: 540rpx;
  150. }
  151. </style>