barLineChart.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. ],
  45. data: [
  46. [1320, 1302, 901, 634, 1390, 1330, 1320, 1000, 500],
  47. [320, 302, 301, 334, 390, 330, 320, 100, 50],
  48. [320, 302, 301, 334, 390, 330, 320, 100, 50],
  49. [1320, 1302, 901, 634, 1390, 1330, 1320, 1000, 500],
  50. [320, 302, 301, 334, 390, 330, 320, 100, 50],
  51. [320, 302, 301, 334, 390, 330, 320, 100, 50],
  52. [1320, 1302, 901, 634, 1390, 1330, 1320, 1000, 500],
  53. [320, 302, 301, 334, 390, 330, 320, 100, 50],
  54. ],
  55. };
  56. },
  57. },
  58. lineData: {
  59. type: Array,
  60. default: () => [200, 350, 400, 500, 600, 700, 800, 900, 1200],
  61. },
  62. lineName: {
  63. type: String,
  64. default: "损失电量",
  65. },
  66. // 单位
  67. units: {
  68. type: Array,
  69. default: () => ["(万KWh)", "(风速)"],
  70. },
  71. // 显示 legend
  72. showLegend: {
  73. type: Boolean,
  74. default: true,
  75. },
  76. // 颜色
  77. color: {
  78. type: Array,
  79. default: () => [
  80. "#323E6F",
  81. "#e17e23",
  82. "#ba3237",
  83. "#c531c7",
  84. "#ffffff",
  85. "#EDEB2F",
  86. ],
  87. },
  88. // 每页显示个数
  89. pageSize: {
  90. type: Number,
  91. default: 20,
  92. },
  93. },
  94. data() {
  95. return {
  96. id: "",
  97. chart: null,
  98. themeName: "light",
  99. areaData: [],
  100. };
  101. },
  102. methods: {
  103. initChart() {
  104. let chart = echarts.init(this.$el);
  105. this.chart = chart;
  106. let option = {
  107. color: this.color,
  108. grid: {
  109. left: 40,
  110. right: 40,
  111. bottom: 16,
  112. top: 16,
  113. containLabel: true,
  114. },
  115. legend: {
  116. show: this.showLegend,
  117. data: this.bardata.legend,
  118. right: 56,
  119. // icon: "ract",
  120. itemWidth: 8,
  121. itemHeight: 8,
  122. inactiveColor:
  123. this.themeName === "dark" ? partten.getColor("gray") : "#838383",
  124. textStyle: {
  125. color:
  126. this.themeName === "dark" ? partten.getColor("grayl") : "#838383",
  127. fontSize: 12,
  128. },
  129. },
  130. tooltip: {
  131. trigger: "axis",
  132. backgroundColor: true
  133. ? "rgba(255,255,255,0.9)"
  134. : "rgba(255,255,255,0.9)",
  135. borderColor:
  136. this.themeName === "dark" ? partten.getColor("gray") : "#838383",
  137. textStyle: {
  138. color: this.themeName === "dark" ? "#fff" : "#838383",
  139. fontSize: util.vh(16),
  140. },
  141. },
  142. dataZoom: [
  143. {
  144. type: "inside",
  145. start: 0,
  146. end: this.end,
  147. yAxisIndex: [0],
  148. },
  149. {
  150. start: 0,
  151. end: this.end,
  152. top: 20,
  153. bottom: 40,
  154. yAxisIndex: [0],
  155. backgroundColor: "transparent",
  156. // 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",
  157. handleStyle: {
  158. color:
  159. this.themeName === "dark"
  160. ? partten.getColor("green")
  161. : partten.getColor("blue"),
  162. },
  163. moveHandleSize: 0,
  164. // dataBackground: {
  165. // lineStyle: {
  166. // color: partten.getColor("gray"),
  167. // },
  168. // areaStyle: {
  169. // color: partten.getColor("gray"),
  170. // },
  171. // },
  172. // selectedDataBackground: {
  173. // lineStyle: {
  174. // color: partten.getColor("yellow"),
  175. // },
  176. // areaStyle: {
  177. // color: partten.getColor("yellow"),
  178. // },
  179. // },
  180. fillerColor: "transparent",
  181. textStyle: {
  182. color:
  183. this.themeName === "dark"
  184. ? partten.getColor("grayl")
  185. : "#838383",
  186. },
  187. borderColor:
  188. this.themeName === "dark" ? partten.getColor("gray") : "#838383",
  189. brushSelect: false,
  190. },
  191. ],
  192. yAxis: [
  193. {
  194. type: "category",
  195. axisLabel: {
  196. color:
  197. this.themeName === "dark"
  198. ? partten.getColor("gray")
  199. : "#838383",
  200. },
  201. inverse: true,
  202. // minInterval: 10,
  203. // maxInterval: 10,
  204. axisLine: {
  205. show: false,
  206. },
  207. axisTick: {
  208. show: false,
  209. },
  210. data: this.areaData,
  211. },
  212. ],
  213. xAxis: [
  214. {
  215. type: "value",
  216. name: "万kWh",
  217. axisLabel: {
  218. color:
  219. this.themeName === "dark"
  220. ? partten.getColor("gray")
  221. : "#838383",
  222. // formatter: "{value}万kWh",
  223. },
  224. axisLine: {
  225. type: "dashed",
  226. lineStyle: {
  227. color:
  228. this.themeName === "dark"
  229. ? partten.getColor("gray")
  230. : "#838383",
  231. },
  232. width: 5,
  233. },
  234. axisTick: {
  235. show: false,
  236. },
  237. splitLine: {
  238. lineStyle: {
  239. type: "dashed",
  240. dashOffset: 10,
  241. color: this.themeName === "dark" ? "#5a6162" : "#838383" + 80,
  242. },
  243. },
  244. },
  245. {
  246. type: "value",
  247. name: "",
  248. axisLabel: {
  249. show: false,
  250. // formatter: "{value}万kWh",
  251. // color: partten.getColor("gray"),
  252. },
  253. axisLine: {
  254. show: false,
  255. },
  256. axisTick: {
  257. show: false,
  258. },
  259. splitLine: {
  260. show: false,
  261. },
  262. },
  263. ],
  264. series: [],
  265. };
  266. if (this.bardata && this.bardata.legend) {
  267. // bar data
  268. for (var i = 0; i < this.bardata.legend.length; i++) {
  269. option.series.push({
  270. name: this.bardata.legend[i].name,
  271. type: "bar",
  272. stack: "总量",
  273. barWidth: 16,
  274. label: {
  275. show: false,
  276. position: "insideRight",
  277. },
  278. data: this.bardata.data[i],
  279. });
  280. }
  281. }
  282. // line data
  283. if (this.lineData.length > 0) {
  284. option.series.push({
  285. name: "理论发电量",
  286. type: "line",
  287. data: this.lineData,
  288. smooth: false, //平滑展示
  289. xAxisIndex: 1,
  290. lineStyle: {
  291. color:
  292. this.themeName === "dark"
  293. ? partten.getColor("green")
  294. : partten.getColor("blue"),
  295. },
  296. itemStyle: {
  297. color:
  298. this.themeName === "dark"
  299. ? partten.getColor("green")
  300. : partten.getColor("blue"),
  301. },
  302. });
  303. }
  304. chart.setOption(option);
  305. console.log(55555555555555555)
  306. chart.resize();
  307. },
  308. },
  309. created() {
  310. this.id = "pie-chart-" + util.newGUID();
  311. if (this.bardata.area && this.bardata.area.length < this.pageSize) {
  312. this.areaData = this.bardata.area;
  313. for (let i = this.bardata.area.length; i <= this.pageSize; i++) {
  314. this.areaData.push("");
  315. }
  316. }
  317. },
  318. mounted() {
  319. this.$nextTick(() => {
  320. this.$el.style.width = this.width;
  321. this.$el.style.height = this.height;
  322. this.initChart();
  323. });
  324. },
  325. updated() {
  326. this.$nextTick(() => {
  327. this.initChart();
  328. });
  329. },
  330. beforeUpdate() {
  331. this.areaData = this.bardata.area;
  332. },
  333. beforeUpdate() {
  334. this.areaData = this.bardata.area;
  335. },
  336. activated() {
  337. this.$nextTick(() => {
  338. this.$el.style.width = this.width;
  339. this.$el.style.height = this.height;
  340. this.initChart();
  341. });
  342. },
  343. computed: {
  344. collapse() {
  345. return this.$store.state.collapse;
  346. },
  347. legend() {
  348. return this.bardata.legend;
  349. },
  350. end() {
  351. var result = 20;
  352. if (this.areaData) {
  353. result = parseInt((this.pageSize / this.areaData.length) * 100);
  354. }
  355. return result;
  356. },
  357. },
  358. watch: {
  359. collapse(val) {
  360. if (this.chart) {
  361. setTimeout(() => {
  362. this.chart.resize();
  363. }, 300);
  364. }
  365. },
  366. bardata(val) {
  367. if (val.area && val.area.length < this.pageSize) {
  368. this.areaData = val.area;
  369. for (let i = val.area.length; i <= this.pageSize; i++) {
  370. this.areaData.push("");
  371. }
  372. }
  373. },
  374. },
  375. };
  376. </script>
  377. <style lang="less" scoped>
  378. .chart {
  379. width: 100%;
  380. height: 100%;
  381. display: inline-block;
  382. }
  383. </style>