bar-line-chart.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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: this.$store.state.themeName === "dark"
  125. ? partten.getColor("gray")
  126. : "#000",
  127. textStyle: {
  128. color: this.$store.state.themeName === "dark"
  129. ? partten.getColor("grayl")
  130. : "#000",
  131. fontSize: 12,
  132. },
  133. },
  134. tooltip: {
  135. trigger: "axis",
  136. backgroundColor: this.$store.state.themeName === "dark"
  137. ? "rgba(0,0,0,0.4)"
  138. : "rgba(255,255,255,0.5)",
  139. borderColor: this.$store.state.themeName === "dark"
  140. ? partten.getColor("gray")
  141. : "#000",
  142. textStyle: {
  143. color: this.$store.state.themeName === "dark" ? "#fff" : "#000",
  144. fontSize: util.vh(16),
  145. },
  146. },
  147. dataZoom: [
  148. {
  149. type: "inside",
  150. start: 0,
  151. end: this.end,
  152. yAxisIndex: [0],
  153. },
  154. {
  155. start: 0,
  156. end: this.end,
  157. bottom: 40,
  158. yAxisIndex: [0],
  159. backgroundColor: "transparent",
  160. // 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",
  161. handleStyle: {
  162. color: this.$store.state.themeName === "dark"
  163. ? partten.getColor("green")
  164. : partten.getColor("blue"),
  165. },
  166. moveHandleSize: 0,
  167. // dataBackground: {
  168. // lineStyle: {
  169. // color: partten.getColor("gray"),
  170. // },
  171. // areaStyle: {
  172. // color: partten.getColor("gray"),
  173. // },
  174. // },
  175. // selectedDataBackground: {
  176. // lineStyle: {
  177. // color: partten.getColor("yellow"),
  178. // },
  179. // areaStyle: {
  180. // color: partten.getColor("yellow"),
  181. // },
  182. // },
  183. fillerColor: "transparent",
  184. textStyle: {
  185. color: this.$store.state.themeName === "dark"
  186. ? partten.getColor("grayl")
  187. : "#000",
  188. },
  189. borderColor: this.$store.state.themeName === "dark"
  190. ? partten.getColor("gray")
  191. : "#000",
  192. brushSelect: false,
  193. },
  194. ],
  195. yAxis: [
  196. {
  197. type: "category",
  198. axisLabel: {
  199. color: this.$store.state.themeName === "dark"
  200. ? partten.getColor("gray")
  201. : "#000",
  202. },
  203. inverse: true,
  204. // minInterval: 10,
  205. // maxInterval: 10,
  206. axisLine: {
  207. show: false,
  208. },
  209. axisTick: {
  210. show: false,
  211. },
  212. data: this.areaData,
  213. },
  214. ],
  215. xAxis: [
  216. {
  217. type: "value",
  218. axisLabel: {
  219. color: this.$store.state.themeName === "dark"
  220. ? partten.getColor("gray")
  221. : "#000",
  222. },
  223. axisLine: {
  224. type: "dashed",
  225. lineStyle: {
  226. color: this.$store.state.themeName === "dark"
  227. ? partten.getColor("gray")
  228. : "#000",
  229. },
  230. width: 5,
  231. },
  232. axisTick: {
  233. show: false,
  234. },
  235. splitLine: {
  236. lineStyle: {
  237. type: "dashed",
  238. dashOffset: 10,
  239. color: this.$store.state.themeName === "dark" ? "#5a6162" : "#000" + 80,
  240. },
  241. },
  242. },
  243. {
  244. type: "value",
  245. name: "",
  246. axisLabel: {
  247. show: false,
  248. // formatter: "{value}",
  249. // color: partten.getColor("gray"),
  250. },
  251. axisLine: {
  252. show: false,
  253. },
  254. axisTick: {
  255. show: false,
  256. },
  257. splitLine: {
  258. show: false,
  259. },
  260. },
  261. ],
  262. series: [],
  263. };
  264. if (this.bardata && this.bardata.legend)
  265. // bar data
  266. for (var i = 0; i < this.bardata.legend.length; i++) {
  267. option.series.push({
  268. name: this.bardata.legend[i],
  269. type: "bar",
  270. stack: "总量",
  271. barWidth: 16,
  272. label: {
  273. show: false,
  274. position: "insideRight",
  275. },
  276. data: this.bardata.data[i],
  277. });
  278. }
  279. // line data
  280. if (this.lineData.length > 0) {
  281. option.series.push({
  282. name: this.lineName,
  283. type: "line",
  284. data: this.lineData,
  285. smooth: false, //平滑展示
  286. xAxisIndex: 1,
  287. lineStyle: {
  288. color: this.$store.state.themeName === "dark"
  289. ? partten.getColor("green")
  290. : partten.getColor("blue"),
  291. },
  292. itemStyle: {
  293. color: this.$store.state.themeName === "dark"
  294. ? partten.getColor("green")
  295. : partten.getColor("blue"),
  296. },
  297. });
  298. }
  299. chart.setOption(option);
  300. },
  301. },
  302. created() {
  303. this.id = "pie-chart-" + util.newGUID();
  304. if (this.bardata.area && this.bardata.area.length < this.pageSize) {
  305. this.areaData = this.bardata.area;
  306. for (let i = this.bardata.area.length; i <= this.pageSize; i++) {
  307. this.areaData.push("");
  308. }
  309. }
  310. },
  311. mounted() {
  312. this.$nextTick(() => {
  313. this.$el.style.width = this.width;
  314. this.$el.style.height = this.height;
  315. this.initChart();
  316. });
  317. },
  318. updated() {
  319. this.$nextTick(() => {
  320. this.initChart();
  321. });
  322. },
  323. beforeUpdate(){
  324. this.areaData = this.bardata.area;
  325. },
  326. beforeUpdate(){
  327. this.areaData = this.bardata.area;
  328. },
  329. watch: {
  330. bardata(val) {
  331. if (val.area && val.area.length < this.pageSize) {
  332. this.areaData = val.area;
  333. for (let i = val.area.length; i <= this.pageSize; i++) {
  334. this.areaData.push("");
  335. }
  336. }
  337. },
  338. "$store.state.themeName"() {
  339. this.initChart();
  340. },
  341. },
  342. };
  343. </script>
  344. <style lang="less">
  345. .chart {
  346. width: 100%;
  347. height: 100%;
  348. display: inline-block;
  349. }
  350. </style>