multiple-line-chart.vue 11 KB

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