multiple-line-chart.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <template>
  2. <div class="chart" :id="id"></div>
  3. </template>
  4. <script>
  5. import * as echarts from "echarts";
  6. export default {
  7. name: "multiple-line-chart",
  8. componentName: "multiple-line-chart",
  9. props: {
  10. width: {
  11. type: String,
  12. default: "100%",
  13. },
  14. height: {
  15. type: String,
  16. default: "13.889vh",
  17. },
  18. // 数据
  19. list: {
  20. type: Array,
  21. default: () => [
  22. {
  23. title: "日发电量",
  24. yAxisIndex: 0,
  25. value: [
  26. {
  27. text: "1",
  28. value: 1,
  29. },
  30. {
  31. text: "2",
  32. value: 2,
  33. },
  34. {
  35. text: "3",
  36. value: 1,
  37. },
  38. {
  39. text: "4",
  40. value: 4,
  41. },
  42. {
  43. text: "5",
  44. value: 6,
  45. },
  46. {
  47. text: "6",
  48. value: 2,
  49. },
  50. {
  51. text: "7",
  52. value: 3,
  53. },
  54. {
  55. text: "8",
  56. value: 8,
  57. },
  58. {
  59. text: "9",
  60. value: 3,
  61. },
  62. {
  63. text: "10",
  64. value: 2,
  65. },
  66. {
  67. text: "11",
  68. value: 5,
  69. },
  70. {
  71. text: "12",
  72. value: 8,
  73. },
  74. {
  75. text: "13",
  76. value: 3,
  77. },
  78. {
  79. text: "14",
  80. value: 9,
  81. },
  82. {
  83. text: "15",
  84. value: 3,
  85. },
  86. {
  87. text: "16",
  88. value: 2,
  89. },
  90. {
  91. text: "17",
  92. value: 1,
  93. },
  94. {
  95. text: "18",
  96. value: 3,
  97. },
  98. {
  99. text: "19",
  100. value: 4,
  101. },
  102. {
  103. text: "20",
  104. value: 9,
  105. },
  106. {
  107. text: "21",
  108. value: 2,
  109. },
  110. {
  111. text: "22",
  112. value: 1,
  113. },
  114. {
  115. text: "23",
  116. value: 2,
  117. },
  118. {
  119. text: "24",
  120. value: 3,
  121. },
  122. {
  123. text: "25",
  124. value: 3,
  125. },
  126. {
  127. text: "26",
  128. value: 4,
  129. },
  130. {
  131. text: "27",
  132. value: 9,
  133. },
  134. {
  135. text: "28",
  136. value: 7,
  137. },
  138. {
  139. text: "29",
  140. value: 4,
  141. },
  142. {
  143. text: "30",
  144. value: 1,
  145. },
  146. {
  147. text: "31",
  148. value: 2,
  149. },
  150. ],
  151. },
  152. {
  153. title: "上网电量",
  154. yAxisIndex: 0,
  155. value: [
  156. {
  157. text: "1",
  158. value: 1,
  159. },
  160. {
  161. text: "2",
  162. value: 2,
  163. },
  164. {
  165. text: "3",
  166. value: 1,
  167. },
  168. {
  169. text: "4",
  170. value: 3,
  171. },
  172. ],
  173. },
  174. {
  175. title: "购网电量",
  176. yAxisIndex: 0,
  177. value: [
  178. {
  179. text: "1",
  180. value: 1,
  181. },
  182. ],
  183. },
  184. {
  185. title: "风速",
  186. yAxisIndex: 1,
  187. value: [
  188. {
  189. text: "1",
  190. value: 100,
  191. },
  192. {
  193. text: "2",
  194. value: 200,
  195. },
  196. {
  197. text: "1",
  198. value: 100,
  199. },
  200. {
  201. text: "2",
  202. value: 400,
  203. },
  204. ],
  205. },
  206. ],
  207. },
  208. // 单位
  209. units: {
  210. type: Array,
  211. default: () => ["(MW)", "(风速)"],
  212. },
  213. showLegend: {
  214. type: Boolean,
  215. default: false,
  216. },
  217. hoverType: {
  218. type: String,
  219. default: "item",
  220. },
  221. },
  222. data() {
  223. return {
  224. id: "",
  225. chart: null,
  226. color: ["#05bb4c", "#1D6AEB"],
  227. };
  228. },
  229. computed: {
  230. datas() {
  231. return this.list.map((t) => {
  232. return t.value;
  233. });
  234. },
  235. legend() {
  236. return this.list.map((t) => {
  237. return t.title;
  238. });
  239. },
  240. xdata() {
  241. if (this.list.length > 0)
  242. return this.list[0].value.map((t) => {
  243. return t.text;
  244. });
  245. return [];
  246. },
  247. series() {
  248. let result = [];
  249. this.list.forEach((value, index) => {
  250. result.push({
  251. name: value.title,
  252. type: "line",
  253. smooth: true,
  254. showSymbol: false,
  255. zlevel: index,
  256. lineStyle: {
  257. normal: {
  258. color: this.color[index],
  259. width: 1,
  260. },
  261. // emphasis: {
  262. // color: this.color[index],
  263. // },
  264. },
  265. // areaStyle: {
  266. // normal: {
  267. // color:
  268. // index == 0
  269. // ? new echarts.graphic.LinearGradient(
  270. // 0,
  271. // 0,
  272. // 0,
  273. // 1,
  274. // [
  275. // {
  276. // offset: 0,
  277. // color: this.hexToRgba(this.color[index], 0.3),
  278. // },
  279. // {
  280. // offset: 1,
  281. // color: this.hexToRgba(this.color[index], 0.1),
  282. // },
  283. // ],
  284. // false
  285. // )
  286. // : "transparent",
  287. // shadowColor: this.hexToRgba(this.color[index], 0.1),
  288. // shadowBlur: 10,
  289. // },
  290. // emphasis: {
  291. // color: new echarts.graphic.LinearGradient(
  292. // 0,
  293. // 0,
  294. // 0,
  295. // 1,
  296. // [
  297. // {
  298. // offset: 0,
  299. // color: this.hexToRgba(this.color[index], 0.3),
  300. // },
  301. // {
  302. // offset: 1,
  303. // color: this.hexToRgba(this.color[index], 0.1),
  304. // },
  305. // ],
  306. // false
  307. // ),
  308. // shadowColor: this.hexToRgba(this.color[index], 0.1),
  309. // shadowBlur: 10,
  310. // },
  311. // },
  312. tooltip: {
  313. show: true,
  314. position: [10, 10],
  315. },
  316. yAxisIndex: value.yAxisIndex,
  317. data: value.value.map((t) => {
  318. return t.value;
  319. }),
  320. });
  321. });
  322. return result;
  323. },
  324. yAxis() {
  325. let result = [];
  326. this.units.forEach((value, index) => {
  327. result.push({
  328. type: "value",
  329. name: value,
  330. axisLabel: {
  331. formatter: "{value}",
  332. fontSize: 14,
  333. },
  334. axisLine: {
  335. show: false,
  336. },
  337. //分格线
  338. splitLine: {
  339. show: index == 0,
  340. lineStyle: {
  341. color: '#606769',
  342. type: "dashed",
  343. },
  344. },
  345. });
  346. });
  347. return result;
  348. },
  349. },
  350. methods: {
  351. hexToRgba(hex, opacity) {
  352. let rgbaColor = "";
  353. let reg = /^#[\da-f]{6}$/i;
  354. if (reg.test(hex)) {
  355. rgbaColor = `rgba(${parseInt("0x" + hex.slice(1, 3))},${parseInt("0x" + hex.slice(3, 5))},${parseInt("0x" + hex.slice(5, 7))},${opacity})`;
  356. }
  357. return rgbaColor;
  358. },
  359. resize() {},
  360. initChart() {
  361. let chart = echarts.init(this.$el);
  362. let option = {
  363. color: this.color,
  364. tooltip: {
  365. trigger: this.hoverType,
  366. axisPointer:
  367. this.hoverType != "item"
  368. ? {
  369. type: "cross",
  370. }
  371. : {},
  372. backgroundColor: "rgba(0,0,0,0.4)",
  373. borderColor: '#606769',
  374. textStyle: {
  375. fontSize: 16,
  376. color: "#fff",
  377. },
  378. },
  379. legend: {
  380. show: this.showLegend,
  381. data: this.legend,
  382. right: 56,
  383. icon: "circle",
  384. itemWidth: 6,
  385. inactiveColor: '#606769',
  386. textStyle: {
  387. color: '#B3BDC0',
  388. fontSize: 12,
  389. },
  390. },
  391. grid: {
  392. top: 32,
  393. left: 40,
  394. right: 40,
  395. bottom: 24,
  396. },
  397. xAxis: [
  398. {
  399. type: "category",
  400. boundaryGap: false,
  401. axisLabel: {
  402. interval:60,
  403. showMinLabel: true,
  404. showMaxLabel: true,
  405. formatter: "{value}",
  406. fontSize: 14,
  407. textStyle: {
  408. color: '#606769',
  409. },
  410. },
  411. axisLine: {
  412. show: false,
  413. },
  414. data: this.xdata,
  415. },
  416. ],
  417. yAxis: this.yAxis,
  418. series: this.series,
  419. };
  420. chart.clear();
  421. chart && option && chart.setOption(option);
  422. this.resize = function() {
  423. chart.resize();
  424. };
  425. window.addEventListener("resize", this.resize);
  426. chart.resize();
  427. },
  428. },
  429. created() {
  430. this.$nextTick(() => {
  431. this.id = "pie-chart-photo";
  432. });
  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>