multiple-bar-line-chart.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <template>
  2. <div class="chart" id="stand-alone-chart"></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. lineData: {
  26. type: Object,
  27. default: () => {},
  28. },
  29. // 单位
  30. units: {
  31. type: Array,
  32. default: () => ["(万KWh)", "(风速)"],
  33. },
  34. // 显示 legend
  35. showLegend: {
  36. type: Boolean,
  37. default: true,
  38. },
  39. // 颜色
  40. color: {
  41. type: Array,
  42. default: () => ["#05bb4c", "#e17e23", "#ba3237", "#c531c7", "#1a93cf"],
  43. },
  44. showAnimation: {
  45. type: Boolean,
  46. default: true,
  47. },
  48. },
  49. data() {
  50. return {
  51. id: "",
  52. chart: null,
  53. firstAnimation: true,
  54. newbarData: [],
  55. newlineData: [],
  56. };
  57. },
  58. watch: {
  59. barData: {
  60. handler(newValue, oldValue) {
  61. this.newbarData = newValue;
  62. this.initChart();
  63. },
  64. deep: true,
  65. },
  66. lineData: {
  67. handler(newValue, oldValue) {
  68. this.newlineData = newValue;
  69. this.initChart();
  70. },
  71. deep: true,
  72. },
  73. "$store.state.themeName"() {
  74. this.initChart();
  75. },
  76. },
  77. computed: {
  78. legend() {
  79. return [
  80. ...this.newbarData.map((t) => {
  81. return t.title;
  82. }),
  83. this.newlineData.name,
  84. ];
  85. },
  86. xdata() {
  87. let result = [];
  88. if (
  89. this.newbarData &&
  90. this.newbarData.length > 0 &&
  91. this.newbarData[0].value.length > 0
  92. ) {
  93. result = this.newbarData[0].value.map((t) => {
  94. return t.text;
  95. });
  96. }
  97. return result;
  98. },
  99. ydata() {
  100. let result = [];
  101. this.units.forEach((value, index) => {
  102. let data = null;
  103. if (index == 0) {
  104. data = {
  105. type: "value",
  106. name: value,
  107. axisLabel: {
  108. formatter: "{value} ",
  109. fontSize: 12,
  110. },
  111. //分格线
  112. splitLine: {
  113. lineStyle: {
  114. color: "#5a6162",
  115. type: "dashed",
  116. },
  117. },
  118. };
  119. } else {
  120. data = {
  121. type: "value",
  122. name: value,
  123. axisLabel: {
  124. formatter: "{value}",
  125. fontSize: 12,
  126. },
  127. //分格线
  128. splitLine: {
  129. show: false,
  130. },
  131. };
  132. }
  133. result.push(data);
  134. });
  135. return result;
  136. },
  137. series() {
  138. let result = [];
  139. if (this.newbarData && this.newbarData.length > 0) {
  140. this.newbarData.forEach((value, index) => {
  141. result.push({
  142. name: value.title,
  143. type: "bar",
  144. barWidth: "10%",
  145. animation: this.firstAnimation && this.showAnimation,
  146. yAxisIndex: value.yAxisIndex,
  147. data: value.value.map((t) => {
  148. return t.value;
  149. }),
  150. });
  151. });
  152. }
  153. if (
  154. this.newlineData &&
  155. this.newlineData.data &&
  156. this.newlineData.data.length > 0
  157. ) {
  158. result.push({
  159. name: this.newlineData.name,
  160. type: "line",
  161. data: this.newlineData.data,
  162. smooth: true, //平滑展示
  163. yAxisIndex: 1,
  164. lineStyle: {
  165. color: "#f8de5b",
  166. },
  167. itemStyle: {
  168. color: "#f8de5b",
  169. },
  170. });
  171. }
  172. return result;
  173. },
  174. },
  175. methods: {
  176. resize() {},
  177. initChart() {
  178. let chart = echarts.init(this.$el);
  179. let option = {
  180. color: this.color,
  181. tooltip: {
  182. trigger: "axis",
  183. backgroundColor:
  184. this.$store.state.themeName === "dark"
  185. ? "rgba(0,0,0,0.4)"
  186. : "rgba(255,255,255,0.5)",
  187. borderColor:
  188. this.$store.state.themeName === "dark"
  189. ? partten.getColor("gray")
  190. : "#000",
  191. textStyle: {
  192. color: this.$store.state.themeName === "dark" ? "#fff" : "#000",
  193. fontSize: 12,
  194. },
  195. },
  196. legend: {
  197. show: this.showLegend,
  198. data: this.legend,
  199. right: 56,
  200. icon: "ract",
  201. itemWidth: 8,
  202. itemHeight: 8,
  203. inactiveColor:
  204. this.$store.state.themeName === "dark"
  205. ? partten.getColor("gray")
  206. : "#000",
  207. textStyle: {
  208. color:
  209. this.$store.state.themeName === "dark"
  210. ? partten.getColor("grayl")
  211. : "#000",
  212. fontSize: 12,
  213. },
  214. },
  215. grid: {
  216. top: 32,
  217. left: 40,
  218. right: this.ydata.length > 1 ? 40 : 14,
  219. bottom: 24,
  220. },
  221. xAxis: [
  222. {
  223. type: "category",
  224. data: this.xdata,
  225. axisPointer: {
  226. type: "shadow",
  227. },
  228. axisLabel: {
  229. fontSize: 12,
  230. },
  231. },
  232. ],
  233. yAxis: this.ydata,
  234. series: this.series,
  235. };
  236. chart.clear();
  237. chart.setOption(option);
  238. this.resize = function () {
  239. chart.resize();
  240. };
  241. window.addEventListener("resize", this.resize);
  242. },
  243. },
  244. created() {
  245. // this.id = "pie-chart-" + util.newGUID();
  246. this.newbarData = this.barData;
  247. this.newlineData = this.lineData;
  248. },
  249. mounted() {
  250. this.$nextTick(() => {
  251. this.$el.style.width = this.width;
  252. this.$el.style.height = this.height;
  253. this.initChart();
  254. this.firstAnimation = false;
  255. });
  256. },
  257. updated() {
  258. this.$nextTick(() => {
  259. this.initChart();
  260. });
  261. },
  262. unmounted() {
  263. window.removeEventListener("resize", this.resize);
  264. },
  265. };
  266. </script>
  267. <style lang="less">
  268. .chart {
  269. width: 100%;
  270. height: 100%;
  271. display: inline-block;
  272. }
  273. </style>