multiple-bar-line-chart.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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: "13.889vh",
  19. },
  20. // 传入数据
  21. barData: {
  22. type: Array,
  23. default: () => [
  24. {
  25. title: "日发电量",
  26. yAxisIndex: 0,
  27. value: [
  28. {
  29. text: "1日",
  30. value: 1,
  31. },
  32. {
  33. text: "2日",
  34. value: 2,
  35. },
  36. {
  37. text: "3日",
  38. value: 1,
  39. },
  40. {
  41. text: "4日",
  42. value: 3,
  43. },
  44. {
  45. text: "5日",
  46. value: 3,
  47. },
  48. {
  49. text: "6日",
  50. value: 3,
  51. },
  52. {
  53. text: "7日",
  54. value: 3,
  55. },
  56. ],
  57. },
  58. {
  59. title: "上网电量",
  60. yAxisIndex: 0,
  61. value: [
  62. {
  63. text: "1日",
  64. value: 1,
  65. },
  66. {
  67. text: "2日",
  68. value: 2,
  69. },
  70. {
  71. text: "3日",
  72. value: 1,
  73. },
  74. {
  75. text: "4日",
  76. value: 3,
  77. },
  78. {
  79. text: "5日",
  80. value: 3,
  81. },
  82. {
  83. text: "6日",
  84. value: 3,
  85. },
  86. {
  87. text: "7日",
  88. value: 3,
  89. },
  90. ],
  91. },
  92. {
  93. title: "购网电量",
  94. yAxisIndex: 0,
  95. value: [
  96. {
  97. text: "1日",
  98. value: 1,
  99. },
  100. {
  101. text: "2日",
  102. value: 2,
  103. },
  104. {
  105. text: "3日",
  106. value: 1,
  107. },
  108. {
  109. text: "4日",
  110. value: 3,
  111. },
  112. {
  113. text: "5日",
  114. value: 3,
  115. },
  116. {
  117. text: "6日",
  118. value: 3,
  119. },
  120. {
  121. text: "7日",
  122. value: 3,
  123. },
  124. ],
  125. },
  126. {
  127. title: "风速",
  128. yAxisIndex: 1,
  129. value: [
  130. {
  131. text: "1日",
  132. value: 1,
  133. },
  134. {
  135. text: "2日",
  136. value: 2,
  137. },
  138. {
  139. text: "3日",
  140. value: 1,
  141. },
  142. {
  143. text: "4日",
  144. value: 3,
  145. },
  146. {
  147. text: "5日",
  148. value: 3,
  149. },
  150. {
  151. text: "6日",
  152. value: 3,
  153. },
  154. {
  155. text: "7日",
  156. value: 3,
  157. },
  158. ],
  159. },
  160. ],
  161. },
  162. lineData: {
  163. type: Object,
  164. default: () => {
  165. return {
  166. name: "风速",
  167. unit: "km",
  168. data: [200, 800, 400, 500, 800, 700, 800, 900, 200],
  169. };
  170. },
  171. },
  172. // 单位
  173. units: {
  174. type: Array,
  175. default: () => ["(万KWh)", "(风速)"],
  176. },
  177. // 显示 legend
  178. showLegend: {
  179. type: Boolean,
  180. default: true,
  181. },
  182. // 颜色
  183. color: {
  184. type: Array,
  185. default: () => [
  186. "#05bb4c",
  187. "#4b55ae",
  188. "#fa8c16",
  189. "#f8de5b",
  190. "#1a93cf",
  191. "#c531c7",
  192. "#bd3338",
  193. ],
  194. },
  195. showAnimation: {
  196. type: Boolean,
  197. default: true,
  198. },
  199. },
  200. data() {
  201. return {
  202. id: "",
  203. chart: null,
  204. firstAnimation: true,
  205. newbarData: null,
  206. };
  207. },
  208. watch: {
  209. barData: {
  210. handler(newValue, oldValue) {
  211. this.newbarData = newValue;
  212. this.initChart();
  213. },
  214. deep: true,
  215. },
  216. lineData : {
  217. handler(newValue, oldValue) {
  218. this.newlineData = newValue;
  219. this.initChart();
  220. },
  221. deep: true,
  222. },
  223. },
  224. computed: {
  225. legend() {
  226. return this.newbarData.map((t) => {
  227. return t.title;
  228. });
  229. },
  230. xdata() {
  231. let result = [];
  232. if (
  233. this.newbarData &&
  234. this.newbarData.length > 0 &&
  235. this.newbarData[0].value.length > 0
  236. ) {
  237. result = this.newbarData[0].value.map((t) => {
  238. return t.text;
  239. });
  240. }
  241. return result;
  242. },
  243. ydata() {
  244. let result = [];
  245. this.units.forEach((value, index) => {
  246. let data = null;
  247. if (index == 0) {
  248. data = {
  249. type: "value",
  250. name: value,
  251. axisLabel: {
  252. formatter: "{value} ",
  253. fontSize: 12,
  254. },
  255. //分格线
  256. splitLine: {
  257. lineStyle: {
  258. color: "#5a6162",
  259. type: "dashed",
  260. },
  261. },
  262. };
  263. } else {
  264. data = {
  265. type: "value",
  266. name: value,
  267. axisLabel: {
  268. formatter: "{value}",
  269. fontSize: 12,
  270. },
  271. //分格线
  272. splitLine: {
  273. show: false,
  274. },
  275. };
  276. }
  277. result.push(data);
  278. });
  279. return result;
  280. },
  281. series() {
  282. let result = [];
  283. if (this.newbarData && this.newbarData.length > 0) {
  284. this.newbarData.forEach((value, index) => {
  285. result.push({
  286. name: value.title,
  287. type: "bar",
  288. barWidth: "10%",
  289. animation: this.firstAnimation && this.showAnimation,
  290. yAxisIndex: value.yAxisIndex,
  291. data: value.value.map((t) => {
  292. return t.value;
  293. }),
  294. });
  295. });
  296. }
  297. return result;
  298. },
  299. },
  300. methods: {
  301. resize() {},
  302. initChart() {
  303. let chart = echarts.init(this.$el);
  304. let option = {
  305. color: this.color,
  306. tooltip: {
  307. trigger: "axis",
  308. backgroundColor: "rgba(0,0,0,0.4)",
  309. borderColor: partten.getColor("gray"),
  310. textStyle: {
  311. color: "#fff",
  312. fontSize: 12,
  313. },
  314. },
  315. legend: {
  316. show: this.showLegend,
  317. data: this.legend,
  318. right: 56,
  319. icon: "ract",
  320. itemWidth: 8,
  321. itemHeight: 8,
  322. inactiveColor: partten.getColor("gray"),
  323. textStyle: {
  324. color: partten.getColor("grayl"),
  325. fontSize: 12,
  326. },
  327. },
  328. grid: {
  329. top: 32,
  330. left: 40,
  331. right: this.ydata.length > 1 ? 40 : 14,
  332. bottom: 24,
  333. },
  334. xAxis: [
  335. {
  336. type: "category",
  337. data: this.xdata,
  338. axisPointer: {
  339. type: "shadow",
  340. },
  341. axisLabel: {
  342. fontSize: 12,
  343. },
  344. },
  345. ],
  346. yAxis: this.ydata,
  347. series: this.series,
  348. };
  349. // line data
  350. if (this.newlineData && this.newlineData.data.length > 0) {
  351. option.yAxis.push({
  352. type: "value",
  353. name: this.newlineData.name,
  354. axisLabel: {
  355. formatter: "{value} ",
  356. color: partten.getColor("gray"),
  357. },
  358. axisLine: {
  359. show: false,
  360. },
  361. axisTick: {
  362. show: false,
  363. },
  364. splitLine: {
  365. show: false,
  366. lineStyle: {
  367. type: "dashed",
  368. dashOffset: 10,
  369. color: partten.getColor("gray") + 80,
  370. },
  371. },
  372. });
  373. option.series.push({
  374. name: this.newlineData.name,
  375. type: "line",
  376. data: this.newlineData.data,
  377. smooth: true, //平滑展示
  378. yAxisIndex: option.yAxis.length - 1,
  379. lineStyle: {
  380. color: partten.getColor("yellow"),
  381. },
  382. itemStyle: {
  383. color: partten.getColor("yellow"),
  384. },
  385. });
  386. }
  387. chart.clear();
  388. chart.setOption(option);
  389. this.resize = function () {
  390. chart.resize();
  391. };
  392. window.addEventListener("resize", this.resize);
  393. },
  394. },
  395. created() {
  396. this.id = "pie-chart-" + util.newGUID();
  397. this.newbarData = this.barData;
  398. },
  399. mounted() {
  400. this.$nextTick(() => {
  401. this.$el.style.width = this.width;
  402. this.$el.style.height = this.height;
  403. this.initChart();
  404. this.firstAnimation = false;
  405. });
  406. },
  407. updated() {
  408. this.$nextTick(() => {
  409. this.initChart();
  410. });
  411. },
  412. unmounted() {
  413. window.removeEventListener("resize", this.resize);
  414. },
  415. };
  416. </script>
  417. <style lang="less">
  418. .chart {
  419. width: 100%;
  420. height: 100%;
  421. display: inline-block;
  422. }
  423. </style>