multiple-bar-chart.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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-bar-chart",
  10. componentName: "multiple-bar-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. },
  34. {
  35. title: "上网电量",
  36. yAxisIndex: 0,
  37. value: [
  38. {
  39. text: "1日",
  40. value: 1,
  41. },
  42. {
  43. text: "2日",
  44. value: 2,
  45. },
  46. {
  47. text: "3日",
  48. value: 1,
  49. },
  50. {
  51. text: "4日",
  52. value: 3,
  53. },
  54. {
  55. text: "5日",
  56. value: 3,
  57. },
  58. {
  59. text: "6日",
  60. value: 3,
  61. },
  62. {
  63. text: "7日",
  64. value: 3,
  65. },
  66. ],
  67. },
  68. {
  69. title: "购网电量",
  70. yAxisIndex: 0,
  71. value: [
  72. {
  73. text: "1日",
  74. value: 1,
  75. },
  76. {
  77. text: "2日",
  78. value: 2,
  79. },
  80. {
  81. text: "3日",
  82. value: 1,
  83. },
  84. {
  85. text: "4日",
  86. value: 3,
  87. },
  88. {
  89. text: "5日",
  90. value: 3,
  91. },
  92. {
  93. text: "6日",
  94. value: 3,
  95. },
  96. {
  97. text: "7日",
  98. value: 3,
  99. },
  100. ],
  101. },
  102. {
  103. title: "风速",
  104. yAxisIndex: 1,
  105. value: [
  106. {
  107. text: "1日",
  108. value: 1,
  109. },
  110. {
  111. text: "2日",
  112. value: 2,
  113. },
  114. {
  115. text: "3日",
  116. value: 1,
  117. },
  118. {
  119. text: "4日",
  120. value: 3,
  121. },
  122. {
  123. text: "5日",
  124. value: 3,
  125. },
  126. {
  127. text: "6日",
  128. value: 3,
  129. },
  130. {
  131. text: "7日",
  132. value: 3,
  133. },
  134. ],
  135. },
  136. ],
  137. },
  138. // 单位
  139. units: {
  140. type: Array,
  141. default: () => ["(万kWh)", "(风速)"],
  142. },
  143. // 显示 legend
  144. showLegend: {
  145. type: Boolean,
  146. default: true,
  147. },
  148. // 颜色#
  149. color: {
  150. type: Array,
  151. default: () => [
  152. "#05bb4c",
  153. "#4b55ae",
  154. "#fa8c16",
  155. "#f8de5b",
  156. "#1a93cf",
  157. "#c531c7",
  158. "#bd3338",
  159. ],
  160. },
  161. showAnimation: {
  162. type: Boolean,
  163. default: true,
  164. },
  165. // 柱子最大宽度
  166. barMaxWidth: {
  167. type: Number || String,
  168. default: 0,
  169. },
  170. // 柱子间距
  171. barGap: {
  172. type: Number || String,
  173. default: 0,
  174. },
  175. },
  176. data() {
  177. return {
  178. id: "",
  179. chart: null,
  180. firstAnimation: true,
  181. };
  182. },
  183. computed: {
  184. legend() {
  185. return this.list.map((t) => {
  186. return t.title;
  187. });
  188. },
  189. xdata() {
  190. let result = [];
  191. if (this.list && this.list.length > 0 && this.list[0].value.length > 0) {
  192. result = this.list[0].value.map((t) => {
  193. return t.text;
  194. });
  195. }
  196. return result;
  197. },
  198. ydata() {
  199. let result = [];
  200. this.units.forEach((value, index) => {
  201. let data = null;
  202. if (index == 0) {
  203. data = {
  204. type: "value",
  205. name: value,
  206. axisLabel: {
  207. formatter: "{value} ",
  208. fontSize: 12,
  209. textStyle: {
  210. color:
  211. this.$store.state.themeName === "dark"
  212. ? "rgb(116,124,128)"
  213. : "#000",
  214. },
  215. },
  216. //分格线
  217. splitLine: {
  218. lineStyle: {
  219. color:
  220. this.$store.state.themeName === "dark" ? "#5a6162" : "#000",
  221. type: "dashed",
  222. },
  223. },
  224. };
  225. } else {
  226. data = {
  227. type: "value",
  228. name: value,
  229. axisLabel: {
  230. formatter: "{value}",
  231. fontSize: 12,
  232. textStyle: {
  233. color:
  234. this.$store.state.themeName === "dark"
  235. ? "rgb(116,124,128)"
  236. : "#000",
  237. },
  238. },
  239. //分格线
  240. splitLine: {
  241. show: false,
  242. },
  243. };
  244. }
  245. result.push(data);
  246. });
  247. return result;
  248. },
  249. series() {
  250. let result = [];
  251. if (this.list && this.list.length > 0) {
  252. this.list.forEach((value, index) => {
  253. let seriesItem = {
  254. name: value.title,
  255. type: "bar",
  256. barWidth: "8%",
  257. animation: this.firstAnimation && this.showAnimation,
  258. yAxisIndex: value.yAxisIndex,
  259. data: value.value.map((t) => {
  260. return t.value;
  261. }),
  262. };
  263. if (this.barMaxWidth) {
  264. seriesItem.barMaxWidth = this.barMaxWidth;
  265. } else {
  266. seriesItem.barWidth = "8%";
  267. }
  268. if (this.barGap) {
  269. seriesItem.barGap = this.barGap;
  270. }
  271. result.push(seriesItem);
  272. });
  273. }
  274. return result;
  275. },
  276. },
  277. methods: {
  278. resize() {
  279. this.initChart();
  280. },
  281. initChart() {
  282. let chart = echarts.init(this.$el);
  283. let option = {
  284. color: this.color,
  285. tooltip: {
  286. trigger: "axis",
  287. backgroundColor:
  288. this.$store.state.themeName === "dark"
  289. ? "rgba(0,0,0,0.4)"
  290. : "rgba(255,255,255,0.5)",
  291. borderColor:
  292. this.$store.state.themeName === "dark"
  293. ? partten.getColor("gray")
  294. : "#000",
  295. textStyle: {
  296. color: this.$store.state.themeName === "dark" ? "#fff" : "#000",
  297. fontSize: 12,
  298. },
  299. },
  300. legend: {
  301. show: this.showLegend,
  302. data: this.legend,
  303. right: 56,
  304. icon: "ract",
  305. itemWidth: 8,
  306. itemHeight: 8,
  307. inactiveColor:
  308. this.$store.state.themeName === "dark"
  309. ? partten.getColor("gray")
  310. : "#000",
  311. textStyle: {
  312. fontSize: 12,
  313. color:
  314. this.$store.state.themeName === "dark"
  315. ? partten.getColor("grayl")
  316. : "#000",
  317. },
  318. },
  319. grid: {
  320. top: 32,
  321. left: 8,
  322. right: 8,
  323. bottom: 0,
  324. containLabel: true,
  325. },
  326. xAxis: [
  327. {
  328. type: "category",
  329. data: this.xdata,
  330. nameLocation: "center",
  331. axisPointer: {
  332. type: "shadow",
  333. },
  334. axisLabel: {
  335. interval: 0,
  336. fontSize: 12,
  337. textStyle: {
  338. color:
  339. this.$store.state.themeName === "dark"
  340. ? "rgb(116,124,128)"
  341. : "#000",
  342. },
  343. },
  344. },
  345. ],
  346. yAxis: this.ydata,
  347. series: this.series,
  348. };
  349. chart.clear();
  350. chart.setOption(option);
  351. this.resize = function () {
  352. chart.resize();
  353. };
  354. window.addEventListener("resize", this.resize);
  355. },
  356. },
  357. created() {
  358. this.$nextTick(() => {
  359. this.id = "pie-chart-" + util.newGUID();
  360. });
  361. },
  362. mounted() {
  363. this.$nextTick(() => {
  364. this.$el.style.width = this.width;
  365. this.$el.style.height = this.height;
  366. this.initChart();
  367. this.firstAnimation = false;
  368. });
  369. },
  370. updated() {
  371. this.$nextTick(() => {
  372. this.initChart();
  373. });
  374. },
  375. unmounted() {
  376. window.removeEventListener("resize", this.resize);
  377. },
  378. watch: {
  379. "$store.state.themeName"() {
  380. let myChart = echarts.init(document.getElementById(this.id));
  381. myChart.dispose();
  382. setTimeout(() => {
  383. this.initChart();
  384. }, 300);
  385. },
  386. },
  387. };
  388. </script>
  389. <style lang="less">
  390. .chart {
  391. width: 100%;
  392. height: 100%;
  393. display: inline-block;
  394. }
  395. </style>