bar-line-chart.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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: "800px",
  19. },
  20. // 传入数据
  21. bardata: {
  22. type: Object,
  23. default: () => {
  24. return {
  25. area: [
  26. "风场1",
  27. "风场2",
  28. "风场3",
  29. "风场4",
  30. "风场5",
  31. "风场6",
  32. "风场7",
  33. "风场8",
  34. "风场9",
  35. ],
  36. legend: [
  37. "实际电量",
  38. "计划检修损失",
  39. "非计划检修损失",
  40. "限电损失",
  41. "受累损失",
  42. "性能损失",
  43. ],
  44. data: [
  45. [1320, 1302, 901, 634, 1390, 1330, 1320, 1000, 500],
  46. [320, 302, 301, 334, 390, 330, 320, 100, 50],
  47. [320, 302, 301, 334, 390, 330, 320, 100, 50],
  48. [1320, 1302, 901, 634, 1390, 1330, 1320, 1000, 500],
  49. [320, 302, 301, 334, 390, 330, 320, 100, 50],
  50. [320, 302, 301, 334, 390, 330, 320, 100, 50],
  51. [1320, 1302, 901, 634, 1390, 1330, 1320, 1000, 500],
  52. [320, 302, 301, 334, 390, 330, 320, 100, 50],
  53. ],
  54. };
  55. },
  56. },
  57. lineData: {
  58. type: Array,
  59. default: () => [200, 350, 400, 500, 600, 700, 800, 900, 1200],
  60. },
  61. lineName: {
  62. type: String,
  63. default: "损失电量",
  64. },
  65. // 单位
  66. units: {
  67. type: Array,
  68. default: () => ["(万KWh)", "(风速)"],
  69. },
  70. // 显示 legend
  71. showLegend: {
  72. type: Boolean,
  73. default: true,
  74. },
  75. mxFlag: {
  76. type: Boolean,
  77. default: false,
  78. },
  79. // 颜色
  80. color: {
  81. type: Array,
  82. default: () => ["#323E6F", "#e17e23", "#ba3237", "#c531c7", "#ffffff", "#EDEB2F"],
  83. },
  84. // 每页显示个数
  85. pageSize: {
  86. type: Number,
  87. default: 23,
  88. },
  89. },
  90. data() {
  91. return {
  92. id: "",
  93. chart: null,
  94. areaData: [],
  95. };
  96. },
  97. computed: {
  98. legend() {
  99. return this.bardata.legend;
  100. },
  101. end() {
  102. var result = 20;
  103. if (this.areaData) {
  104. result = parseInt((this.pageSize / this.areaData.length) * 100);
  105. }
  106. return result;
  107. },
  108. },
  109. methods: {
  110. initChart() {
  111. let chart = echarts.init(this.$el);
  112. let option = {
  113. color: this.color,
  114. grid: {
  115. left: 40,
  116. right: 16,
  117. bottom: 16,
  118. top: 35,
  119. containLabel: true,
  120. },
  121. legend: {
  122. show: this.showLegend,
  123. data: this.bardata.legend,
  124. right: 0,
  125. icon: "ract",
  126. itemWidth: 4,
  127. itemHeight: 4,
  128. inactiveColor: '#999999',
  129. padding: [10, 0],
  130. textStyle: {
  131. color: '#999999',
  132. fontSize: 12,
  133. },
  134. },
  135. tooltip: {
  136. trigger: "axis",
  137. // backgroundColor: "rgba(0,0,0,0.4)",
  138. borderColor: '#05bb4c',
  139. backgroundColor: "rgba(5, 187, 76,0.35)",
  140. textStyle: {
  141. color: "#fff",
  142. fontSize: util.vh(16),
  143. },
  144. axisPointer: {
  145. type: 'shadow',
  146. shadowStyle: {
  147. color: 'rgba(105,105,105, .05)',
  148. width: '1'
  149. }
  150. }
  151. },
  152. yAxis: [
  153. {
  154. type: "category",
  155. axisLabel: {
  156. color: '#999999',
  157. },
  158. inverse: true,
  159. // minInterval: 10,
  160. // maxInterval: 10,
  161. axisLine: {
  162. show: false,
  163. },
  164. axisTick: {
  165. show: false,
  166. },
  167. data: this.bardata.area,
  168. },
  169. ],
  170. xAxis: [
  171. {
  172. type: "value",
  173. axisLabel: {
  174. show: false,
  175. color: '#999999',
  176. },
  177. axisLine: {
  178. type: "",
  179. lineStyle: {
  180. color: '#999999',
  181. },
  182. width: 15,
  183. },
  184. axisTick: {
  185. show: true,
  186. },
  187. splitLine: {
  188. lineStyle: {
  189. type: "dashed",
  190. dashOffset: 100,
  191. color: "#5a6162",
  192. },
  193. },
  194. },
  195. {
  196. type: "value",
  197. name: "",
  198. axisLabel: {
  199. show: false,
  200. // formatter: "{value}",
  201. // color: partten.getColor("gray"),
  202. },
  203. axisLine: {
  204. show: false,
  205. },
  206. axisTick: {
  207. show: false,
  208. },
  209. splitLine: {
  210. show: false,
  211. },
  212. },
  213. ],
  214. series: [],
  215. };
  216. if (this.bardata && this.bardata.legend)
  217. // bar data
  218. for (var i = 0; i < this.bardata.legend.length; i++) {
  219. option.series.push({
  220. name: this.bardata.legend[i],
  221. type: "bar",
  222. stack: "总量",
  223. barWidth: 10,
  224. label: {
  225. show: false,
  226. position: "insideRight",
  227. },
  228. data: this.bardata.data[i],
  229. });
  230. }
  231. // line data
  232. if (this.lineData.length > 0) {
  233. option.series.push({
  234. name: this.lineName,
  235. type: "line",
  236. data: this.lineData,
  237. smooth: false, //平滑展示
  238. xAxisIndex: 0,
  239. lineStyle: {
  240. color: '#05bb4c',
  241. },
  242. itemStyle: {
  243. color: '#05bb4c',
  244. },
  245. });
  246. }
  247. chart.setOption(option);//重新绘制图标
  248. },
  249. },
  250. created() {
  251. this.id = "pie-chart-" + util.newGUID();
  252. if (this.bardata.area && this.bardata.area.length < this.pageSize) {
  253. this.areaData = this.bardata.area;
  254. for (let i = this.bardata.area.length; i <= this.pageSize; i++) {
  255. this.areaData.push("");
  256. }
  257. }
  258. },
  259. mounted() {
  260. this.$nextTick(() => {
  261. this.$el.style.width = this.width;
  262. this.$el.style.height = this.height;
  263. this.initChart();
  264. });
  265. },
  266. updated() {
  267. this.$nextTick(() => {
  268. this.initChart();
  269. });
  270. },
  271. beforeUpdate() {
  272. this.areaData = this.bardata.area;
  273. },
  274. beforeUpdate() {
  275. this.areaData = this.bardata.area;
  276. },
  277. watch: {
  278. bardata(val) {
  279. if (val.area && val.area.length < this.pageSize) {
  280. this.areaData = val.area;
  281. for (let i = val.area.length; i <= this.pageSize; i++) {
  282. this.areaData.push("");
  283. }
  284. }
  285. },
  286. "height"() {
  287. this.areaData = this.bardata.area;
  288. this.initChart();
  289. },
  290. },
  291. };
  292. </script>
  293. <style lang="less">
  294. .chart {
  295. width: 100%;
  296. height: 100%;
  297. display: inline-block;
  298. }
  299. </style>