multiple-line-chart.vue 10.0 KB

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