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

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