multiple-line-chart.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. },
  216. data () {
  217. return {
  218. id: "",
  219. chart: null,
  220. color: ["#05bb4c", "#4b55ae", "#fa8c16", "#f8de5b"],
  221. };
  222. },
  223. computed: {
  224. datas () {
  225. return this.list.map((t) => {
  226. return t.value;
  227. });
  228. },
  229. xdata () {
  230. return this.list[0].value.map((t) => {
  231. return t.text;
  232. });
  233. },
  234. series () {
  235. let result = [];
  236. this.list.forEach((value, index) => {
  237. result.push({
  238. name: value.title,
  239. type: "line",
  240. smooth: true,
  241. showSymbol: false,
  242. zlevel: index,
  243. lineStyle: {
  244. normal: {
  245. color: this.color[index],
  246. },
  247. },
  248. areaStyle: {
  249. normal: {
  250. color: new echarts.graphic.LinearGradient(
  251. 0,
  252. 0,
  253. 0,
  254. 1,
  255. [
  256. {
  257. offset: 0,
  258. color: this.hexToRgba(this.color[index], 0.3),
  259. },
  260. {
  261. offset: 1,
  262. color: this.hexToRgba(this.color[index], 0.1),
  263. },
  264. ],
  265. false
  266. ),
  267. shadowColor: this.hexToRgba(this.color[index], 0.1),
  268. shadowBlur: 10,
  269. },
  270. },
  271. yAxisIndex: value.yAxisIndex,
  272. data: value.value.map((t) => {
  273. return t.value;
  274. }),
  275. });
  276. });
  277. return result;
  278. },
  279. yAxis () {
  280. let result = [];
  281. this.units.forEach((value, index) => {
  282. result.push({
  283. type: "value",
  284. name: value,
  285. axisLabel: {
  286. formatter: "{value}",
  287. fontSize: util.vh(14),
  288. },
  289. //分格线
  290. splitLine: {
  291. lineStyle: {
  292. color: "#999",
  293. type: "dashed",
  294. },
  295. },
  296. });
  297. });
  298. return result;
  299. },
  300. },
  301. methods: {
  302. hexToRgba (hex, opacity) {
  303. let rgbaColor = "";
  304. let reg = /^#[\da-f]{6}$/i;
  305. if (reg.test(hex)) {
  306. rgbaColor = `rgba(${parseInt("0x" + hex.slice(1, 3))},${parseInt("0x" + hex.slice(3, 5))},${parseInt("0x" + hex.slice(5, 7))},${opacity})`;
  307. }
  308. return rgbaColor;
  309. },
  310. initChart () {
  311. let option = {
  312. color: this.color,
  313. tooltip: {
  314. trigger: "item",
  315. backgroundColor: "rgba(255,255,255,0.3)",
  316. textStyle: {
  317. color: "#fff",
  318. fontSize: util.vh(16),
  319. },
  320. formatter: function (param) {
  321. console.log(param);
  322. return param.name + "<br >" + param.marker + param.seriesName + ":" + param.value;
  323. },
  324. },
  325. grid: {
  326. top: util.vh(32),
  327. left: util.vh(40),
  328. right: util.vh(40),
  329. bottom: util.vh(24),
  330. },
  331. xAxis: [
  332. {
  333. type: "category",
  334. boundaryGap: false,
  335. axisLabel: {
  336. formatter: "{value}",
  337. fontSize: util.vh(14),
  338. textStyle: {
  339. color: "#333",
  340. },
  341. },
  342. data: this.xdata,
  343. },
  344. ],
  345. yAxis: this.yAxis,
  346. series: this.series,
  347. };
  348. this.chart.setOption(option);
  349. },
  350. },
  351. created () {
  352. this.id = "pie-chart-" + util.newGUID();
  353. },
  354. mounted () {
  355. this.$el.style.width = this.width;
  356. this.$el.style.height = this.height;
  357. this.chart = echarts.init(this.$el);
  358. this.initChart();
  359. },
  360. updated () {
  361. this.initChart();
  362. },
  363. };
  364. </script>
  365. <style lang="less">
  366. .chart {
  367. width: 100%;
  368. height: 100%;
  369. display: inline-block;
  370. }
  371. </style>