multiple-bar-chart.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. },
  144. data() {
  145. return {
  146. id: "",
  147. chart: null,
  148. color: ["#05bb4c", "#4b55ae", "#fa8c16", "#f8de5b"],
  149. };
  150. },
  151. computed: {
  152. xdata() {
  153. let result = [];
  154. if (this.list && this.list.length > 0 && this.list[0].value.length > 0) {
  155. result = this.list[0].value.map((t) => {
  156. return t.text;
  157. });
  158. }
  159. return result;
  160. },
  161. ydata() {
  162. let result = [];
  163. this.units.forEach((value, index) => {
  164. let data = null;
  165. if (index == 0) {
  166. data = {
  167. type: "value",
  168. name: value,
  169. axisLabel: {
  170. formatter: "{value} ",
  171. fontSize: util.vh(14),
  172. },
  173. //分格线
  174. splitLine: {
  175. lineStyle: {
  176. color: "#5a6162",
  177. type: "dashed",
  178. },
  179. },
  180. };
  181. } else {
  182. data = {
  183. type: "value",
  184. name: value,
  185. axisLabel: {
  186. formatter: "{value}",
  187. fontSize: util.vh(14),
  188. },
  189. //分格线
  190. splitLine: {
  191. show: false,
  192. },
  193. };
  194. }
  195. result.push(data);
  196. });
  197. return result;
  198. },
  199. series() {
  200. let result = [];
  201. if (this.list && this.list.length > 0) {
  202. this.list.forEach((value, index) => {
  203. result.push({
  204. name: value.title,
  205. type: "bar",
  206. yAxisIndex: value.yAxisIndex,
  207. data: value.value.map((t) => {
  208. return t.value;
  209. }),
  210. });
  211. });
  212. }
  213. return result;
  214. },
  215. },
  216. methods: {
  217. initChart() {
  218. let option = {
  219. color: this.color,
  220. tooltip: {
  221. trigger: "item",
  222. backgroundColor: "rgba(0,0,0,0.3)",
  223. textStyle: {
  224. color: "#fff",
  225. fontSize: util.vh(16),
  226. },
  227. formatter: function(param) {
  228. return param.name + "<br >" + param.marker + param.seriesName + ":" + param.value;
  229. },
  230. },
  231. grid: {
  232. top: util.vh(32),
  233. left: util.vh(40),
  234. right: this.ydata.length > 1 ? util.vh(40) : util.vh(16),
  235. bottom: util.vh(24),
  236. },
  237. xAxis: [
  238. {
  239. type: "category",
  240. data: this.xdata,
  241. axisPointer: {
  242. type: "shadow",
  243. },
  244. axisLabel: {
  245. fontSize: util.vh(14),
  246. },
  247. },
  248. ],
  249. yAxis: this.ydata,
  250. series: this.series,
  251. };
  252. this.chart.setOption(option);
  253. },
  254. },
  255. created() {
  256. this.$nextTick(() => {
  257. this.id = "pie-chart-" + util.newGUID();
  258. });
  259. },
  260. mounted() {
  261. this.$nextTick(() => {
  262. this.$el.style.width = this.width;
  263. this.$el.style.height = this.height;
  264. this.chart = echarts.init(this.$el);
  265. this.initChart();
  266. });
  267. },
  268. updated() {
  269. this.$nextTick(() => {
  270. this.initChart();
  271. });
  272. },
  273. };
  274. </script>
  275. <style lang="less">
  276. .chart {
  277. width: 100%;
  278. height: 100%;
  279. display: inline-block;
  280. }
  281. </style>