multiple-bar-line-chart.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. console.warn(newValue);
  212. this.newbarData = newValue;
  213. this.initChart();
  214. },
  215. deep: true,
  216. },
  217. lineData : {
  218. handler(newValue, oldValue) {
  219. console.warn(newValue);
  220. this.newlineData = newValue;
  221. this.initChart();
  222. },
  223. deep: true,
  224. },
  225. },
  226. computed: {
  227. legend() {
  228. return this.newbarData.map((t) => {
  229. return t.title;
  230. });
  231. },
  232. xdata() {
  233. let result = [];
  234. if (
  235. this.newbarData &&
  236. this.newbarData.length > 0 &&
  237. this.newbarData[0].value.length > 0
  238. ) {
  239. result = this.newbarData[0].value.map((t) => {
  240. return t.text;
  241. });
  242. }
  243. return result;
  244. },
  245. ydata() {
  246. let result = [];
  247. this.units.forEach((value, index) => {
  248. let data = null;
  249. if (index == 0) {
  250. data = {
  251. type: "value",
  252. name: value,
  253. axisLabel: {
  254. formatter: "{value} ",
  255. fontSize: 12,
  256. },
  257. //分格线
  258. splitLine: {
  259. lineStyle: {
  260. color: "#5a6162",
  261. type: "dashed",
  262. },
  263. },
  264. };
  265. } else {
  266. data = {
  267. type: "value",
  268. name: value,
  269. axisLabel: {
  270. formatter: "{value}",
  271. fontSize: 12,
  272. },
  273. //分格线
  274. splitLine: {
  275. show: false,
  276. },
  277. };
  278. }
  279. result.push(data);
  280. });
  281. return result;
  282. },
  283. series() {
  284. let result = [];
  285. if (this.newbarData && this.newbarData.length > 0) {
  286. this.newbarData.forEach((value, index) => {
  287. result.push({
  288. name: value.title,
  289. type: "bar",
  290. barWidth: "10%",
  291. animation: this.firstAnimation && this.showAnimation,
  292. yAxisIndex: value.yAxisIndex,
  293. data: value.value.map((t) => {
  294. return t.value;
  295. }),
  296. });
  297. });
  298. }
  299. return result;
  300. },
  301. },
  302. methods: {
  303. resize() {},
  304. initChart() {
  305. let chart = echarts.init(this.$el);
  306. let option = {
  307. color: this.color,
  308. tooltip: {
  309. trigger: "axis",
  310. backgroundColor: "rgba(0,0,0,0.4)",
  311. borderColor: partten.getColor("gray"),
  312. textStyle: {
  313. color: "#fff",
  314. fontSize: 12,
  315. },
  316. },
  317. legend: {
  318. show: this.showLegend,
  319. data: this.legend,
  320. right: 56,
  321. icon: "ract",
  322. itemWidth: 8,
  323. itemHeight: 8,
  324. inactiveColor: partten.getColor("gray"),
  325. textStyle: {
  326. color: partten.getColor("grayl"),
  327. fontSize: 12,
  328. },
  329. },
  330. grid: {
  331. top: 32,
  332. left: 40,
  333. right: this.ydata.length > 1 ? 40 : 14,
  334. bottom: 24,
  335. },
  336. xAxis: [
  337. {
  338. type: "category",
  339. data: this.xdata,
  340. axisPointer: {
  341. type: "shadow",
  342. },
  343. axisLabel: {
  344. fontSize: 12,
  345. },
  346. },
  347. ],
  348. yAxis: this.ydata,
  349. series: this.series,
  350. };
  351. // line data
  352. if (this.newlineData && this.newlineData.data.length > 0) {
  353. option.yAxis.push({
  354. type: "value",
  355. name: this.newlineData.name,
  356. axisLabel: {
  357. formatter: "{value} ",
  358. color: partten.getColor("gray"),
  359. },
  360. axisLine: {
  361. show: false,
  362. },
  363. axisTick: {
  364. show: false,
  365. },
  366. splitLine: {
  367. show: false,
  368. lineStyle: {
  369. type: "dashed",
  370. dashOffset: 10,
  371. color: partten.getColor("gray") + 80,
  372. },
  373. },
  374. });
  375. option.series.push({
  376. name: this.newlineData.name,
  377. type: "line",
  378. data: this.newlineData.data,
  379. smooth: true, //平滑展示
  380. yAxisIndex: option.yAxis.length - 1,
  381. lineStyle: {
  382. color: partten.getColor("yellow"),
  383. },
  384. itemStyle: {
  385. color: partten.getColor("yellow"),
  386. },
  387. });
  388. }
  389. chart.clear();
  390. chart.setOption(option);
  391. this.resize = function () {
  392. chart.resize();
  393. };
  394. window.addEventListener("resize", this.resize);
  395. },
  396. },
  397. created() {
  398. this.id = "pie-chart-" + util.newGUID();
  399. this.newbarData = this.barData;
  400. },
  401. mounted() {
  402. this.$nextTick(() => {
  403. this.$el.style.width = this.width;
  404. this.$el.style.height = this.height;
  405. this.initChart();
  406. this.firstAnimation = false;
  407. });
  408. },
  409. updated() {
  410. this.$nextTick(() => {
  411. this.initChart();
  412. });
  413. },
  414. unmounted() {
  415. window.removeEventListener("resize", this.resize);
  416. },
  417. };
  418. </script>
  419. <style lang="less">
  420. .chart {
  421. width: 100%;
  422. height: 100%;
  423. display: inline-block;
  424. }
  425. </style>