multiple-bar-chart.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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: () => ["#05bb4c", "#4b55ae", "#fa8c16", "#f8de5b", "#1a93cf", "#c531c7", "#bd3338"],
  152. },
  153. showAnimation: {
  154. type: Boolean,
  155. default: true,
  156. },
  157. },
  158. data() {
  159. return {
  160. id: "",
  161. chart: null,
  162. firstAnimation: true,
  163. };
  164. },
  165. computed: {
  166. legend() {
  167. return this.list.map((t) => {
  168. return t.title;
  169. });
  170. },
  171. xdata() {
  172. let result = [];
  173. if (this.list && this.list.length > 0 && this.list[0].value.length > 0) {
  174. result = this.list[0].value.map((t) => {
  175. return t.text;
  176. });
  177. }
  178. return result;
  179. },
  180. ydata() {
  181. let result = [];
  182. this.units.forEach((value, index) => {
  183. let data = null;
  184. if (index == 0) {
  185. data = {
  186. type: "value",
  187. name: value,
  188. axisLabel: {
  189. formatter: "{value} ",
  190. fontSize: 12,
  191. },
  192. //分格线
  193. splitLine: {
  194. lineStyle: {
  195. color: "#5a6162",
  196. type: "dashed",
  197. },
  198. },
  199. };
  200. } else {
  201. data = {
  202. type: "value",
  203. name: value,
  204. axisLabel: {
  205. formatter: "{value}",
  206. fontSize: 12,
  207. },
  208. //分格线
  209. splitLine: {
  210. show: false,
  211. },
  212. };
  213. }
  214. result.push(data);
  215. });
  216. return result;
  217. },
  218. series() {
  219. let result = [];
  220. if (this.list && this.list.length > 0) {
  221. this.list.forEach((value, index) => {
  222. result.push({
  223. name: value.title,
  224. type: "bar",
  225. barWidth: "8%",
  226. // barWidth: "10%",
  227. animation: this.firstAnimation && this.showAnimation,
  228. yAxisIndex: value.yAxisIndex,
  229. data: value.value.map((t) => {
  230. return t.value;
  231. }),
  232. });
  233. });
  234. }
  235. return result;
  236. },
  237. },
  238. methods: {
  239. resize() {},
  240. initChart() {
  241. let chart = echarts.init(this.$el);
  242. let option = {
  243. color: this.color,
  244. tooltip: {
  245. trigger: "axis",
  246. backgroundColor: "rgba(0,0,0,0.4)",
  247. borderColor: partten.getColor("gray"),
  248. textStyle: {
  249. color: "#fff",
  250. fontSize: 12,
  251. },
  252. },
  253. legend: {
  254. show: this.showLegend,
  255. data: this.legend,
  256. right: 56,
  257. icon: "ract",
  258. itemWidth: 8,
  259. itemHeight: 8,
  260. inactiveColor: partten.getColor("gray"),
  261. textStyle: {
  262. color: partten.getColor("grayl"),
  263. fontSize: 12,
  264. },
  265. },
  266. grid: {
  267. top: 32,
  268. left: 8,
  269. right: 8,
  270. bottom: 0,
  271. containLabel: true,
  272. },
  273. xAxis: [
  274. {
  275. type: "category",
  276. data: this.xdata,
  277. nameLocation: "center",
  278. axisPointer: {
  279. type: "shadow",
  280. },
  281. axisLabel: {
  282. interval: 0,
  283. fontSize: 12,
  284. },
  285. },
  286. ],
  287. yAxis: this.ydata,
  288. series: this.series,
  289. };
  290. chart.clear();
  291. chart.setOption(option);
  292. this.resize = function() {
  293. chart.resize();
  294. };
  295. window.addEventListener("resize", this.resize);
  296. },
  297. },
  298. created() {
  299. this.$nextTick(() => {
  300. this.id = "pie-chart-" + util.newGUID();
  301. });
  302. },
  303. mounted() {
  304. this.$nextTick(() => {
  305. this.$el.style.width = this.width;
  306. this.$el.style.height = this.height;
  307. this.initChart();
  308. this.firstAnimation = false;
  309. });
  310. },
  311. updated() {
  312. this.$nextTick(() => {
  313. this.initChart();
  314. });
  315. },
  316. unmounted() {
  317. window.removeEventListener("resize", this.resize);
  318. },
  319. };
  320. </script>
  321. <style lang="less">
  322. .chart {
  323. width: 100%;
  324. height: 100%;
  325. display: inline-block;
  326. }
  327. </style>