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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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.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: -15,
  313. axisLabel: {
  314. formatter: "{value}",
  315. fontSize: 12,
  316. textStyle: {
  317. color:
  318. this.$store.state.themeName === "dark"
  319. ? "rgb(116,124,128)"
  320. : "#000",
  321. },
  322. },
  323. //分格线
  324. splitLine: {
  325. lineStyle: {
  326. color: partten.getColor("gray"),
  327. type: "dashed",
  328. },
  329. },
  330. axisLine: {
  331. show: true,
  332. },
  333. axisTick: {
  334. show: true,
  335. },
  336. });
  337. p[item.position]++;
  338. });
  339. return result;
  340. },
  341. series() {
  342. let result = [];
  343. this.list.forEach((value, index) => {
  344. result.push({
  345. name: value.title,
  346. type: "line",
  347. smooth: true,
  348. zlevel: index,
  349. lineStyle: {
  350. normal: {
  351. color: this.color[index],
  352. width: 1,
  353. },
  354. },
  355. showSymbol: false,
  356. yAxisIndex: value.yAxisIndex,
  357. data: value.value.map((t) => {
  358. return t.value;
  359. }),
  360. });
  361. });
  362. return result;
  363. },
  364. },
  365. methods: {
  366. resize() {},
  367. initChart() {
  368. const chart = echarts.init(this.$el);
  369. let option = this.option();
  370. chart.clear();
  371. chart.setOption(option);
  372. this.resize = function () {
  373. chart.resize();
  374. };
  375. window.addEventListener("resize", this.resize);
  376. },
  377. option: function () {
  378. return {
  379. color: this.color,
  380. tooltip: {
  381. trigger: "axis",
  382. backgroundColor:
  383. this.$store.state.themeName === "dark"
  384. ? "rgba(0,0,0,0.4)"
  385. : "rgba(255,255,255,0.5)",
  386. borderColor:
  387. this.$store.state.themeName === "dark"
  388. ? partten.getColor("gray")
  389. : "#000",
  390. textStyle: {
  391. color: this.$store.state.themeName === "dark" ? "#fff" : "#000",
  392. fontSize: 12,
  393. },
  394. },
  395. legend: {
  396. show: this.showLegend,
  397. data: this.legend,
  398. top: "top",
  399. icon: "circle",
  400. itemWidth: 6,
  401. inactiveColor: partten.getColor("gray"),
  402. textStyle: {
  403. color:
  404. this.$store.state.themeName === "dark"
  405. ? partten.getColor("grayl")
  406. : "#000",
  407. fontSize: 12,
  408. },
  409. },
  410. grid: {
  411. top: 27,
  412. left: 16,
  413. right: 16,
  414. bottom: 0,
  415. containLabel: true,
  416. },
  417. xAxis: [
  418. {
  419. type: "category",
  420. boundaryGap: false,
  421. axisLabel: {
  422. formatter: "{value}",
  423. fontSize: 12,
  424. textStyle: {
  425. color:
  426. this.$store.state.themeName === "dark"
  427. ? partten.getColor("gray")
  428. : "#000",
  429. },
  430. },
  431. data: this.xdata,
  432. },
  433. ],
  434. yAxis: this.yAxis,
  435. series: this.series,
  436. };
  437. },
  438. reload: function () {
  439. const chart = echarts.getInstanceByDom(this.$el);
  440. chart.clear();
  441. let option = this.option();
  442. chart.setOption(option);
  443. },
  444. },
  445. created() {
  446. this.id = "pie-chart-" + util.newGUID();
  447. },
  448. mounted() {
  449. this.$nextTick(() => {
  450. this.$el.style.width = this.width;
  451. this.$el.style.height = this.height;
  452. this.initChart();
  453. });
  454. },
  455. updated() {
  456. this.$nextTick(() => {
  457. this.initChart();
  458. });
  459. },
  460. unmounted() {
  461. window.removeEventListener("resize", this.resize);
  462. },
  463. watch: {
  464. "$store.state.themeName"() {
  465. this.initChart();
  466. },
  467. },
  468. };
  469. </script>
  470. <style lang="less">
  471. .chart {
  472. width: 100%;
  473. height: 100%;
  474. display: inline-block;
  475. }
  476. </style>