multiple-bar-chart.vue 8.7 KB

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