multiple-y-line-chart.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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",
  10. componentName: "multiple-y-line-chart",
  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. yAxis: {
  27. min: -0.01,
  28. max: 0,
  29. unit: "",
  30. position: "left",
  31. },
  32. value: [
  33. {
  34. text: "-0.003",
  35. value: -0.003,
  36. },
  37. {
  38. text: "-0.002",
  39. value: -0.002,
  40. },
  41. {
  42. text: "-0.003",
  43. value: -0.006,
  44. },
  45. {
  46. text: "-0.003",
  47. value: -0.003,
  48. },
  49. {
  50. text: "-0.002",
  51. value: -0.002,
  52. },
  53. {
  54. text: "-0.003",
  55. value: -0.006,
  56. },
  57. {
  58. text: "-0.003",
  59. value: -0.003,
  60. },
  61. {
  62. text: "-0.002",
  63. value: -0.002,
  64. },
  65. {
  66. text: "-0.003",
  67. value: -0.006,
  68. },
  69. ],
  70. },
  71. {
  72. title: "机舱震动y方向",
  73. yAxis: {
  74. min: -0.01,
  75. max: 0,
  76. unit: "",
  77. position: "right",
  78. },
  79. value: [
  80. {
  81. text: "-0.01",
  82. value: -0.01,
  83. },
  84. {
  85. text: "-0.005",
  86. value: -0.005,
  87. },
  88. {
  89. text: "-0.008",
  90. value: -0.008,
  91. },
  92. {
  93. text: "-0.01",
  94. value: -0.01,
  95. },
  96. {
  97. text: "-0.005",
  98. value: -0.005,
  99. },
  100. {
  101. text: "-0.008",
  102. value: -0.008,
  103. },
  104. {
  105. text: "-0.01",
  106. value: -0.01,
  107. },
  108. {
  109. text: "-0.005",
  110. value: -0.005,
  111. },
  112. {
  113. text: "-0.008",
  114. value: -0.008,
  115. },
  116. ],
  117. },
  118. {
  119. title: "机舱震动最大偏移值",
  120. yAxis: {
  121. min: 0,
  122. max: 1,
  123. unit: "",
  124. position: "left",
  125. },
  126. value: [
  127. {
  128. text: "1",
  129. value: 1,
  130. },
  131. {
  132. text: "0.05",
  133. value: 0.05,
  134. },
  135. {
  136. text: "0.5",
  137. value: 0.5,
  138. },
  139. {
  140. text: "1",
  141. value: 1,
  142. },
  143. {
  144. text: "0.05",
  145. value: 0.05,
  146. },
  147. {
  148. text: "0.5",
  149. value: 0.5,
  150. },
  151. {
  152. text: "1",
  153. value: 1,
  154. },
  155. {
  156. text: "0.05",
  157. value: 0.05,
  158. },
  159. {
  160. text: "0.5",
  161. value: 0.5,
  162. },
  163. ],
  164. },
  165. {
  166. title: "风速1",
  167. yAxis: {
  168. min: 0,
  169. max: 10,
  170. unit: "",
  171. position: "right",
  172. },
  173. value: [
  174. {
  175. text: "1",
  176. value: 1,
  177. },
  178. {
  179. text: "5",
  180. value: 5,
  181. },
  182. {
  183. text: "3",
  184. value: 1,
  185. },
  186. {
  187. text: "1",
  188. value: 1,
  189. },
  190. {
  191. text: "5",
  192. value: 5,
  193. },
  194. {
  195. text: "3",
  196. value: 1,
  197. },
  198. {
  199. text: "1",
  200. value: 1,
  201. },
  202. {
  203. text: "5",
  204. value: 5,
  205. },
  206. {
  207. text: "3",
  208. value: 1,
  209. },
  210. ],
  211. },
  212. {
  213. title: "风速2",
  214. yAxis: {
  215. min: 0,
  216. max: 10,
  217. unit: "",
  218. position: "left",
  219. },
  220. value: [
  221. {
  222. text: "1",
  223. value: 1,
  224. },
  225. {
  226. text: "2",
  227. value: 2,
  228. },
  229. {
  230. text: "3",
  231. value: 1,
  232. },
  233. {
  234. text: "1",
  235. value: 1,
  236. },
  237. {
  238. text: "2",
  239. value: 2,
  240. },
  241. {
  242. text: "3",
  243. value: 1,
  244. },
  245. {
  246. text: "1",
  247. value: 1,
  248. },
  249. {
  250. text: "2",
  251. value: 2,
  252. },
  253. {
  254. text: "3",
  255. value: 1,
  256. },
  257. ],
  258. },
  259. ],
  260. },
  261. showLegend: {
  262. type: Boolean,
  263. default: false,
  264. },
  265. },
  266. data() {
  267. return {
  268. id: "",
  269. chart: null,
  270. color: ["#323E6F", "#1DA0D7", "#05BB4C", "#EDB32F", "#DB5520"],
  271. };
  272. },
  273. computed: {
  274. legend() {
  275. return this.list.map((t) => {
  276. return t.title;
  277. });
  278. },
  279. xdata() {
  280. return this.list[0].value.map((t) => {
  281. return t.text;
  282. });
  283. },
  284. yAxis() {
  285. let result = [];
  286. let p = {left: 0, right: 0};
  287. this.list.forEach((item, index) => {
  288. result.push({
  289. type: "value",
  290. name: `${item.title}${item.yAxis.unit}`,
  291. nameLocation: p[item.yAxis.position] % 2 == 0 ? "end" : "start",
  292. min: item.yAxis.min,
  293. max: item.yAxis.max,
  294. position: item.yAxis.position,
  295. offset: p[item.yAxis.position] * 60,
  296. axisLabel: {
  297. formatter: "{value}",
  298. fontSize: util.vh(14),
  299. },
  300. //分格线
  301. splitLine: {
  302. lineStyle: {
  303. color: partten.getColor("gray"),
  304. type: "dashed",
  305. },
  306. },
  307. axisLine: {
  308. show: true,
  309. },
  310. axisTick: {
  311. show: true
  312. }
  313. });
  314. p[item.yAxis.position]++;
  315. });
  316. return result;
  317. },
  318. series() {
  319. let result = [];
  320. this.list.forEach((value, index) => {
  321. result.push({
  322. name: value.title,
  323. type: "line",
  324. smooth: true,
  325. zlevel: index,
  326. lineStyle: {
  327. normal: {
  328. color: this.color[index],
  329. width: 1,
  330. },
  331. },
  332. yAxisIndex: index,
  333. data: value.value.map((t) => {
  334. return t.value;
  335. }),
  336. });
  337. });
  338. return result;
  339. },
  340. },
  341. methods: {
  342. initChart() {
  343. const chart = echarts.init(this.$el);
  344. let option = {
  345. color: this.color,
  346. tooltip: {
  347. trigger: "axis",
  348. backgroundColor: partten.getColor("gray"),
  349. textStyle: {
  350. color: "#fff",
  351. fontSize: util.vh(16),
  352. },
  353. },
  354. legend: {
  355. show: this.showLegend,
  356. data: this.legend,
  357. right: 56,
  358. icon: "circle",
  359. itemWidth: 6,
  360. inactiveColor: partten.getColor("gray"),
  361. textStyle: {
  362. color: partten.getColor("grayl"),
  363. fontSize: 12,
  364. },
  365. },
  366. dataZoom: [{
  367. show: true,
  368. height: 30,
  369. xAxisIndex: [0],
  370. bottom: 30,
  371. "start": 0,
  372. "end": 10,
  373. handleIcon: 'path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z',
  374. handleSize: '110%',
  375. handleStyle: {
  376. color: "#05bb4c",
  377. },
  378. textStyle:{
  379. color:"rgba(204,187,225,0.5)",
  380. },
  381. fillerColor:"rgba(5,187,76,0.4)",
  382. borderColor: "rgba(5,187,76,0.5)",
  383. }, {
  384. type: "inside",
  385. show: true,
  386. height: 15,
  387. start: 1,
  388. end: 35
  389. }],
  390. grid: {
  391. top: util.vh(80),
  392. left: util.vh(80),
  393. right: util.vh(80),
  394. bottom: util.vh(64),
  395. containLabel: true,
  396. },
  397. xAxis: [
  398. {
  399. type: "category",
  400. boundaryGap: false,
  401. axisLabel: {
  402. formatter: "{value}",
  403. fontSize: util.vh(14),
  404. textStyle: {
  405. color: partten.getColor("gray"),
  406. },
  407. },
  408. data: this.xdata,
  409. },
  410. ],
  411. yAxis: this.yAxis,
  412. series: this.series,
  413. };
  414. console.log(option)
  415. chart.clear();
  416. chart.setOption(option);
  417. this.resize = function() {
  418. chart.resize();
  419. };
  420. window.addEventListener("resize", this.resize);
  421. },
  422. datazoom: function (start, end) {
  423. const chart = echarts.getInstanceByDom(this.$el);
  424. chart.dispatchAction({
  425. type: 'dataZoom',
  426. start: start,
  427. end: end,
  428. });
  429. },
  430. },
  431. created() {
  432. this.id = "pie-chart-" + util.newGUID();
  433. },
  434. mounted() {
  435. this.$nextTick(() => {
  436. this.$el.style.width = this.width;
  437. this.$el.style.height = this.height;
  438. this.initChart();
  439. });
  440. },
  441. updated() {
  442. this.$nextTick(() => {
  443. this.initChart();
  444. });
  445. },
  446. unmounted() {
  447. window.removeEventListener("resize", this.resize);
  448. },
  449. };
  450. </script>
  451. <style lang="less">
  452. .chart {
  453. width: 100%;
  454. height: 100%;
  455. display: inline-block;
  456. }
  457. </style>