multiple-line-chart.vue 11 KB

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