1
0

barCharts.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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: "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: 20,
  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: String,
  45. default: "",
  46. },
  47. // 显示 legend
  48. showLegend: {
  49. type: Boolean,
  50. default: true,
  51. },
  52. // X轴是否为时间
  53. xdate: {
  54. type: Boolean,
  55. default: true,
  56. },
  57. // 颜色#
  58. color: {
  59. type: Array,
  60. default: () => [
  61. "#005bd9",
  62. "#019f2e",
  63. "#db6200",
  64. "#a10f0f",
  65. "#850894",
  66. "#9fa0a2",
  67. ],
  68. },
  69. showAnimation: {
  70. type: Boolean,
  71. default: true,
  72. },
  73. colorIndex: {
  74. type: Boolean,
  75. default: false,
  76. },
  77. // 柱子最大宽度
  78. barMaxWidth: {
  79. type: Number || String,
  80. default: 0,
  81. },
  82. // 柱子间距
  83. barGap: {
  84. type: Number || String,
  85. default: 0,
  86. },
  87. showkey: {
  88. type: String,
  89. default: "",
  90. },
  91. showName: {
  92. type: String,
  93. default: "",
  94. },
  95. },
  96. data() {
  97. return {
  98. id: "",
  99. chart: null,
  100. firstAnimation: true,
  101. };
  102. },
  103. computed: {
  104. legend() {
  105. return this.list.map((t) => {
  106. return t.name;
  107. });
  108. },
  109. xdata() {
  110. if (this.list[0].value.length) {
  111. return this.list[0].value.map((item) => item.dateTime);
  112. } else {
  113. return [];
  114. }
  115. },
  116. series() {
  117. let result = [];
  118. if (this.showkey == "electric") {
  119. this.list.forEach((value, index) => {
  120. let seriesItem = {
  121. name: value.name,
  122. type: "bar",
  123. barWidth: "12",
  124. animation: this.firstAnimation && this.showAnimation,
  125. itemStyle: {
  126. borderColor: this.color[index],
  127. borderWidth: 1,
  128. },
  129. data: value.value,
  130. };
  131. result.push(seriesItem);
  132. });
  133. } else {
  134. result = [
  135. {
  136. name: this.list[0].name,
  137. type: "bar",
  138. barWidth: "12",
  139. itemStyle: {
  140. borderColor: this.color[0],
  141. borderWidth: 1,
  142. },
  143. data: this.list[0].value,
  144. },
  145. ];
  146. }
  147. return result;
  148. },
  149. },
  150. methods: {
  151. initChart() {
  152. let chart = echarts.init(this.$el);
  153. var units = this.units;
  154. let option = {
  155. color: this.color,
  156. zoom: 12,
  157. tooltip: {
  158. trigger: "axis",
  159. backgroundColor: "rgba(5, 187, 76,0.35)",
  160. borderColor: "#05bb4c",
  161. formatter: function (params) {
  162. var htmlStr = `<div style='margin-bottom:5px'>${params[0].axisValue}</div>`;
  163. for (var i = 0; i < params.length; i++) {
  164. htmlStr += `<div style='margin-bottom:2px'>`;
  165. var param = params[i];
  166. var seriesName = param.seriesName; //图例名称
  167. var value = param.value; //y轴值
  168. var data = param.data; //单位判断code
  169. var mark = param.marker; //点
  170. var unit = `<span style='font-size:14px'>`;
  171. htmlStr += mark; //一个点
  172. htmlStr += `${seriesName} : ${
  173. value != null ? value + unit + ` ${units}</span>` : "--"
  174. }`; //圆点后面显示的文本
  175. htmlStr += "</div>";
  176. }
  177. return htmlStr;
  178. },
  179. padding: [10, 10, 3, 10],
  180. textStyle: {
  181. color: "#fff",
  182. fontSize: 16,
  183. fontFamily: "楷体",
  184. fontWeight: "normal",
  185. },
  186. axisPointer: {
  187. type: "shadow",
  188. shadowStyle: {
  189. color: "rgba(105,105,105, .05)",
  190. width: "1",
  191. },
  192. },
  193. },
  194. legend: {
  195. show: this.showLegend,
  196. data: this.legend,
  197. padding: this.padding,
  198. right: 56,
  199. icon: "ract",
  200. itemWidth: 8,
  201. itemHeight: 8,
  202. inactiveColor: partten.getColor("gray"),
  203. textStyle: {
  204. fontSize: 12,
  205. color: partten.getColor("grayl"),
  206. },
  207. },
  208. grid: {
  209. left: 30,
  210. right: 40,
  211. top: 30,
  212. bottom: 30,
  213. containLabel: true,
  214. },
  215. xAxis: [
  216. {
  217. type: "category",
  218. data: this.xdata,
  219. axisLabel: {
  220. interval: this.interval,
  221. fontSize: 12,
  222. textStyle: {
  223. color: "rgb(116,124,128)",
  224. },
  225. },
  226. },
  227. ],
  228. yAxis: {
  229. type: "value",
  230. name: this.units,
  231. //分格线
  232. splitLine: {
  233. show: false,
  234. },
  235. },
  236. series: this.series,
  237. };
  238. chart.setOption(option);
  239. },
  240. },
  241. created() {
  242. this.$nextTick(() => {
  243. this.id = "pie-chart-" + util.newGUID();
  244. });
  245. },
  246. mounted() {
  247. this.$nextTick(() => {
  248. // this.$el.style.width = this.width;
  249. // this.$el.style.height = this.height;
  250. this.initChart();
  251. this.firstAnimation = false;
  252. });
  253. },
  254. watch: {
  255. "$store.state.themeName"() {
  256. this.initChart();
  257. },
  258. list: {
  259. handler() {
  260. this.initChart();
  261. },
  262. },
  263. },
  264. };
  265. </script>
  266. <style lang="less">
  267. .chart {
  268. // width: 100%;
  269. // height: 100%;
  270. // display: inline-block;
  271. // margin-top: 5px;
  272. }
  273. </style>