barCharts.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. import dayjs from "dayjs";
  9. export default {
  10. name: "yfdl-multiple-bar-chart",
  11. componentName: "yfdl-multiple-bar-chart",
  12. props: {
  13. width: {
  14. type: String,
  15. default: "100%",
  16. },
  17. height: {
  18. type: String,
  19. default: "100%",
  20. },
  21. // 传入数据
  22. list: {
  23. type: Array,
  24. default: () => [],
  25. },
  26. },
  27. data() {
  28. return {
  29. id: "",
  30. chart: null,
  31. firstAnimation: true,
  32. };
  33. },
  34. computed: {
  35. xdata() {
  36. return [
  37. "一月",
  38. "二月",
  39. "三月",
  40. "四月",
  41. "五月",
  42. "六月",
  43. "七月",
  44. "八月",
  45. "九月",
  46. "十月",
  47. "十一月",
  48. "十二月",
  49. ];
  50. },
  51. series() {
  52. let result = [];
  53. if (this.list && this.list.length > 0) {
  54. this.list.forEach((listItem, index) => {
  55. let seriesItem = {
  56. name: listItem?.name,
  57. type: "bar",
  58. data: listItem?.data,
  59. itemStyle: {
  60. color: listItem.color,
  61. },
  62. barWidth: "20%",
  63. };
  64. result.push(seriesItem);
  65. });
  66. }
  67. return result;
  68. },
  69. },
  70. methods: {
  71. resize() {
  72. this.initChart();
  73. },
  74. initChart() {
  75. let chart = echarts.init(this.$el);
  76. let option = {
  77. tooltip: {
  78. trigger: "axis",
  79. backgroundColor: "rgba(5, 187, 76,0.35)",
  80. borderColor: "#05bb4c",
  81. formatter: function (params) {
  82. var htmlStr = `<div style='margin-bottom:5px'>${params[0].axisValue}</div>`;
  83. for (var i = 0; i < params.length; i++) {
  84. htmlStr += `<div style='margin-bottom:2px'>`;
  85. var param = params[i];
  86. var seriesName = param.seriesName; //图例名称
  87. var value = param.value; //y轴值
  88. var data = param.data; //单位判断code
  89. var mark = param.marker; //点
  90. var unit = `<span style='font-size:14px'>`;
  91. htmlStr += mark; //一个点
  92. htmlStr += `${seriesName} : ${
  93. value != null ? value + unit + ` 万kWh</span>` : "--"
  94. }`; //圆点后面显示的文本
  95. htmlStr += "</div>";
  96. }
  97. return htmlStr;
  98. },
  99. padding: [10, 10, 3, 10],
  100. textStyle: {
  101. color: "#fff",
  102. fontSize: 16,
  103. },
  104. axisPointer: {
  105. type: "shadow",
  106. shadowStyle: {
  107. color: "rgba(105,105,105, .05)",
  108. width: "1",
  109. },
  110. },
  111. },
  112. legend: {
  113. show: true,
  114. data: this.list.map((i) => i?.name),
  115. right: 56,
  116. icon: "ract",
  117. itemWidth: 8,
  118. itemHeight: 8,
  119. inactiveColor: partten.getColor("gray"),
  120. textStyle: {
  121. fontSize: 12,
  122. color: partten.getColor("grayl"),
  123. },
  124. },
  125. grid: {
  126. top: "10%",
  127. left: "1%",
  128. right: "2%",
  129. bottom: "5%",
  130. containLabel: true,
  131. },
  132. xAxis: [
  133. {
  134. type: "category",
  135. data: this.xdata,
  136. nameLocation: "center",
  137. axisPointer: {
  138. type: "shadow",
  139. },
  140. axisLine: {
  141. lineStyle: {
  142. color: "#93989A",
  143. },
  144. },
  145. axisLabel: {
  146. textStyle: {
  147. fontSize: 14,
  148. color: "#93989A",
  149. },
  150. },
  151. },
  152. ],
  153. yAxis: {
  154. type: "value",
  155. name: "单位:万千瓦时",
  156. nameTextStyle: {
  157. color: "#93989A",
  158. },
  159. splitLine: {
  160. show: true,
  161. lineStyle: {
  162. color: "rgba(96,103,105,0.3)",
  163. type: "dashed",
  164. },
  165. },
  166. axisLabel: {
  167. textStyle: {
  168. fontSize: 14,
  169. color: "#93989A",
  170. },
  171. },
  172. },
  173. series: this.series,
  174. };
  175. chart.clear();
  176. chart.setOption(option);
  177. this.resize = function () {
  178. chart.resize();
  179. };
  180. window.addEventListener("resize", this.resize);
  181. },
  182. },
  183. created() {
  184. this.$nextTick(() => {
  185. this.id = "pie-chart-" + util.newGUID();
  186. });
  187. },
  188. mounted() {
  189. this.$nextTick(() => {
  190. this.$el.style.width = this.width;
  191. this.$el.style.height = this.height;
  192. this.initChart();
  193. this.firstAnimation = false;
  194. });
  195. },
  196. updated() {
  197. this.$nextTick(() => {
  198. this.initChart();
  199. });
  200. },
  201. unmounted() {
  202. window.removeEventListener("resize", this.resize);
  203. },
  204. watch: {
  205. "$store.state.themeName"() {
  206. this.initChart();
  207. },
  208. },
  209. };
  210. </script>
  211. <style lang="less">
  212. .chart {
  213. width: 100%;
  214. height: 100%;
  215. display: inline-block;
  216. }
  217. </style>