BarChart.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <template>
  2. <div id="barChart" ref="barChart"></div>
  3. </template>
  4. <script>
  5. import * as echarts from "echarts";
  6. export default {
  7. name: "barChart",
  8. data() {
  9. return {
  10. barChart: null,
  11. };
  12. },
  13. props: {
  14. categoryData: {
  15. type: Array,
  16. default() {
  17. return [];
  18. },
  19. },
  20. soilValueData: {
  21. type: Array,
  22. default() {
  23. return [];
  24. },
  25. },
  26. waterValueData: {
  27. type: Array,
  28. default() {
  29. return [];
  30. },
  31. },
  32. forestryValueData: {
  33. type: Array,
  34. default() {
  35. return [];
  36. },
  37. },
  38. sourceType: {
  39. type: String,
  40. default: "A",
  41. },
  42. unit: {
  43. type: String,
  44. default: "公顷",
  45. },
  46. },
  47. mounted() {
  48. this.initEchart(
  49. this.categoryData,
  50. this.soilValueData,
  51. this.waterValueData,
  52. this.forestryValueData
  53. );
  54. },
  55. watch: {
  56. sourceType(val) {
  57. // console.log(val, "sourceType");
  58. this.initEchart(
  59. this.categoryData,
  60. this.soilValueData,
  61. this.waterValueData,
  62. this.forestryValueData
  63. );
  64. },
  65. },
  66. methods: {
  67. initEchart(categoryData, soil, water, forestry) {
  68. if (!this.barChart) {
  69. this.barChart = echarts.init(this.$refs.barChart);
  70. }
  71. let option = {
  72. tooltip: {
  73. trigger: "axis",
  74. axisPointer: {
  75. // Use axis to trigger tooltip
  76. type: "shadow", // 'shadow' as default; can also be 'line' or 'shadow'
  77. },
  78. formatter: function (params) {
  79. let relVal = params[0].name;
  80. for (let i = 0; i < params.length; i++) {
  81. if (params[i].value === 0) {
  82. relVal += `<br/>${params[i].marker}${params[i].seriesName} ${params[i].value}`;
  83. } else {
  84. relVal += `<br/>${params[i].marker}${params[i].seriesName} ${params[i].value}公顷`;
  85. }
  86. }
  87. return relVal;
  88. },
  89. },
  90. legend: {
  91. bottom: "0%",
  92. right: "-1%",
  93. orient: "vertical",
  94. textStyle: {
  95. color: "#FFF",
  96. fontSize: "14",
  97. fontFamily: "PingFang SC",
  98. },
  99. },
  100. grid: {
  101. top: "5%",
  102. left: "0",
  103. right: "19%",
  104. bottom: "-5%",
  105. containLabel: true,
  106. },
  107. xAxis: {
  108. type: "value",
  109. show: false,
  110. },
  111. yAxis: {
  112. axisLine: {
  113. lineStyle: {
  114. color: "#FFF",
  115. },
  116. },
  117. // axisLine: "none",
  118. axisTick: "none",
  119. axisLabel: {
  120. // inside:true,
  121. },
  122. type: "category",
  123. data: categoryData,
  124. // data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
  125. },
  126. series: [
  127. {
  128. name: "土地",
  129. type: "bar",
  130. stack: "total",
  131. // label: {
  132. // show: true
  133. // },
  134. emphasis: {
  135. focus: "series",
  136. },
  137. data: soil,
  138. // itemStyle:{
  139. // color:"#f00"
  140. // }
  141. // data: [320, 302, 301, 334, 390, 330, 320],
  142. },
  143. {
  144. name: "水",
  145. type: "bar",
  146. stack: "total",
  147. // label: {
  148. // show: true
  149. // },
  150. emphasis: {
  151. focus: "series",
  152. },
  153. data: water,
  154. // itemStyle:{
  155. // color:"#f00"
  156. // }
  157. // data: [120, 132, 101, 134, 90, 230, 210],
  158. },
  159. {
  160. name: "林地",
  161. type: "bar",
  162. stack: "total",
  163. // label: {
  164. // show: true
  165. // },
  166. emphasis: {
  167. focus: "series",
  168. },
  169. data: forestry,
  170. // itemStyle:{
  171. // color:"#f00"
  172. // }
  173. // data: [220, 182, 191, 234, 290, 330, 182],
  174. },
  175. ],
  176. };
  177. this.barChart.setOption(option);
  178. },
  179. },
  180. };
  181. </script>