cyChart.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. <template>
  2. <view class="charts">
  3. <view v-if="chartType!='table'">
  4. <div class="moreTime">
  5. <div v-for="(item,index) in range" :key="index" @click="change(index)"
  6. :class="choiceTime===index?'bod':''">
  7. {{item}}
  8. </div>
  9. </div>
  10. <div class="childTime">
  11. <div v-for="(item,index) in childTime" v-show="choiceTime===index">
  12. <template>
  13. <template v-for="(o,i) in item">
  14. <div @click="getTime(i,o.value)" :class="choiceChildTime===i?'box':''">{{o.text}}</div>
  15. </template>
  16. </template>
  17. </div>
  18. </div>
  19. <view style="height:300px">
  20. <l-echart ref="chartRef" @finished="getServerData(true)"></l-echart>
  21. </view>
  22. </view>
  23. <scroll-view v-if="chartType=='table'" style="width: 100%;height: 350px;" scroll-y="true">
  24. <view>
  25. <text class="tabletitle">{{tableTitle}}</text>
  26. </view>
  27. <uni-table ref="table" border stripe emptyText="暂无更多数据">
  28. <uni-tr>
  29. <uni-th width="100" align="left">点位</uni-th>
  30. <uni-th width="100" align="left">最新值</uni-th>
  31. </uni-tr>
  32. <uni-tr v-for="(item, index) in tableData" :key="index">
  33. <uni-td>{{ item.itemName }}</uni-td>
  34. <uni-td>
  35. <view class="name">{{ item.itemValue }}</view>
  36. </uni-td>
  37. </uni-tr>
  38. </uni-table>
  39. </scroll-view>
  40. </view>
  41. </template>
  42. <script>
  43. import {
  44. nowDate,
  45. timeToDate
  46. } from '../../utils/date.js'
  47. // nvue 不需要引入
  48. // #ifdef VUE2
  49. import * as echarts from '@/uni_modules/lime-echart/static/echarts.min';
  50. // #endif
  51. // #ifdef VUE3
  52. // #ifdef MP
  53. // 由于vue3 使用vite 不支持umd格式的包,小程序依然可以使用,但需要使用require
  54. const echarts = require('../../static/echarts.min');
  55. // #endif
  56. // #ifndef MP
  57. // 由于 vue3 使用vite 不支持umd格式的包,故引入npm的包
  58. import * as echarts from 'echarts/dist/echarts.esm';
  59. // #endif
  60. // #endif
  61. export default {
  62. components: {
  63. },
  64. props: {
  65. //图表类型 bar,line,pie,table
  66. chartType: {
  67. type: String,
  68. default: 'bar'
  69. },
  70. //图表高度
  71. height: {
  72. type: String,
  73. default: '300px'
  74. },
  75. //后台定义的图表id
  76. chartId: {
  77. type: Number,
  78. default: ''
  79. }
  80. },
  81. data() {
  82. return {
  83. seriesData: [],
  84. timeData:[],
  85. option: {},
  86. tableData: [],
  87. tableTitle:'数据表格',
  88. range: ["分", "时", "日", "周"],
  89. childTime: [
  90. [{
  91. value: 900000,
  92. text: "15分"
  93. },
  94. {
  95. value: 1800000,
  96. text: "30分"
  97. },
  98. {
  99. value: 2700000,
  100. text: "45分"
  101. }
  102. ],
  103. [{
  104. value: 3600000,
  105. text: "1小时"
  106. },
  107. {
  108. value: 21600000,
  109. text: "6小时"
  110. },
  111. {
  112. value: 43200000,
  113. text: "12小时"
  114. },
  115. {
  116. value: 64800000,
  117. text: "18小时"
  118. }
  119. ],
  120. [{
  121. value: 86400000,
  122. text: "1日"
  123. },
  124. {
  125. value: 259200000,
  126. text: "3日"
  127. },
  128. {
  129. value: 432000000,
  130. text: "5日"
  131. }
  132. ],
  133. [{
  134. value: 604800000,
  135. text: "1周"
  136. },
  137. {
  138. value: 1209600000,
  139. text: "2周"
  140. },
  141. {
  142. value: 1814400000,
  143. text: "3周"
  144. },
  145. {
  146. value: 2419200000,
  147. text: "4周"
  148. }
  149. ],
  150. ],
  151. startTime: null,
  152. endTime:null,
  153. choiceTime: 0,
  154. choiceChildTime: 0,
  155. chartTime:null,
  156. chartInterval:3000,
  157. };
  158. },
  159. created() {
  160. this.startTime = timeToDate(new Date().getTime() - 900000)
  161. this.endTime = timeToDate(new Date().getTime())
  162. },
  163. mounted() {
  164. if(this.chartType=='table'){
  165. this.getServerData(true)
  166. }
  167. },
  168. methods: {
  169. async init() {
  170. // chart 图表实例不能存在data里
  171. const chart = await this.$refs.chartRef.init(echarts)
  172. chart.setOption(this.option)
  173. if(this.chartTime==null){
  174. this.chartTime = setInterval(()=>{
  175. this.getServerData(false)
  176. },this.chartInterval)
  177. }
  178. },
  179. //分时日周选择
  180. change(i) {
  181. this.choiceTime === i ? this.choiceTime = -1 : this.choiceTime = i
  182. this.choiceChildTime = -1
  183. },
  184. //二级时间选择
  185. getTime(i, val) {
  186. this.choiceChildTime = i
  187. this.startTime = timeToDate(new Date().getTime() - val)
  188. this.getServerData(true);
  189. },
  190. //获得图表数据
  191. getServerData(first) {
  192. let id = this.chartId
  193. let startTime = null
  194. let endTime = null
  195. if(first){
  196. startTime = this.startTime
  197. endTime = this.endTime
  198. }else{
  199. startTime = this.endTime
  200. endTime = timeToDate(new Date().getTime())
  201. this.endTime = endTime
  202. }
  203. //获取数据
  204. uni.$http.get(`/chart/getChartById?id=${id}&startTime=${startTime}&endTime=${endTime}`).then(res => {
  205. const data = res.data.data;
  206. const chartData = data.chartItemList
  207. //x轴数据,时间轴
  208. const timeData = chartData[0].valueTimeList
  209. const bucketType = data.bucketType
  210. const bucketValue = data.bucketValue
  211. this.chartInterval = this.getChartInterval(bucketType,bucketValue)
  212. let optionData = this.getChartOption(this.chartType)
  213. //y轴数据,数据轴
  214. if (this.chartType == 'bar' || this.chartType == 'line') {
  215. optionData.title.text = data.chartName
  216. const series = chartData.map(item => {
  217. return {
  218. name: item.describe ? item.describe : item.itemReadName,
  219. data: item.valueList,
  220. type: this.chartType
  221. }
  222. })
  223. if(first){
  224. this.seriesData = series
  225. this.timeData = timeData
  226. }else{
  227. //追加x轴数据
  228. this.timeData = this.timeData.concat(timeData);
  229. this.timeData.splice(0,timeData.length)
  230. //追加y轴数据
  231. series.forEach((item,index)=>{
  232. this.seriesData.forEach((sitem,sindex)=>{
  233. if(item.name==sitem.name){
  234. sitem.data = sitem.data.concat(item.data);
  235. sitem.data.splice(0,item.data.length)
  236. }
  237. })
  238. });
  239. }
  240. optionData.xAxis.data = this.timeData
  241. optionData.series = this.seriesData
  242. } else if (this.chartType == 'pie') {
  243. optionData.title.text = data.chartName
  244. const {
  245. valueList,
  246. valueTimeList
  247. } = chartData[0]
  248. let sdata = valueTimeList.map((item, i) => {
  249. return {
  250. name: item,
  251. value: valueList[i]
  252. }
  253. });
  254. if(first){
  255. this.seriesData = sdata
  256. }else{
  257. this.seriesData = this.seriesData.concat(sdata)
  258. this.seriesData.splice(0,sdata.length)
  259. }
  260. optionData.series = [{
  261. type: 'pie',
  262. radius: '55%',
  263. label: {
  264. show: false
  265. },
  266. data: this.seriesData,
  267. center: ['70%', '50%']
  268. }]
  269. } else if (this.chartType == 'table') {
  270. let dataList = [];
  271. for (let dataItem of chartData) {
  272. let data = {
  273. itemName: dataItem.describe != null ? dataItem.describe : dataItem
  274. .itemReadName,
  275. itemValue: dataItem.valueList[0],
  276. itemAlert: dataItem.valueTimeList[0],
  277. };
  278. dataList.push(data)
  279. }
  280. this.tableData = dataList
  281. this.tableTitle = data.chartName
  282. //定时刷新
  283. this.chartInterval = 3000
  284. if(this.chartTime==null){
  285. this.chartTime = setInterval(()=>{
  286. this.getServerData(false)
  287. },this.chartInterval)
  288. }
  289. }
  290. if(this.chartType!='table'){
  291. this.option = optionData
  292. if(first){
  293. this.init()
  294. }else{
  295. this.$refs.chartRef.setOption(this.option)
  296. }
  297. }
  298. });
  299. },
  300. getChartInterval(bucketType,bucketValue){
  301. let time = null
  302. switch (bucketType) {
  303. case 1:
  304. time = 86400000 * bucketValue
  305. break;
  306. case 2:
  307. time = 3600000 * bucketValue
  308. break;
  309. case 3:
  310. time = 60000 * bucketValue
  311. break;
  312. case 4:
  313. time = 1000 * bucketValue
  314. break;
  315. }
  316. return time
  317. },
  318. //获得图表option数据
  319. getChartOption(cType) {
  320. let option = {}
  321. if (cType == 'bar') {
  322. option = {
  323. title: {
  324. text: '',
  325. textStyle: {
  326. color: '#666666',
  327. fontWeight: 'normal',
  328. fontSize: '14'
  329. }
  330. },
  331. xAxis: {
  332. type: 'category',
  333. data: [],
  334. axisLabel: {
  335. formatter: function(value) {
  336. return value.substring(11, 19) + "\n" + value.substring(0, 10).split('-').join(
  337. '')
  338. }
  339. }
  340. },
  341. yAxis: {
  342. type: 'value'
  343. },
  344. legend: {
  345. type: 'scroll',
  346. top: 30
  347. },
  348. tooltip: {
  349. trigger: 'axis',
  350. axisPointer: {
  351. type: 'cross',
  352. lineStyle: {
  353. type: 'dashed'
  354. },
  355. label: {
  356. show: false
  357. }
  358. },
  359. textStyle: {
  360. color: '#ffffff'
  361. },
  362. confine: true,
  363. extraCssText: 'white-space: normal; word-break: break-all;',
  364. backgroundColor: 'rgba(108,106,120,0.9)',
  365. position: 'inside',
  366. // formatter: function(params) {
  367. // let result = ''
  368. // for (let i in params) {
  369. // let value = '--'
  370. // if (params[i].data !== null) {
  371. // value = params[i].data
  372. // }
  373. // result += params[i].seriesName + ':' + value
  374. // }
  375. // return result
  376. // }
  377. },
  378. dataZoom: [{
  379. type: 'slider',
  380. show: true,
  381. start: 90,
  382. end: 100
  383. },
  384. ],
  385. series: []
  386. };
  387. } else if (cType == 'line') {
  388. option = {
  389. title: {
  390. text: '',
  391. textStyle: {
  392. color: '#666666',
  393. fontWeight: 'normal',
  394. fontSize: '14'
  395. }
  396. },
  397. xAxis: {
  398. type: 'category',
  399. data: [],
  400. axisLabel: {
  401. formatter: function(value) {
  402. return value.substring(11, 19) + "\n" + value.substring(0, 10).split('-').join(
  403. '')
  404. }
  405. }
  406. },
  407. yAxis: {
  408. type: 'value'
  409. },
  410. legend: {
  411. type: 'scroll',
  412. top: 30
  413. },
  414. tooltip: {
  415. trigger: 'axis',
  416. axisPointer: {
  417. type: 'cross',
  418. lineStyle: {
  419. type: 'dashed'
  420. },
  421. label: {
  422. show: false
  423. },
  424. },
  425. textStyle: {
  426. color: '#ffffff'
  427. },
  428. confine: true,
  429. extraCssText: 'white-space: normal; word-break: break-all;',
  430. backgroundColor: 'rgba(108,106,120,0.9)', //通过设置rgba调节背景颜色与透明度
  431. position: 'inside',
  432. // formatter: function(params) {
  433. // let result = ''
  434. // for (let i in params) {
  435. // let value = '--'
  436. // if (params[i].data !== null) {
  437. // value = params[i].data
  438. // }
  439. // result += params[i].seriesName + ':' + value + '<br/>'
  440. // }
  441. // return result
  442. // }
  443. },
  444. dataZoom: [
  445. {
  446. type: 'slider',
  447. show: true,
  448. start: 90,
  449. end: 100,
  450. bottom: 10
  451. }
  452. ],
  453. series: []
  454. };
  455. } else if (cType == 'pie') {
  456. option = {
  457. title: {
  458. text: '',
  459. textStyle: {
  460. color: '#666666',
  461. fontWeight: 'normal',
  462. fontSize: '14'
  463. }
  464. },
  465. legend: {
  466. type: 'scroll',
  467. orient: 'vertical',
  468. left: 'left',
  469. top: 20
  470. },
  471. tooltip: {
  472. trigger: 'item',
  473. confine: true,
  474. extraCssText: 'white-space: normal; word-break: break-all;',
  475. backgroundColor: 'rgba(108,106,120,0.9)',
  476. textStyle: {
  477. color: '#ffffff'
  478. },
  479. },
  480. series: []
  481. };
  482. }
  483. return option
  484. }
  485. }
  486. };
  487. </script>
  488. <style scoped>
  489. .charts {
  490. height: 400px;
  491. }
  492. .moreTime {
  493. display: flex;
  494. justify-content: space-between;
  495. align-items: center;
  496. width: 80vw;
  497. margin: auto
  498. }
  499. .bod {
  500. border-bottom: 3px solid #007AFF;
  501. }
  502. .box {
  503. border: 1px solid #007AFF !important;
  504. }
  505. .childTime {
  506. margin: 16rpx 60rpx 0 60rpx;
  507. }
  508. .childTime>div {
  509. display: flex;
  510. justify-content: space-between;
  511. align-items: center;
  512. }
  513. .childTime>div>div {
  514. width: 140rpx;
  515. padding: 10rpx;
  516. border: 1px solid #e6e6e6;
  517. border-radius: 16rpx;
  518. color: #7e7e7e;
  519. font-size: 26rpx;
  520. text-align: center;
  521. }
  522. .tabletitle{
  523. font-size: 28rpx;
  524. padding: 8px 10px;
  525. font-weight: bold;
  526. color: #606266;
  527. }
  528. </style>