multiple-bar-chart.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. "#ba3237",
  154. "#e17e23",
  155. "#4b55ae",
  156. "#c531c7",
  157. "#ccf0d3",
  158. ],
  159. },
  160. showAnimation: {
  161. type: Boolean,
  162. default: true,
  163. },
  164. // 柱子最大宽度
  165. barMaxWidth: {
  166. type: Number || String,
  167. default: 0,
  168. },
  169. // 柱子间距
  170. barGap: {
  171. type: Number || String,
  172. default: 0,
  173. },
  174. },
  175. data() {
  176. return {
  177. id: "",
  178. chart: null,
  179. firstAnimation: true,
  180. };
  181. },
  182. computed: {
  183. legend() {
  184. return this.list.map((t) => {
  185. return t.title;
  186. });
  187. },
  188. xdata() {
  189. let result = [];
  190. if (this.list && this.list.length > 0 && this.list[0].value.length > 0) {
  191. result = this.list[0].value.map((t) => {
  192. return t.text;
  193. });
  194. }
  195. return result;
  196. },
  197. ydata() {
  198. let result = [];
  199. this.units.forEach((value, index) => {
  200. let data = null;
  201. if (index == 0) {
  202. data = {
  203. type: "value",
  204. name: value,
  205. axisLabel: {
  206. formatter: "{value} ",
  207. fontSize: 12,
  208. textStyle: {
  209. color:
  210. this.$store.state.themeName === "dark"
  211. ? "rgb(116,124,128)"
  212. : "#000",
  213. },
  214. },
  215. //分格线
  216. splitLine: {
  217. lineStyle: {
  218. color:
  219. this.$store.state.themeName === "dark" ? "#5a6162" : "#000",
  220. type: "dashed",
  221. },
  222. },
  223. };
  224. } else {
  225. data = {
  226. type: "value",
  227. name: value,
  228. axisLabel: {
  229. formatter: "{value}",
  230. fontSize: 12,
  231. textStyle: {
  232. color:
  233. this.$store.state.themeName === "dark"
  234. ? "rgb(116,124,128)"
  235. : "#000",
  236. },
  237. },
  238. //分格线
  239. splitLine: {
  240. show: false,
  241. },
  242. };
  243. }
  244. result.push(data);
  245. });
  246. return result;
  247. },
  248. series() {
  249. let result = [];
  250. if (this.list && this.list.length > 0) {
  251. this.list.forEach((value, index) => {
  252. let seriesItem = {
  253. name: value.title,
  254. type: "bar",
  255. barWidth: "8%",
  256. animation: this.firstAnimation && this.showAnimation,
  257. yAxisIndex: value.yAxisIndex,
  258. data: value.value.map((t) => {
  259. return t.value;
  260. }),
  261. };
  262. if (this.barMaxWidth) {
  263. seriesItem.barMaxWidth = this.barMaxWidth;
  264. } else {
  265. seriesItem.barWidth = "8%";
  266. }
  267. if (this.barGap) {
  268. seriesItem.barGap = this.barGap;
  269. }
  270. result.push(seriesItem);
  271. });
  272. }
  273. return result;
  274. },
  275. },
  276. methods: {
  277. resize() {
  278. this.initChart();
  279. },
  280. initChart() {
  281. let chart = echarts.init(this.$el);
  282. let option = {
  283. color: this.color,
  284. tooltip: {
  285. trigger: "axis",
  286. backgroundColor:
  287. this.$store.state.themeName === "dark"
  288. ? "rgba(0,0,0,0.4)"
  289. : "rgba(255,255,255,0.5)",
  290. borderColor:
  291. this.$store.state.themeName === "dark"
  292. ? partten.getColor("gray")
  293. : "#000",
  294. textStyle: {
  295. color: this.$store.state.themeName === "dark" ? "#fff" : "#000",
  296. fontSize: 12,
  297. },
  298. },
  299. legend: {
  300. show: this.showLegend,
  301. data: this.legend,
  302. right: 56,
  303. icon: "ract",
  304. itemWidth: 8,
  305. itemHeight: 8,
  306. inactiveColor:
  307. this.$store.state.themeName === "dark"
  308. ? partten.getColor("gray")
  309. : "#000",
  310. textStyle: {
  311. fontSize: 12,
  312. color:
  313. this.$store.state.themeName === "dark"
  314. ? partten.getColor("grayl")
  315. : "#000",
  316. },
  317. },
  318. grid: {
  319. top: 32,
  320. left: 8,
  321. right: 8,
  322. bottom: 0,
  323. containLabel: true,
  324. },
  325. xAxis: [
  326. {
  327. type: "category",
  328. data: this.xdata,
  329. nameLocation: "center",
  330. axisPointer: {
  331. type: "shadow",
  332. },
  333. axisLabel: {
  334. interval: 0,
  335. fontSize: 12,
  336. textStyle: {
  337. color:
  338. this.$store.state.themeName === "dark"
  339. ? "rgb(116,124,128)"
  340. : "#000",
  341. },
  342. },
  343. },
  344. ],
  345. yAxis: this.ydata,
  346. series: this.series,
  347. };
  348. chart.clear();
  349. chart.setOption(option);
  350. this.resize = function () {
  351. chart.resize();
  352. };
  353. window.addEventListener("resize", this.resize);
  354. },
  355. },
  356. created() {
  357. this.$nextTick(() => {
  358. this.id = "pie-chart-" + util.newGUID();
  359. });
  360. },
  361. mounted() {
  362. this.$nextTick(() => {
  363. this.$el.style.width = this.width;
  364. this.$el.style.height = this.height;
  365. this.initChart();
  366. this.firstAnimation = false;
  367. });
  368. },
  369. updated() {
  370. this.$nextTick(() => {
  371. this.initChart();
  372. });
  373. },
  374. unmounted() {
  375. window.removeEventListener("resize", this.resize);
  376. },
  377. watch: {
  378. "$store.state.themeName"() {
  379. this.initChart();
  380. },
  381. },
  382. };
  383. </script>
  384. <style lang="less">
  385. .chart {
  386. width: 100%;
  387. height: 100%;
  388. display: inline-block;
  389. }
  390. </style>