multiple-line-chart.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. console.log('list0:',this.list[0])
  246. console.log('tvalue:',t.value)
  247. // return t.text;
  248. return t.value
  249. });
  250. return [];
  251. },
  252. series() {
  253. let result = [];
  254. this.list.forEach((value, index) => {
  255. console.log('list:',this.list)
  256. result.push({
  257. name: value.title,
  258. type: "line",
  259. smooth: true,
  260. showSymbol: false,
  261. zlevel: index,
  262. lineStyle: {
  263. normal: {
  264. // color: index != 0 ? partten.getColor("gray") : this.color[index],
  265. color: partten.getColor("gray"),
  266. width: 1,
  267. },
  268. emphasis: {
  269. color: this.color[index],
  270. },
  271. },
  272. areaStyle: {
  273. normal: {
  274. color:
  275. index == 0
  276. ? new echarts.graphic.LinearGradient(
  277. 0,
  278. 0,
  279. 0,
  280. 1,
  281. [
  282. {
  283. offset: 0,
  284. color: this.hexToRgba(this.color[index], 0.3),
  285. },
  286. {
  287. offset: 1,
  288. color: this.hexToRgba(this.color[index], 0.1),
  289. },
  290. ],
  291. false
  292. )
  293. : "transparent",
  294. shadowColor: this.hexToRgba(this.color[index], 0.1),
  295. shadowBlur: 10,
  296. },
  297. emphasis: {
  298. color: new echarts.graphic.LinearGradient(
  299. 0,
  300. 0,
  301. 0,
  302. 1,
  303. [
  304. {
  305. offset: 0,
  306. color: this.hexToRgba(this.color[index], 0.3),
  307. },
  308. {
  309. offset: 1,
  310. color: this.hexToRgba(this.color[index], 0.1),
  311. },
  312. ],
  313. false
  314. ),
  315. shadowColor: this.hexToRgba(this.color[index], 0.1),
  316. shadowBlur: 10,
  317. },
  318. },
  319. tooltip: {
  320. show: true,
  321. position: [10, 10],
  322. },
  323. yAxisIndex: value.yAxisIndex,
  324. data: value.value.map((t) => {
  325. return t.value;
  326. }),
  327. });
  328. });
  329. return result;
  330. },
  331. yAxis() {
  332. let result = [];
  333. console.log('units:',this.units)
  334. this.units.forEach((value, index) => {
  335. result.push({
  336. type: "value",
  337. name: value,
  338. axisLabel: {
  339. formatter: "{value}",
  340. fontSize: util.vh(14),
  341. },
  342. axisLine: {
  343. show: false,
  344. },
  345. //分格线
  346. splitLine: {
  347. show: index == 0,
  348. lineStyle: {
  349. color: partten.getColor("gray"),
  350. type: "dashed",
  351. },
  352. },
  353. });
  354. });
  355. console.log('result:',result)
  356. return result;
  357. },
  358. },
  359. methods: {
  360. hexToRgba(hex, opacity) {
  361. let rgbaColor = "";
  362. let reg = /^#[\da-f]{6}$/i;
  363. if (reg.test(hex)) {
  364. rgbaColor = `rgba(${parseInt("0x" + hex.slice(1, 3))},${parseInt("0x" + hex.slice(3, 5))},${parseInt("0x" + hex.slice(5, 7))},${opacity})`;
  365. }
  366. return rgbaColor;
  367. },
  368. resize() {},
  369. initChart() {
  370. let chart = echarts.init(this.$el);
  371. let option = {
  372. color: this.color,
  373. tooltip: {
  374. trigger: this.hoverType,
  375. axisPointer:
  376. this.hoverType != "item"
  377. ? {
  378. type: "cross",
  379. }
  380. : {},
  381. backgroundColor: "rgba(0,0,0,0.4)",
  382. borderColor: partten.getColor("gray"),
  383. textStyle: {
  384. fontSize: util.vh(16),
  385. color: "#fff",
  386. },
  387. },
  388. legend: {
  389. show: this.showLegend,
  390. data: this.legend,
  391. right: 56,
  392. icon: "circle",
  393. itemWidth: 6,
  394. inactiveColor: partten.getColor("gray"),
  395. textStyle: {
  396. color: partten.getColor("grayl"),
  397. fontSize: 12,
  398. },
  399. },
  400. grid: {
  401. top: 32,
  402. left: 40,
  403. right: 40,
  404. bottom: 24,
  405. },
  406. xAxis: [
  407. {
  408. name:"(m/s)",
  409. type: "category",
  410. boundaryGap: false,
  411. axisLabel: {
  412. formatter: "{value}",
  413. fontSize: util.vh(14),
  414. textStyle: {
  415. color: partten.getColor("gray"),
  416. },
  417. },
  418. axisLine: {
  419. show: false,
  420. },
  421. data: this.xdata,
  422. },
  423. ],
  424. yAxis: this.yAxis,
  425. series: this.series,
  426. };
  427. console.log('this:',this)
  428. chart.clear();
  429. chart && option && chart.setOption(option);
  430. this.resize = function() {
  431. chart.resize();
  432. };
  433. window.addEventListener("resize", this.resize);
  434. chart.resize();
  435. },
  436. },
  437. created() {
  438. this.$nextTick(() => {
  439. this.id = "pie-chart-" + util.newGUID();
  440. });
  441. },
  442. mounted() {
  443. this.$nextTick(() => {
  444. this.$el.style.width = this.width;
  445. this.$el.style.height = this.height;
  446. this.initChart();
  447. });
  448. },
  449. updated() {
  450. this.$nextTick(() => {
  451. this.initChart();
  452. });
  453. },
  454. unmounted() {
  455. window.removeEventListener("resize", this.resize);
  456. },
  457. };
  458. </script>
  459. <style lang="less">
  460. .chart {
  461. width: 100%;
  462. height: 100%;
  463. display: inline-block;
  464. }
  465. </style>