barCharts.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <template>
  2. <div class="chart" :id="id" :style="{ width, height }"></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: "multiple-bar-chart",
  11. componentName: "multiple-bar-chart",
  12. props: {
  13. width: {
  14. type: String,
  15. default: "100%",
  16. },
  17. height: {
  18. type: String,
  19. default: "13.889vh",
  20. },
  21. pillarName: {
  22. type: String,
  23. default: "",
  24. },
  25. top: {
  26. type: Number,
  27. default: 30,
  28. },
  29. interval: {
  30. type: Number,
  31. default: 0,
  32. },
  33. padding: {
  34. type: Array,
  35. default: () => [10, 10],
  36. },
  37. // 传入数据
  38. list: {
  39. type: Array,
  40. default: () => [],
  41. },
  42. // 单位
  43. units: {
  44. type: Array,
  45. default: () => ["(万KWh)", ""],
  46. },
  47. units2: {
  48. type: String,
  49. default: "",
  50. },
  51. // 显示 legend
  52. showLegend: {
  53. type: Boolean,
  54. default: true,
  55. },
  56. // X轴是否为时间
  57. xdate: {
  58. type: Boolean,
  59. default: true,
  60. },
  61. // 颜色#
  62. color: {
  63. type: Array,
  64. default: () => ["#2999a0", "#67b9ff", "#ff6271", "#05b2fa", "#0ef167"],
  65. },
  66. showAnimation: {
  67. type: Boolean,
  68. default: true,
  69. },
  70. colorIndex: {
  71. type: Boolean,
  72. default: false,
  73. },
  74. // 柱子最大宽度
  75. barMaxWidth: {
  76. type: Number || String,
  77. default: 0,
  78. },
  79. // 柱子间距
  80. barGap: {
  81. type: Number || String,
  82. default: 0,
  83. },
  84. dataInterval: {
  85. type: String,
  86. default: "D",
  87. },
  88. },
  89. data() {
  90. return {
  91. id: "",
  92. chart: null,
  93. firstAnimation: true,
  94. };
  95. },
  96. computed: {
  97. legend() {
  98. return this.list.map((t) => {
  99. return t.name;
  100. });
  101. },
  102. xdata() {
  103. let result = [];
  104. if (this.list && this.list.length > 0) {
  105. result = this.list[0].date.map((t) => {
  106. if (this.dataInterval === "M") {
  107. return dayjs(t).format("YYYY-MM");
  108. } else if (this.dataInterval === "Y") {
  109. return dayjs(t).format("YYYY");
  110. } else {
  111. return this.xdate ? dayjs(t).format("MM-DD") : t;
  112. }
  113. });
  114. }
  115. return result;
  116. },
  117. ydata() {
  118. let result = [];
  119. this.units.forEach((value, index) => {
  120. let data = null;
  121. if (index == 0) {
  122. data = {
  123. type: "value",
  124. name: value,
  125. axisLabel: {
  126. formatter: "{value} ",
  127. fontSize: 12,
  128. textStyle: {
  129. // color: "#828484",
  130. color: "#f8f8ff",
  131. },
  132. },
  133. //分格线
  134. splitLine: {
  135. lineStyle: {
  136. color: "rgba(96,103,105,0.3)",
  137. type: "dashed",
  138. },
  139. },
  140. };
  141. } else {
  142. data = {
  143. type: "value",
  144. name: value,
  145. axisLabel: {
  146. formatter: "{value}",
  147. fontSize: 12,
  148. textStyle: {
  149. color: "#828484",
  150. },
  151. },
  152. //分格线
  153. splitLine: {
  154. show: false,
  155. },
  156. };
  157. }
  158. result.push(data);
  159. });
  160. return result;
  161. },
  162. series() {
  163. let result = [];
  164. if (this.list && this.list.length > 0) {
  165. this.list.forEach((value, index) => {
  166. let seriesItem = {
  167. name: value.name,
  168. type: "bar",
  169. barWidth: "12",
  170. animation: this.firstAnimation && this.showAnimation,
  171. yAxisIndex: value.yAxisIndex,
  172. itemStyle: {
  173. borderColor: this.color[index],
  174. borderWidth: 1,
  175. // shadowBlur: 1,
  176. // shadowColor: "#16ADD4",
  177. },
  178. data: value.children,
  179. };
  180. result.push(seriesItem);
  181. });
  182. }
  183. return result;
  184. },
  185. },
  186. methods: {
  187. resize() {
  188. this.initChart();
  189. },
  190. initChart() {
  191. let chart = echarts.init(this.$el);
  192. let that = this;
  193. let option = {
  194. color: this.color,
  195. title: {
  196. text: this.pillarName,
  197. textStyle: {
  198. // color: "#999999",
  199. color: "#f8f8ff",
  200. fontSize: 18,
  201. },
  202. },
  203. zoom: 12,
  204. tooltip: {
  205. trigger: "axis",
  206. backgroundColor: "rgba(#2169c3, 0.35)",
  207. borderWidth: 1,
  208. padding: [10, 10, 3, 10],
  209. borderColor: "#2169c3",
  210. textStyle: {
  211. color: "#fff",
  212. fontSize: 12,
  213. },
  214. axisPointer: {
  215. type: "shadow",
  216. shadowStyle: {
  217. color: "rgba(105,105,105, .05)",
  218. width: "1",
  219. },
  220. },
  221. formatter: function (params) {
  222. var htmlStr = `<div style='margin-bottom:5px'>${params[0].axisValue}</div>`;
  223. var unit = that.units2;
  224. for (var i = 0; i < params.length; i++) {
  225. htmlStr += `<div style='margin-bottom:2px'>`;
  226. var param = params[i];
  227. var seriesName = param.seriesName; //图例名称
  228. var value = param.value; //y轴值
  229. var mark = param.marker; //点
  230. var unit = `<span style='font-size:12px'>${unit}</span>`;
  231. htmlStr += mark; //一个点
  232. htmlStr += `${seriesName} : ${
  233. value != null ? value + unit : "--"
  234. }`; //圆点后面显示的文本
  235. htmlStr += "</div>";
  236. }
  237. return htmlStr;
  238. },
  239. },
  240. legend: {
  241. show: this.showLegend,
  242. data: this.legend,
  243. padding: this.padding,
  244. right: 56,
  245. icon: "ract",
  246. itemWidth: 8,
  247. itemHeight: 8,
  248. inactiveColor: partten.getColor("gray"),
  249. textStyle: {
  250. fontSize: 12,
  251. // color: partten.getColor("grayl"),
  252. color: "#f8f8ff",
  253. },
  254. },
  255. grid: {
  256. top: 40,
  257. left: 10,
  258. right: 15,
  259. bottom: 10,
  260. containLabel: true,
  261. },
  262. xAxis: [
  263. {
  264. type: "category",
  265. data: this.xdata,
  266. nameLocation: "center",
  267. axisPointer: {
  268. type: "shadow",
  269. },
  270. axisLabel: {
  271. interval: this.interval,
  272. fontSize: 12,
  273. textStyle: {
  274. // color: "rgb(116,124,128)",
  275. color: "f8f8ff",
  276. },
  277. },
  278. },
  279. ],
  280. yAxis: this.ydata,
  281. series: this.series,
  282. };
  283. chart.clear();
  284. chart.setOption(option);
  285. this.resize = function () {
  286. chart.resize();
  287. };
  288. window.addEventListener("resize", this.resize);
  289. },
  290. },
  291. created() {
  292. this.$nextTick(() => {
  293. this.id = "pie-chart-" + util.newGUID();
  294. });
  295. },
  296. mounted() {
  297. this.$nextTick(() => {
  298. // this.$el.style.width = this.width;
  299. // this.$el.style.height = this.height;
  300. this.initChart();
  301. this.firstAnimation = false;
  302. });
  303. },
  304. updated() {
  305. this.$nextTick(() => {
  306. this.initChart();
  307. });
  308. },
  309. unmounted() {
  310. window.removeEventListener("resize", this.resize);
  311. },
  312. watch: {
  313. "$store.state.themeName"() {
  314. this.initChart();
  315. },
  316. },
  317. };
  318. </script>
  319. <style lang="less">
  320. // .chart {
  321. // width: 100%;
  322. // height: 100%;
  323. // display: inline-block;
  324. // }
  325. </style>