bar.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <template>
  2. <div id="bar_echart" ref="echart"></div>
  3. </template>
  4. <script>
  5. import { cloneDeep } from "lodash";
  6. let option = {
  7. backgroundColor: "rgba(0,0,0,0)",
  8. tooltip: {
  9. backgroundColor: "RGBA(20, 106, 178, 0.4)",
  10. trigger: "axis",
  11. textStyle: {
  12. fontSize: 14,
  13. color: "#fff",
  14. },
  15. },
  16. grid: {
  17. left: "5%",
  18. right: "4%",
  19. bottom: "5%",
  20. top: "20%",
  21. containLabel: true,
  22. },
  23. legend: {
  24. data: [], //"收储面积", "收储个数"
  25. left: "7%",
  26. top: "5%",
  27. textStyle: {
  28. color: "#666666",
  29. },
  30. itemWidth: 15,
  31. itemHeight: 10,
  32. itemGap: 25,
  33. show: false,
  34. },
  35. xAxis: {
  36. axisTick: { show: false },
  37. axisLine: { lineStyle: { color: "#BCD3E5" } },
  38. axisLabel: {
  39. textStyle: { fontSize: 12, color: "#BCD3E5" },
  40. },
  41. axisLabel: {
  42. interval: 0,
  43. formatter: function (value) {
  44. // 当文字长度大于2时,使用slice方法截取字符串并拼接省略号;否则直接返回原文字
  45. if (value.length > 4) {
  46. return `${value.slice(0, 4)}...`;
  47. } else {
  48. return value;
  49. }
  50. },
  51. },
  52. data: [],
  53. },
  54. yAxis: [
  55. {
  56. type: "value",
  57. name: "",
  58. nameTextStyle: {
  59. color: "#ffffff",
  60. },
  61. splitLine: {
  62. show: false,
  63. lineStyle: {
  64. color: "rgba(163, 163, 163, 0.5)",
  65. type: "dashed",
  66. },
  67. },
  68. axisLabel: {
  69. color: "#A0A4AA",
  70. },
  71. axisLine: {
  72. show: true,
  73. lineStyle: {
  74. color: "rgba(65, 97, 128, 0.5)",
  75. },
  76. },
  77. },
  78. {
  79. // name: obj.yAxis ? obj.yAxis[0].name : "项目个数/个",
  80. // nameTextStyle: {
  81. // color: "#fff",
  82. // fontSize: 12,
  83. // padding: [0, 0, 4, 0], //name文字位置 对应 上右下左
  84. // },
  85. type: "value",
  86. name: "",
  87. nameTextStyle: {
  88. color: "#ffffff",
  89. },
  90. position: "right",
  91. axisLine: {
  92. lineStyle: {
  93. color: "#cdd5e2",
  94. },
  95. },
  96. splitLine: {
  97. show: false,
  98. },
  99. axisLabel: {
  100. show: false,
  101. formatter: "{value} %", //右侧Y轴文字显示
  102. textStyle: {
  103. color: "#666666",
  104. },
  105. },
  106. },
  107. ],
  108. series: [],
  109. };
  110. // 柱体
  111. let seriesItem = {
  112. name: "", //收储面积
  113. type: "bar",
  114. barWidth: "12px",
  115. itemStyle: {
  116. normal: {
  117. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  118. {
  119. offset: 0,
  120. color: "#409EFF",
  121. },
  122. {
  123. offset: 1,
  124. color: "rgba(24, 253, 255, 0)",
  125. },
  126. ]),
  127. },
  128. },
  129. data: [],
  130. };
  131. let lineItem = {
  132. name: "", //收储个数
  133. type: "line",
  134. smooth: true,
  135. itemStyle: {
  136. normal: {
  137. color: "#FFCC64", // 折线的颜色
  138. },
  139. },
  140. data: [],
  141. };
  142. export default {
  143. name: "bar_echart",
  144. props: {
  145. unit: {
  146. type: String,
  147. default: "km²", //"平方千米",
  148. },
  149. },
  150. components: {},
  151. data() {
  152. return {
  153. myChart: null,
  154. };
  155. },
  156. methods: {
  157. setOptions(v) {
  158. let _this = this;
  159. if (!this.myChart) {
  160. // var dom = document.getElementById("bar_echart");
  161. this.myChart = echarts.init(this.$refs.echart);
  162. }
  163. option.legend.data = v.legend;
  164. option.xAxis.data = v.xData;
  165. option.yAxis[0].name = "变化面积/km²";
  166. option.series = [];
  167. v.yData.forEach((item, k) => {
  168. let o = cloneDeep(seriesItem); // JSON.parse(JSON.stringify(seriesItem));
  169. o.name = v.xData[k];
  170. o.data = item;
  171. // alldata = [...alldata, ...o.data];
  172. // if (v.interval) {
  173. // option.xAxis.axisLabel.interval = 0;
  174. // option.xAxis.axisLabel.rotate = 0;
  175. // }
  176. option.series.push(o);
  177. });
  178. this.myChart.resize();
  179. this.myChart.setOption(option);
  180. this.myChart.on("click", function (params) {
  181. _this.$emit("echartClick", params.name, { color: params.color });
  182. });
  183. },
  184. },
  185. mounted() {
  186. // this.setOptions();
  187. },
  188. };
  189. </script>
  190. <style lang="scss" scoped>
  191. .bar_echart {
  192. width: 100%;
  193. height: 100%;
  194. }
  195. </style>