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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. // 轴
  241. yAxises: {
  242. type: Array,
  243. default: () => [
  244. {
  245. name: "机舱震动x方向",
  246. min: -0.01,
  247. max: 0,
  248. unit: "",
  249. position: "left",
  250. },
  251. {
  252. name: "机舱震动y方向",
  253. min: -0.01,
  254. max: 0,
  255. unit: "",
  256. position: "right",
  257. },
  258. {
  259. name: "机舱震动最大偏移值",
  260. min: 0,
  261. max: 1,
  262. unit: "",
  263. position: "left",
  264. },
  265. {
  266. name: "风速1",
  267. min: 0,
  268. max: 10,
  269. unit: "",
  270. position: "right",
  271. },
  272. {
  273. name: "风速2",
  274. min: 0,
  275. max: 10,
  276. unit: "",
  277. position: "left",
  278. },
  279. ],
  280. },
  281. },
  282. data() {
  283. return {
  284. id: "",
  285. chart: null,
  286. color: ["#323E6F", "#1DA0D7", "#05BB4C", "#EDB32F", "#DB5520"],
  287. };
  288. },
  289. computed: {
  290. legend() {
  291. return this.list.map((t) => {
  292. return t && t.title;
  293. });
  294. },
  295. xdata() {
  296. return this.list[0]?.value.map((t) => {
  297. return t.text;
  298. });
  299. },
  300. yAxis() {
  301. let result = [];
  302. let p = { left: 0, right: 0 };
  303. this.yAxises.forEach((item, index) => {
  304. result.push({
  305. type: "value",
  306. name: `${item.name}${item.unit}`,
  307. nameLocation: p[item.position] % 2 == 0 ? "end" : "start",
  308. nameGap: 10,
  309. min: item.min,
  310. max: item.max,
  311. position: item.position,
  312. offset: p[item.position] * 50,
  313. axisLabel: {
  314. formatter: "{value}",
  315. fontSize: 12,
  316. },
  317. //分格线
  318. splitLine: {
  319. lineStyle: {
  320. color: partten.getColor("gray"),
  321. type: "dashed",
  322. },
  323. },
  324. axisLine: {
  325. show: true,
  326. },
  327. axisTick: {
  328. show: true,
  329. },
  330. });
  331. p[item.position]++;
  332. });
  333. return result;
  334. },
  335. series() {
  336. let result = [];
  337. this.list.forEach((value, index) => {
  338. result.push({
  339. name: value.title,
  340. type: "line",
  341. smooth: true,
  342. zlevel: index,
  343. lineStyle: {
  344. normal: {
  345. color: this.color[index],
  346. width: 1,
  347. },
  348. },
  349. showSymbol: false,
  350. yAxisIndex: value.yAxisIndex,
  351. data: value.value.map((t) => {
  352. return t.value;
  353. }),
  354. });
  355. });
  356. return result;
  357. },
  358. },
  359. methods: {
  360. resize() {},
  361. initChart() {
  362. const chart = echarts.init(this.$el);
  363. let option = this.option();
  364. chart.clear();
  365. chart.setOption(option);
  366. this.resize = function () {
  367. chart.resize();
  368. };
  369. window.addEventListener("resize", this.resize);
  370. },
  371. option: function () {
  372. return {
  373. color: this.color,
  374. tooltip: {
  375. trigger: "axis",
  376. backgroundColor: "rgba(0,0,0,0.4)",
  377. borderColor: partten.getColor("gray"),
  378. textStyle: {
  379. color: "#fff",
  380. fontSize: 12,
  381. },
  382. },
  383. legend: {
  384. show: this.showLegend,
  385. data: this.legend,
  386. top: "top",
  387. icon: "circle",
  388. itemWidth: 6,
  389. inactiveColor: partten.getColor("gray"),
  390. textStyle: {
  391. color: partten.getColor("grayl"),
  392. fontSize: 12,
  393. },
  394. },
  395. grid: {
  396. top: 27,
  397. left: 16,
  398. right: 16,
  399. bottom: 0,
  400. containLabel: true,
  401. },
  402. xAxis: [
  403. {
  404. type: "category",
  405. boundaryGap: false,
  406. axisLabel: {
  407. formatter: "{value}",
  408. fontSize: 12,
  409. textStyle: {
  410. color: partten.getColor("gray"),
  411. },
  412. },
  413. data: this.xdata,
  414. },
  415. ],
  416. yAxis: this.yAxis,
  417. series: this.series,
  418. };
  419. },
  420. reload: function () {
  421. const chart = echarts.getInstanceByDom(this.$el);
  422. chart.clear();
  423. let option = this.option();
  424. chart.setOption(option);
  425. },
  426. },
  427. created() {
  428. this.id = "pie-chart-" + util.newGUID();
  429. },
  430. mounted() {
  431. this.$nextTick(() => {
  432. this.$el.style.width = this.width;
  433. this.$el.style.height = this.height;
  434. this.initChart();
  435. });
  436. },
  437. updated() {
  438. this.$nextTick(() => {
  439. this.initChart();
  440. });
  441. },
  442. unmounted() {
  443. window.removeEventListener("resize", this.resize);
  444. },
  445. };
  446. </script>
  447. <style lang="less">
  448. .chart {
  449. width: 100%;
  450. height: 100%;
  451. display: inline-block;
  452. }
  453. </style>