multiple-bar-line-chart.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. color: "#fff",
  111. },
  112. axisLine: {
  113. show: false,
  114. type: "dashed",
  115. lineStyle: {
  116. color: "#fff",
  117. },
  118. width: 5,
  119. },
  120. //分格线
  121. splitLine: {
  122. lineStyle: {
  123. color: "rgba(#fff, 0.2)",
  124. type: "dashed",
  125. },
  126. },
  127. };
  128. } else {
  129. data = {
  130. type: "value",
  131. name: value,
  132. axisLabel: {
  133. formatter: "{value} ",
  134. fontSize: 12,
  135. color: "#fff",
  136. },
  137. axisLine: {
  138. show: false,
  139. type: "dashed",
  140. lineStyle: {
  141. color: "#fff",
  142. },
  143. width: 5,
  144. },
  145. //分格线
  146. splitLine: {
  147. show: false,
  148. },
  149. };
  150. }
  151. result.push(data);
  152. });
  153. return result;
  154. },
  155. series() {
  156. let result = [];
  157. if (this.newbarData && this.newbarData.length > 0) {
  158. this.newbarData.forEach((value, index) => {
  159. result.push({
  160. name: value.title,
  161. type: "bar",
  162. barWidth: "10%",
  163. animation: this.firstAnimation && this.showAnimation,
  164. yAxisIndex: value.yAxisIndex,
  165. data: value.value.map((t) => {
  166. return t.value;
  167. }),
  168. });
  169. });
  170. }
  171. if (
  172. this.newlineData &&
  173. this.newlineData.data &&
  174. this.newlineData.data.length > 0
  175. ) {
  176. result.push({
  177. name: this.newlineData.name,
  178. type: "line",
  179. data: this.newlineData.data,
  180. smooth: true, //平滑展示
  181. yAxisIndex: 1,
  182. lineStyle: {
  183. color: "#f8de5b",
  184. },
  185. itemStyle: {
  186. color: "#f8de5b",
  187. },
  188. });
  189. }
  190. return result;
  191. },
  192. },
  193. methods: {
  194. resize() {},
  195. initChart() {
  196. let chart = echarts.init(this.$el);
  197. let option = {
  198. color: this.color,
  199. tooltip: {
  200. trigger: "axis",
  201. backgroundColor: "rgba(255, 255, 255, 0.5)",
  202. borderColor: "#fff",
  203. textStyle: {
  204. color: "#fff",
  205. fontSize: 12,
  206. },
  207. },
  208. legend: {
  209. show: this.showLegend,
  210. data: this.legend,
  211. right: 56,
  212. icon: "ract",
  213. itemWidth: 8,
  214. itemHeight: 8,
  215. inactiveColor: "#fff",
  216. textStyle: {
  217. color: "#fff",
  218. fontSize: 12,
  219. },
  220. },
  221. grid: {
  222. top: 32,
  223. left: 40,
  224. right: this.ydata.length > 1 ? 40 : 14,
  225. bottom: 24,
  226. },
  227. xAxis: [
  228. {
  229. type: "category",
  230. data: this.xdata,
  231. axisPointer: {
  232. type: "shadow",
  233. },
  234. axisLabel: {
  235. color: "#fff",
  236. fontSize: 12,
  237. },
  238. },
  239. ],
  240. yAxis: this.ydata,
  241. series: this.series,
  242. };
  243. chart.clear();
  244. chart.setOption(option);
  245. this.resize = function () {
  246. chart.resize();
  247. };
  248. window.addEventListener("resize", this.resize);
  249. },
  250. },
  251. created() {
  252. // this.id = "pie-chart-" + util.newGUID();
  253. this.newbarData = this.barData;
  254. this.newlineData = this.lineData;
  255. },
  256. mounted() {
  257. this.$nextTick(() => {
  258. this.$el.style.width = this.width;
  259. this.$el.style.height = this.height;
  260. this.initChart();
  261. this.firstAnimation = false;
  262. });
  263. },
  264. updated() {
  265. this.$nextTick(() => {
  266. this.initChart();
  267. });
  268. },
  269. unmounted() {
  270. window.removeEventListener("resize", this.resize);
  271. },
  272. };
  273. </script>
  274. <style lang="less">
  275. .chart {
  276. width: 100%;
  277. height: 100%;
  278. display: inline-block;
  279. }
  280. </style>