img-line-chart.vue 11 KB

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