bar-line-chart.vue 8.0 KB

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