multiple-y-line-chart-normal.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. <template>
  2. <div class="chart" :id="id"></div>
  3. </template>
  4. <script>
  5. import util from "@/helper/util.js";
  6. import partten from "@/helper/partten.js";
  7. import * as echarts from "echarts";
  8. export default {
  9. name: "multiple-y-line-chart-normal",
  10. componentName: "multiple-y-line-chart-normal",
  11. props: {
  12. width: {
  13. type: String,
  14. default: "100%",
  15. },
  16. height: {
  17. type: String,
  18. default: "13.889vh",
  19. },
  20. // 数据
  21. list: {
  22. type: Array,
  23. default: () => [
  24. {
  25. title: "机舱震动x方向",
  26. yAxisIndex: 0,
  27. value: [
  28. {
  29. text: "-0.003",
  30. value: -0.003,
  31. },
  32. {
  33. text: "-0.002",
  34. value: -0.002,
  35. },
  36. {
  37. text: "-0.003",
  38. value: -0.006,
  39. },
  40. {
  41. text: "-0.003",
  42. value: -0.003,
  43. },
  44. {
  45. text: "-0.002",
  46. value: -0.002,
  47. },
  48. {
  49. text: "-0.003",
  50. value: -0.006,
  51. },
  52. {
  53. text: "-0.003",
  54. value: -0.003,
  55. },
  56. {
  57. text: "-0.002",
  58. value: -0.002,
  59. },
  60. {
  61. text: "-0.003",
  62. value: -0.006,
  63. },
  64. ],
  65. },
  66. {
  67. title: "机舱震动y方向",
  68. yAxisIndex: 1,
  69. value: [
  70. {
  71. text: "-0.01",
  72. value: -0.01,
  73. },
  74. {
  75. text: "-0.005",
  76. value: -0.005,
  77. },
  78. {
  79. text: "-0.008",
  80. value: -0.008,
  81. },
  82. {
  83. text: "-0.01",
  84. value: -0.01,
  85. },
  86. {
  87. text: "-0.005",
  88. value: -0.005,
  89. },
  90. {
  91. text: "-0.008",
  92. value: -0.008,
  93. },
  94. {
  95. text: "-0.01",
  96. value: -0.01,
  97. },
  98. {
  99. text: "-0.005",
  100. value: -0.005,
  101. },
  102. {
  103. text: "-0.008",
  104. value: -0.008,
  105. },
  106. ],
  107. },
  108. {
  109. title: "机舱震动最大偏移值",
  110. yAxisIndex: 2,
  111. value: [
  112. {
  113. text: "1",
  114. value: 1,
  115. },
  116. {
  117. text: "0.05",
  118. value: 0.05,
  119. },
  120. {
  121. text: "0.5",
  122. value: 0.5,
  123. },
  124. {
  125. text: "1",
  126. value: 1,
  127. },
  128. {
  129. text: "0.05",
  130. value: 0.05,
  131. },
  132. {
  133. text: "0.5",
  134. value: 0.5,
  135. },
  136. {
  137. text: "1",
  138. value: 1,
  139. },
  140. {
  141. text: "0.05",
  142. value: 0.05,
  143. },
  144. {
  145. text: "0.5",
  146. value: 0.5,
  147. },
  148. ],
  149. },
  150. {
  151. title: "风速1",
  152. yAxisIndex: 3,
  153. value: [
  154. {
  155. text: "1",
  156. value: 1,
  157. },
  158. {
  159. text: "5",
  160. value: 5,
  161. },
  162. {
  163. text: "3",
  164. value: 1,
  165. },
  166. {
  167. text: "1",
  168. value: 1,
  169. },
  170. {
  171. text: "5",
  172. value: 5,
  173. },
  174. {
  175. text: "3",
  176. value: 1,
  177. },
  178. {
  179. text: "1",
  180. value: 1,
  181. },
  182. {
  183. text: "5",
  184. value: 5,
  185. },
  186. {
  187. text: "3",
  188. value: 1,
  189. },
  190. ],
  191. },
  192. {
  193. title: "风速2",
  194. yAxisIndex: 4,
  195. value: [
  196. {
  197. text: "1",
  198. value: 1,
  199. },
  200. {
  201. text: "2",
  202. value: 2,
  203. },
  204. {
  205. text: "3",
  206. value: 1,
  207. },
  208. {
  209. text: "1",
  210. value: 1,
  211. },
  212. {
  213. text: "2",
  214. value: 2,
  215. },
  216. {
  217. text: "3",
  218. value: 1,
  219. },
  220. {
  221. text: "1",
  222. value: 1,
  223. },
  224. {
  225. text: "2",
  226. value: 2,
  227. },
  228. {
  229. text: "3",
  230. value: 1,
  231. },
  232. ],
  233. },
  234. ],
  235. },
  236. showLegend: {
  237. type: Boolean,
  238. default: false,
  239. },
  240. default: {
  241. type: Boolean,
  242. default: false,
  243. },
  244. // 轴
  245. yAxises: {
  246. type: Array,
  247. default: () => [
  248. {
  249. name: "机舱震动x方向",
  250. min: -0.01,
  251. max: 0,
  252. unit: "",
  253. position: "left",
  254. },
  255. {
  256. name: "机舱震动y方向",
  257. min: -0.01,
  258. max: 0,
  259. unit: "",
  260. position: "right",
  261. },
  262. {
  263. name: "机舱震动最大偏移值",
  264. min: 0,
  265. max: 1,
  266. unit: "",
  267. position: "left",
  268. },
  269. {
  270. name: "风速1",
  271. min: 0,
  272. max: 10,
  273. unit: "",
  274. position: "right",
  275. },
  276. {
  277. name: "风速2",
  278. min: 0,
  279. max: 10,
  280. unit: "",
  281. position: "left",
  282. },
  283. ],
  284. },
  285. },
  286. data() {
  287. return {
  288. id: "",
  289. chart: null,
  290. color: ["#1DA0D7","#05BB4C","#323E6F", "#EDB32F", "#DB5520"],
  291. };
  292. },
  293. computed: {
  294. legend() {
  295. return this.list.map((t) => {
  296. return t.title;
  297. });
  298. },
  299. xdata() {
  300. if (this.list.length > 1) {
  301. // this.list[1].value.forEach((lEle, lIndex) => {
  302. // console.log(
  303. // this.list[1].value.indexOf(
  304. // this.list[1].value[lIndex].text == "00:00"
  305. // )
  306. // );
  307. // });
  308. return this.list[1]?.value.map((t) => {
  309. return t.text || new Date(t.ts).formatDate("hh:mm:ss");
  310. });
  311. } else if (this.list.length == 1) {
  312. return this.list[0]?.value.map((t) => {
  313. return t.text || new Date(t.ts).formatDate("hh:mm:ss");
  314. });
  315. }
  316. },
  317. yAxis() {
  318. let result = [];
  319. let p = { left: 0, right: 0 };
  320. this.yAxises.forEach((item, index) => {
  321. result.push({
  322. type: "value",
  323. name: `${item.name}${item.unit}`,
  324. nameLocation: p[item.position] % 2 == 0 ? "end" : "start",
  325. nameGap: 10,
  326. min: item.min,
  327. max: item.max,
  328. position: item.position,
  329. offset: p[item.position] * 50,
  330. axisLabel: {
  331. formatter: "{value}",
  332. fontSize: 12,
  333. },
  334. //分格线
  335. splitLine: {
  336. lineStyle: {
  337. color: this.$store.state.themeName === "dark" ? partten.getColor("gray") : partten.getColor("black"),
  338. type: "dashed",
  339. },
  340. },
  341. axisLine: {
  342. show: true,
  343. },
  344. axisTick: {
  345. show: true,
  346. },
  347. });
  348. p[item.position]++;
  349. });
  350. return result;
  351. },
  352. series() {
  353. let result = [];
  354. this.list.forEach((value, index) => {
  355. result.push({
  356. name: value.title,
  357. type: "line",
  358. smooth: true,
  359. zlevel: index,
  360. lineStyle: {
  361. normal: {
  362. color: this.color[index],
  363. width: 1,
  364. },
  365. },
  366. showSymbol: false,
  367. yAxisIndex: value.yAxisIndex,
  368. data: value.value.map((t) => {
  369. if (t.value || t.value === 0) {
  370. return t.value;
  371. } else if (t.doubleValue || t.doubleValue === 0) {
  372. return t.doubleValue;
  373. } else {
  374. return t.longValue;
  375. }
  376. }),
  377. });
  378. });
  379. return result;
  380. },
  381. },
  382. methods: {
  383. resize() {},
  384. initChart() {
  385. const chart = echarts.init(this.$el);
  386. let option = this.option();
  387. chart.clear();
  388. chart.setOption(option);
  389. if (this.default) {
  390. chart.dispatchAction({
  391. type: "showTip",
  392. seriesIndex: 0, // 显示第几个series
  393. dataIndex: this.list[0].value.length - 600, // 显示第几个数据
  394. });
  395. }
  396. this.resize = function () {
  397. chart.resize();
  398. };
  399. window.addEventListener("resize", this.resize);
  400. },
  401. option: function () {
  402. let themeName = '';
  403. this.$store.state.themeName === "dark" ? themeName = true : themeName = false;
  404. return {
  405. color: this.color,
  406. tooltip: {
  407. trigger: "axis",
  408. triggerOn:this.default?"click":"mousemove",
  409. alwaysShowContent:true,
  410. backgroundColor: themeName ? "rgba(0,0,0,0.4)" : "rgba(255,255,255,0.5)",
  411. borderColor: themeName ? partten.getColor("gray"): "#000",
  412. textStyle: {
  413. color: themeName ? "#fff" : "#000",
  414. fontSize: 12,
  415. },
  416. },
  417. legend: {
  418. show: this.showLegend,
  419. data: this.legend,
  420. top: "top",
  421. icon: "circle",
  422. itemWidth: 6,
  423. inactiveColor: themeName ? partten.getColor("gray"): "#000",
  424. textStyle: {
  425. color: themeName ? partten.getColor("grayl"): "#000",
  426. fontSize: 12,
  427. },
  428. },
  429. grid: {
  430. top: 27,
  431. left: 16,
  432. right: 16,
  433. bottom: 0,
  434. containLabel: true,
  435. },
  436. xAxis: [
  437. {
  438. type: "category",
  439. boundaryGap: false,
  440. axisLabel: {
  441. interval:
  442. Number((this.xdata.length / 8).toFixed(0)) > 2
  443. ? Number((this.xdata.length / 8).toFixed(0))
  444. : 0,
  445. showMinLabel: true,
  446. showMaxLabel: true,
  447. formatter: "{value}",
  448. fontSize: 12,
  449. textStyle: {
  450. color: themeName ? partten.getColor("gray"): "#000",
  451. },
  452. },
  453. data: this.xdata,
  454. },
  455. ],
  456. yAxis: {
  457. type: "value",
  458. axisLabel: {
  459. formatter: "{value}",
  460. fontSize: 14,
  461. },
  462. axisLine: {
  463. show: false,
  464. },
  465. splitLine: {
  466. show: true,
  467. lineStyle: {
  468. color: "#606769",
  469. type: "dashed",
  470. },
  471. },
  472. },
  473. // yAxis: this.yAxis,
  474. series: this.series,
  475. };
  476. },
  477. reload: function () {
  478. const chart = echarts.getInstanceByDom(this.$el);
  479. chart.clear();
  480. let option = this.option();
  481. chart.setOption(option);
  482. },
  483. },
  484. created() {
  485. this.id = "pie-chart-" + util.newGUID();
  486. },
  487. mounted() {
  488. this.$nextTick(() => {
  489. this.$el.style.width = this.width;
  490. this.$el.style.height = this.height;
  491. this.initChart();
  492. });
  493. },
  494. beforeUpdate() {
  495. this.$nextTick(() => {
  496. this.initChart();
  497. });
  498. },
  499. unmounted() {
  500. window.removeEventListener("resize", this.resize);
  501. },
  502. };
  503. </script>
  504. <style lang="less">
  505. .chart {
  506. width: 100%;
  507. height: 100%;
  508. display: inline-block;
  509. }
  510. </style>