temporature.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. <template>
  2. <div
  3. id="tempChart"
  4. style="width: 100%;height: 150px;"
  5. ></div>
  6. </template>
  7. <script>
  8. import { mapState } from "vuex";
  9. var TP_value = 20.3;
  10. var kd = [];
  11. var Gradient = [];
  12. var leftColor = "";
  13. var showValue = 60;
  14. var boxPosition = [65, 0];
  15. var TP_txt = "";
  16. // 刻度使用柱状图模拟,短设置1,长的设置3;构造一个数据
  17. for (let i = 0, len = 80; i <= len; i += 1) {
  18. if (i < 10) {
  19. kd.push("");
  20. } else if ((i - 10) % 10 === 0) {
  21. kd.push("-3");
  22. } else {
  23. kd.push("-1");
  24. }
  25. }
  26. //中间线的渐变色和文本内容
  27. if (TP_value > 24) {
  28. TP_txt = "温度偏高";
  29. Gradient.push(
  30. {
  31. offset: 0,
  32. color: "#93FE94",
  33. },
  34. {
  35. offset: 0.5,
  36. color: "#E4D225",
  37. },
  38. {
  39. offset: 1,
  40. color: "#E01F28",
  41. }
  42. );
  43. } else if (TP_value >= 14) {
  44. TP_txt = "温度正常";
  45. Gradient.push(
  46. {
  47. offset: 0,
  48. color: "#93FE94",
  49. },
  50. {
  51. offset: 1,
  52. color: "#E4D225",
  53. }
  54. );
  55. } else {
  56. TP_txt = "温度偏低";
  57. Gradient.push({
  58. offset: 1,
  59. color: "#93FE94",
  60. });
  61. }
  62. if (TP_value > 80) {
  63. showValue = 80;
  64. } else {
  65. if (TP_value < 0) {
  66. showValue = 0;
  67. } else {
  68. showValue = TP_value + 10;
  69. }
  70. }
  71. if (TP_value < 0) {
  72. boxPosition = [65, -120];
  73. }
  74. leftColor = Gradient[Gradient.length - 1].color;
  75. // 因为柱状初始化为0,温度存在负值,所以加上负值60和空出距离10
  76. export default {
  77. data() {
  78. return {
  79. showValue: "",
  80. chartInstance: null,
  81. allData: null, // 服务器返回的数据
  82. currentPage: 1, //当前显示的页数, 当修改页数时, 数据动态刷新
  83. totalPage: 0, //一共多少也
  84. timerId: null, //定时器
  85. };
  86. },
  87. mounted() {
  88. this.websocket = new WebSocket(`ws://${this.websocketIP}/hbase/ws/belt/46`);
  89. this.initWebSocket();
  90. this.initChart();
  91. this.updateChart();
  92. // this.getData()
  93. // 对窗口大小变化的事件进行监听
  94. // window.addEventListener('resize', this.screenAdapter)
  95. },
  96. computed: {
  97. ...mapState(["websocketIP"]),
  98. },
  99. methods: {
  100. //初始化echarts实例对象
  101. initChart() {
  102. this.chartInstance = this.$echarts.init(
  103. document.getElementById("tempChart")
  104. );
  105. //对图表对象进行鼠标事件监听
  106. this.chartInstance.on("mousecver", () => {
  107. this.clearInterval(this.timerId);
  108. });
  109. this.chartInstance.on("mouseout", () => {
  110. this.startInterval();
  111. });
  112. },
  113. updateChart() {
  114. // 每一页显示1个数据, 获取1个数据
  115. // const start = (this.currentPage - 1) * 1
  116. // const end = this.currentPage * 1
  117. // const showData = this.allData.slice(start, end)
  118. // const showValue = showData[0].wendu-10
  119. // const arrayData = this.showData.map((item) =>{
  120. // return item.value
  121. // })
  122. // showValue = arrayData
  123. const option = {
  124. grid: {
  125. x: 60, //左侧与y轴的距离
  126. y: -50, //top部与x轴的距离
  127. x2: -120, //右侧与y轴的距离
  128. y2: 20, //bottom部与x轴的距离
  129. },
  130. // backgroundColor: '#0C2F6F',
  131. title: {
  132. text: "油缸温度",
  133. show: true,
  134. textStyle: {
  135. color: "pink",
  136. },
  137. },
  138. yAxis: [
  139. {
  140. show: false,
  141. data: [],
  142. min: 0,
  143. max: 120,
  144. axisLine: {
  145. show: false,
  146. },
  147. },
  148. {
  149. show: false,
  150. min: 0,
  151. max: 90,
  152. },
  153. ],
  154. xAxis: [
  155. {
  156. show: false,
  157. min: -10,
  158. max: 80,
  159. data: [],
  160. },
  161. {
  162. show: false,
  163. min: -10,
  164. max: 80,
  165. data: [],
  166. },
  167. {
  168. show: false,
  169. min: -10,
  170. max: 80,
  171. data: [],
  172. },
  173. {
  174. show: false,
  175. min: -5,
  176. max: 80,
  177. },
  178. ],
  179. series: [
  180. {
  181. name: "条",
  182. type: "bar",
  183. // 对应上面XAxis的第一个对象配置
  184. xAxisIndex: 0,
  185. // data: arrayData[1],
  186. data: [
  187. {
  188. value: this.showValue,
  189. },
  190. ],
  191. barWidth: 8,
  192. itemStyle: {
  193. normal: {
  194. color: new echarts.graphic.LinearGradient(0, 1, 0, 0, Gradient),
  195. },
  196. },
  197. z: 2,
  198. },
  199. {
  200. name: "白框",
  201. type: "bar",
  202. xAxisIndex: 1,
  203. barGap: "-100%",
  204. data: [80],
  205. barWidth: 14,
  206. itemStyle: {
  207. normal: {
  208. color: "#0C2E6D",
  209. barBorderRadius: 50,
  210. },
  211. },
  212. z: 1,
  213. },
  214. {
  215. name: "外框",
  216. type: "bar",
  217. xAxisIndex: 2,
  218. barGap: "-100%",
  219. data: [81],
  220. barWidth: 24,
  221. itemStyle: {
  222. normal: {
  223. color: "#4577BA",
  224. barBorderRadius: 50,
  225. },
  226. },
  227. z: 0,
  228. },
  229. {
  230. name: "圆",
  231. type: "scatter",
  232. hoverAnimation: false,
  233. data: [0],
  234. xAxisIndex: 0,
  235. symbolSize: 20,
  236. itemStyle: {
  237. normal: {
  238. color: "#93FE94",
  239. opacity: 1,
  240. },
  241. },
  242. series: {
  243. data: [
  244. {
  245. value: this.showValue,
  246. },
  247. ],
  248. },
  249. z: 2,
  250. },
  251. {
  252. name: "白圆",
  253. type: "scatter",
  254. hoverAnimation: false,
  255. data: [0],
  256. xAxisIndex: 1,
  257. symbolSize: 24,
  258. itemStyle: {
  259. normal: {
  260. color: "#0C2E6D",
  261. opacity: 1,
  262. },
  263. },
  264. z: 1,
  265. },
  266. {
  267. name: "外圆",
  268. type: "scatter",
  269. hoverAnimation: false,
  270. data: [0],
  271. xAxisIndex: 2,
  272. symbolSize: 30,
  273. itemStyle: {
  274. normal: {
  275. color: "#4577BA",
  276. opacity: 1,
  277. },
  278. },
  279. z: 0,
  280. },
  281. {
  282. name: "刻度",
  283. type: "bar",
  284. yAxisIndex: 0,
  285. xAxisIndex: 3,
  286. label: {
  287. normal: {
  288. show: true,
  289. position: "left",
  290. distance: 10,
  291. color: "white",
  292. fontSize: 14,
  293. formatter: function (params) {
  294. if (params.dataIndex > 80 || params.dataIndex < 10) {
  295. return "";
  296. } else {
  297. if ((params.dataIndex - 10) % 10 === 0) {
  298. return params.dataIndex - 10;
  299. } else {
  300. return "";
  301. }
  302. }
  303. },
  304. },
  305. },
  306. barGap: "-100%",
  307. data: kd,
  308. barWidth: 1,
  309. itemStyle: {
  310. normal: {
  311. color: "white",
  312. barBorderRadius: 120,
  313. },
  314. },
  315. z: 0,
  316. },
  317. ],
  318. };
  319. this.chartInstance.setOption(option);
  320. },
  321. addData(data) {
  322. // console.log(data);
  323. this.showValue = data.map((item) => item["youwen_after"])[0];
  324. console.log(this.showValue);
  325. this.chartInstance.setOption({
  326. series: [
  327. {
  328. name: "条",
  329. type: "bar",
  330. // 对应上面XAxis的第一个对象配置
  331. xAxisIndex: 0,
  332. data: [
  333. {
  334. value: Number(this.showValue),
  335. },
  336. ],
  337. },
  338. {
  339. name: "白框",
  340. type: "bar",
  341. xAxisIndex: 1,
  342. barGap: "-100%",
  343. data: [100],
  344. barWidth: 14,
  345. itemStyle: {
  346. normal: {
  347. color: "#0C2E6D",
  348. barBorderRadius: 50,
  349. },
  350. },
  351. z: 1,
  352. },
  353. {
  354. name: "外框",
  355. type: "bar",
  356. xAxisIndex: 2,
  357. barGap: "-100%",
  358. data: [101],
  359. },
  360. {
  361. name: "圆",
  362. type: "scatter",
  363. hoverAnimation: false,
  364. data: [0],
  365. xAxisIndex: 0,
  366. symbolSize: 20,
  367. series: {
  368. data: [
  369. {
  370. value: showValue,
  371. },
  372. ],
  373. },
  374. },
  375. {
  376. name: "白圆",
  377. type: "scatter",
  378. hoverAnimation: false,
  379. data: [0],
  380. series: {
  381. data: [
  382. {
  383. value: showValue,
  384. },
  385. ],
  386. },
  387. },
  388. {
  389. name: "外圆",
  390. type: "scatter",
  391. hoverAnimation: false,
  392. data: [0],
  393. xAxisIndex: 2,
  394. symbolSize: 30,
  395. series: {
  396. data: [
  397. {
  398. value: showValue,
  399. },
  400. ],
  401. },
  402. z: 0,
  403. },
  404. {
  405. name: "刻度",
  406. type: "bar",
  407. yAxisIndex: 0,
  408. xAxisIndex: 3,
  409. barGap: "-100%",
  410. },
  411. ],
  412. });
  413. },
  414. initWebSocket() {
  415. // 连接错误
  416. this.websocket.onerror = () => {
  417. console.log(
  418. "WebSocket连接发生错误 状态码:" + this.websocket.readyState
  419. );
  420. };
  421. // 连接成功
  422. this.websocket.onopen = () => {
  423. console.log(
  424. "WebSocket连接成功 状态码:" + this.websocket.readyState
  425. );
  426. };
  427. // 收到消息的回调
  428. this.websocket.onmessage = (event) => {
  429. // console.log("onmessage", event);
  430. if (JSON.parse(event.data).length) {
  431. this.addData(JSON.parse(event.data));
  432. }
  433. };
  434. // 连接关闭的回调
  435. this.websocket.onclose = () => {
  436. console.log(
  437. "WebSocket连接关闭 状态码:" + this.websocket.readyState
  438. );
  439. };
  440. // 通过$once来监听定时器,在beforeDestroy钩子可以被清除。
  441. this.$once("hook:beforeDestroy", () => {
  442. this.websocket.close();
  443. });
  444. },
  445. },
  446. };
  447. </script>
  448. <style scoped>
  449. </style>