bar-line-chart.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. // 颜色
  76. color: {
  77. type: Array,
  78. default: () => ["#323E6F", "#e17e23", "#ba3237#", "c531c7", "#ffffff", "#EDEB2F"],
  79. },
  80. // 每页显示个数
  81. pageSize: {
  82. type: Number,
  83. default: 20,
  84. },
  85. },
  86. data() {
  87. return {
  88. id: "",
  89. chart: null,
  90. areaData: [],
  91. };
  92. },
  93. computed: {
  94. legend() {
  95. return this.bardata.legend;
  96. },
  97. end() {
  98. var result = 20;
  99. if (this.areaData) {
  100. result = parseInt((this.pageSize / this.areaData.length) * 100);
  101. }
  102. return result;
  103. },
  104. },
  105. methods: {
  106. initChart() {
  107. let chart = echarts.init(this.$el);
  108. let option = {
  109. color: this.color,
  110. grid: {
  111. left: 40,
  112. right: 16,
  113. bottom: 16,
  114. top: 16,
  115. containLabel: true,
  116. },
  117. legend: {
  118. show: this.showLegend,
  119. data: this.bardata.legend,
  120. right: 56,
  121. icon: "ract",
  122. itemWidth: 8,
  123. itemHeight: 8,
  124. inactiveColor: partten.getColor("gray"),
  125. textStyle: {
  126. color: partten.getColor("grayl"),
  127. fontSize: 12,
  128. },
  129. },
  130. tooltip: {
  131. trigger: "axis",
  132. backgroundColor: "rgba(0,0,0,0.4)",
  133. borderColor: partten.getColor("gray"),
  134. textStyle: {
  135. color: "#fff",
  136. fontSize: util.vh(16),
  137. },
  138. },
  139. dataZoom: [
  140. {
  141. type: "inside",
  142. start: 0,
  143. end: this.end,
  144. yAxisIndex: [0],
  145. },
  146. {
  147. start: 0,
  148. end: this.end,
  149. bottom: 40,
  150. yAxisIndex: [0],
  151. backgroundColor: "transparent",
  152. // handleIcon: "path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z",
  153. handleStyle: {
  154. color: partten.getColor("green"),
  155. },
  156. moveHandleSize: 0,
  157. // dataBackground: {
  158. // lineStyle: {
  159. // color: partten.getColor("gray"),
  160. // },
  161. // areaStyle: {
  162. // color: partten.getColor("gray"),
  163. // },
  164. // },
  165. // selectedDataBackground: {
  166. // lineStyle: {
  167. // color: partten.getColor("yellow"),
  168. // },
  169. // areaStyle: {
  170. // color: partten.getColor("yellow"),
  171. // },
  172. // },
  173. fillerColor: "transparent",
  174. textStyle: {
  175. color: partten.getColor("grayl"),
  176. },
  177. borderColor: partten.getColor("gray"),
  178. brushSelect: false,
  179. },
  180. ],
  181. yAxis: [
  182. {
  183. type: "category",
  184. axisLabel: {
  185. color: partten.getColor("gray"),
  186. },
  187. inverse: true,
  188. // minInterval: 10,
  189. // maxInterval: 10,
  190. axisLine: {
  191. show: false,
  192. },
  193. axisTick: {
  194. show: false,
  195. },
  196. data: this.areaData,
  197. },
  198. ],
  199. xAxis: [
  200. {
  201. type: "value",
  202. axisLabel: {
  203. color: partten.getColor("gray"),
  204. },
  205. axisLine: {
  206. type: "dashed",
  207. lineStyle: {
  208. color: partten.getColor("gray"),
  209. },
  210. width: 5,
  211. },
  212. axisTick: {
  213. show: false,
  214. },
  215. splitLine: {
  216. lineStyle: {
  217. type: "dashed",
  218. dashOffset: 10,
  219. color: partten.getColor("gray") + 80,
  220. },
  221. },
  222. },
  223. {
  224. type: "value",
  225. name: "",
  226. axisLabel: {
  227. show: false,
  228. // formatter: "{value}",
  229. // color: partten.getColor("gray"),
  230. },
  231. axisLine: {
  232. show: false,
  233. },
  234. axisTick: {
  235. show: false,
  236. },
  237. splitLine: {
  238. show: false,
  239. },
  240. },
  241. ],
  242. series: [],
  243. };
  244. if (this.bardata && this.bardata.legend)
  245. // bar data
  246. for (var i = 0; i < this.bardata.legend.length; i++) {
  247. option.series.push({
  248. name: this.bardata.legend[i],
  249. type: "bar",
  250. stack: "总量",
  251. barWidth: 16,
  252. label: {
  253. show: false,
  254. position: "insideRight",
  255. },
  256. data: this.bardata.data[i],
  257. });
  258. }
  259. // line data
  260. if (this.lineData.length > 0) {
  261. option.series.push({
  262. name: this.lineName,
  263. type: "line",
  264. data: this.lineData,
  265. smooth: false, //平滑展示
  266. xAxisIndex: 1,
  267. lineStyle: {
  268. color: partten.getColor("green"),
  269. },
  270. itemStyle: {
  271. color: partten.getColor("green"),
  272. },
  273. });
  274. }
  275. chart.setOption(option);
  276. },
  277. },
  278. created() {
  279. this.id = "pie-chart-" + util.newGUID();
  280. if (this.bardata.area && this.bardata.area.length < this.pageSize) {
  281. this.areaData = this.bardata.area;
  282. for (let i = this.bardata.area.length; i <= this.pageSize; i++) {
  283. this.areaData.push("");
  284. }
  285. }
  286. },
  287. mounted() {
  288. this.$nextTick(() => {
  289. this.$el.style.width = this.width;
  290. this.$el.style.height = this.height;
  291. this.initChart();
  292. });
  293. },
  294. updated() {
  295. this.$nextTick(() => {
  296. this.initChart();
  297. });
  298. },
  299. beforeUpdate(){
  300. this.areaData = this.bardata.area;
  301. },
  302. watch: {
  303. bardata(val) {
  304. if (val.area && val.area.length < this.pageSize) {
  305. this.areaData = val.area;
  306. for (let i = val.area.length; i <= this.pageSize; i++) {
  307. this.areaData.push("");
  308. }
  309. }
  310. },
  311. },
  312. };
  313. </script>
  314. <style lang="less">
  315. .chart {
  316. width: 100%;
  317. height: 100%;
  318. display: inline-block;
  319. }
  320. </style>