RealtimeLine.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <template>
  2. <div class="realtime-line">
  3. <div style="width: 100%;height: 100%;">
  4. <el-carousel indicator-position="outside" :interval="30000" height="95%" arrow="never">
  5. <el-carousel-item v-for="(item, index) in Math.ceil(Object.keys(workshopList).length / (x * y))" :key="index">
  6. <div style="display: flex;flex-wrap: wrap;height: 100%">
  7. <div
  8. v-loading="chartLoading"
  9. class="chart-box"
  10. :class="indexkey % x === 0 ? 'row-first-chart' : ''"
  11. v-for="(key, indexkey) in Object.keys(workshopList).slice(index * x * y , (index + 1) * x * y)"
  12. :key="key"
  13. :style="chartBoxStyle"
  14. >
  15. <div class="chart-box-title">{{ workshopList[key].workshopName }}</div>
  16. <div class="chart-box-content">
  17. <div :id="key" style="height: 100%;"/>
  18. </div>
  19. </div>
  20. </div>
  21. </el-carousel-item>
  22. </el-carousel>
  23. </div>
  24. <!--<div
  25. v-loading="chartLoading"
  26. class="chart-box"
  27. :class="index % x === 0 ? 'row-first-chart' : ''"
  28. v-for="(key, index) in Object.keys(workshopList)"
  29. :key="key"
  30. :style="chartBoxStyle"
  31. >
  32. <div class="chart-box-title">{{ workshopList[key].workshopName }}</div>
  33. <div class="chart-box-content">
  34. <div :id="key" style="height: 100%"/>
  35. </div>
  36. </div>-->
  37. </div>
  38. </template>
  39. <script>
  40. import { historyChartAllData } from '@/api/HistoryData'
  41. import { findByAcquisitionStationId } from '@/api/Workshop'
  42. import { findYMax } from '@/api/DashboardLayout'
  43. import moment from 'moment'
  44. import { mapState } from 'vuex'
  45. export default {
  46. name: 'RealtimeLine',
  47. data() {
  48. return {
  49. chartLoading: true,
  50. workshopList: {},
  51. timeList: [],
  52. chartData: {},
  53. chartList: {},
  54. initCompleted: false,
  55. canUpdate: true,
  56. yMaxMap: {},
  57. }
  58. },
  59. computed: {
  60. ...mapState({
  61. realtimeDataMap: (state, getter) => state.queue.realtimeDataMap,
  62. lastTime: (state, getter) => state.queue.lastTime,
  63. acquisitionStation: state => state.company.acquisitionStation,
  64. x: state => state.company.acquisitionStation.xQuantity,
  65. y: state => state.company.acquisitionStation.yQuantity,
  66. }),
  67. chartBoxStyle() {
  68. const baseX = this.x === 1 ? 99 : 90
  69. const baseY = this.y === 1 ? 99 : 90
  70. const baseMargin = this.x === 1 ? 10000 : 3
  71. return {
  72. width: baseX / this.x + '%',
  73. height: baseY / this.y + '%',
  74. marginLeft: baseMargin / (this.x - 1) + '%',
  75. }
  76. },
  77. },
  78. watch: {
  79. lastTime() {
  80. if (this.initCompleted && this.canUpdate) {
  81. this.updateChart()
  82. }
  83. },
  84. async workshopList(newVal) {
  85. if (newVal) {
  86. this.$nextTick(() => {
  87. this.chartLoading = false
  88. for (const key in this.chartList) {
  89. window.removeEventListener('resize', this.chartList[key].resize)
  90. this.chartList[key].dispose()
  91. this.chartList[key] = null
  92. }
  93. this.chartList = {}
  94. for (const key in this.workshopList) {
  95. this.chartList[key] = this.$echarts.init(document.getElementById(key))
  96. const tagList = this.workshopList[key].tagList
  97. const legendData = tagList.map(e => this.getLegendName(this.workshopList[key].workshopName, e.name))
  98. let startType = 'UNKNOWN'
  99. tagList.forEach(e => {
  100. if (e.tableColumn === this.workshopList[key].collectEntityList[0].startType) {
  101. startType = e.desc
  102. }
  103. })
  104. const collectEntityId = this.workshopList[key].collectEntityList[0].id
  105. const legendSelected = {}
  106. legendData.forEach(e => {
  107. legendSelected[e] = e === startType
  108. })
  109. const options = {
  110. legend: {
  111. right: '4%',
  112. left: '20%',
  113. icon: 'roundRect',
  114. textStyle: {
  115. fontSize: 12,
  116. color: '#FFFFFF',
  117. },
  118. data: legendData,
  119. selected: legendSelected,
  120. },
  121. tooltip: {
  122. trigger: 'axis',
  123. axisPointer: {
  124. type: 'cross',
  125. label: {
  126. backgroundColor: '#6a7985'
  127. }
  128. }
  129. },
  130. grid: {
  131. top: '15%',
  132. left: '5%',
  133. right: '10%',
  134. bottom: '3%',
  135. containLabel: true
  136. },
  137. xAxis: {
  138. type: 'category',
  139. boundaryGap: false,
  140. data: [],
  141. axisTick: {
  142. show: false
  143. },
  144. axisLine: {
  145. show: true,
  146. lineStyle: {
  147. width: 2,
  148. color: '#FFF',
  149. }
  150. },
  151. axisLabel: { //y轴文字的配置
  152. color: '#FFF', //Y轴内容文字颜色
  153. },
  154. },
  155. yAxis: {
  156. type: 'value',
  157. axisLine: {
  158. show: true,
  159. lineStyle: {
  160. width: 2,
  161. color: {
  162. x: 0,
  163. y: 0,
  164. colorStops: [{
  165. offset: 0,
  166. color: '#AAA7A7' // 100% 处的颜色
  167. }, {
  168. offset: 1,
  169. color: '#FFFFFF' // 0% 处的颜色
  170. }]
  171. },
  172. }
  173. },
  174. splitLine: {
  175. show: false,
  176. lineStyle: {
  177. width: 2,
  178. color: {
  179. x: 1,
  180. y: 1,
  181. colorStops: [{
  182. offset: 1,
  183. color: '#AAA7A7' // 100% 处的颜色
  184. }, {
  185. offset: 0,
  186. color: '#FFFFFF' // 0% 处的颜色
  187. }]
  188. },
  189. },
  190. },
  191. axisLabel: { //y轴文字的配置
  192. color: '#FFF', //Y轴内容文字颜色
  193. },
  194. max: this.yMaxMap[collectEntityId]
  195. },
  196. series: []
  197. }
  198. for (const e of tagList) {
  199. const name = this.getLegendName(this.workshopList[key].workshopName, e.name)
  200. options.series.push(
  201. {
  202. name,
  203. color: this.getLineColor(name),
  204. type: 'line',
  205. smooth: true,
  206. symbol: 'none',
  207. connectNulls: true,
  208. data: [],
  209. markLine: {
  210. symbol: ['none', 'none'],
  211. data: [
  212. {
  213. name: '告警线',
  214. yAxis: e.warnMaxValue
  215. },
  216. ],
  217. lineStyle: {
  218. type: 'solid',
  219. color: '#ef0505',
  220. },
  221. animation: false,
  222. }
  223. }
  224. )
  225. }
  226. this.chartList[key].setOption(options)
  227. window.addEventListener('resize', this.chartList[key].resize)
  228. }
  229. this.fetchData()
  230. })
  231. }
  232. },
  233. },
  234. /*mounted () {
  235. console.log('======================' + Object.keys(this.workshopList).length)
  236. },*/
  237. methods: {
  238. async initData() {
  239. this.initCompleted = false
  240. await findYMax().then(res => {
  241. if (res && res.code === '200') {
  242. const yMaxMap = {}
  243. res.data.forEach(e => {
  244. yMaxMap[e.collectEntityId] = e.yMax
  245. })
  246. this.yMaxMap = yMaxMap
  247. }
  248. })
  249. await findByAcquisitionStationId({ acquisitionStationId: this.acquisitionStation.id }).then(res => {
  250. if (res && res.code === '200') {
  251. const obj = {}
  252. const arrNotTags = []
  253. res.data.forEach((val, index) => {
  254. if (val.tagList && val.tagList.length > 0) {
  255. // 过滤运行反馈
  256. val.tagList = val.tagList.filter(e => !e.code.endsWith('FEEDBACK'))
  257. obj[val.workshopCode] = val
  258. } else {
  259. arrNotTags.push(val)
  260. }
  261. })
  262. this.workshopList = obj
  263. for (const item of arrNotTags) {
  264. this.$message.warning(item.workshopName + '未配置标签或者标签未启用!!!')
  265. }
  266. }
  267. })
  268. },
  269. async fetchData() {
  270. const params = {
  271. start: moment().subtract(5, 'minutes').valueOf(),
  272. end: moment().valueOf(),
  273. step: 2,
  274. acquisitionStationId: this.acquisitionStation.id
  275. }
  276. await historyChartAllData(params).then(res => {
  277. if (res && res.code === '200') {
  278. this.timeList = res.data.timeList
  279. for (const key in res.data.dataMap) {
  280. this.chartData[key] = res.data.dataMap[key]
  281. }
  282. for (const k in this.workshopList) {
  283. const options = {
  284. xAxis: {
  285. data: this.timeList
  286. },
  287. series: []
  288. }
  289. for (const a of this.workshopList[k].tagList) {
  290. options.series.push({ data: this.chartData[a.code] })
  291. }
  292. this.chartList[k].setOption(options)
  293. }
  294. this.initCompleted = true
  295. }
  296. })
  297. },
  298. getLineColor(name) {
  299. const colors = ['#17f107', '#ec0ad5', '#00DDFF', '#f1ca05']
  300. switch (name) {
  301. case '电流':
  302. return colors[0]
  303. case '振动':
  304. return colors[1]
  305. case '噪声':
  306. return colors[2]
  307. case '反馈':
  308. return colors[3]
  309. default:
  310. return colors[4]
  311. }
  312. },
  313. getLegendName(prefix, name) {
  314. if (name.lastIndexOf(prefix) !== -1) {
  315. return name.split(prefix)[1]
  316. } else {
  317. return name
  318. }
  319. },
  320. updateChart() {
  321. this.canUpdate = false
  322. this.timeList.push(this.lastTime)
  323. this.timeList.shift()
  324. for (const key in this.chartList) {
  325. const opt = {
  326. xAxis: {
  327. data: this.timeList
  328. },
  329. series: []
  330. }
  331. for (const e of this.workshopList[key].tagList) {
  332. this.chartData[e.code].shift()
  333. this.chartData[e.code].push(this.realtimeDataMap[e.code])
  334. const data = this.chartData[e.code]
  335. opt.series.push({ data })
  336. }
  337. this.chartList[key].setOption(opt)
  338. }
  339. try {
  340. } catch (e) {
  341. console.error(e)
  342. } finally {
  343. this.canUpdate = true
  344. }
  345. },
  346. },
  347. }
  348. </script>
  349. <style lang="less" scoped>
  350. .realtime-line {
  351. height: 100%;
  352. width: 100%;
  353. display: flex;
  354. flex-wrap: wrap;
  355. /*justify-content: flex-start;*/
  356. align-items: center;
  357. .chart-row {
  358. display: flex; /* 使用 flex 布局 */
  359. height: 100%;
  360. }
  361. .chart-box {
  362. display: flex;
  363. flex-direction: column;
  364. .chart-box-title {
  365. background-color: #0c3048;
  366. padding: 5px 16px;
  367. }
  368. .chart-box-content {
  369. flex: 1;
  370. padding-top: 8px;
  371. }
  372. }
  373. .row-first-chart {
  374. /*margin-left: 0 !important;*/
  375. }
  376. }
  377. .el-carousel {
  378. height: 100%;
  379. }
  380. </style>