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