农经大屏
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

245 line
5.3 KiB

  1. import * as echarts from 'echarts';
  2. import elementResizeDetectorMaker from 'element-resize-detector';
  3. export default {
  4. props: {
  5. id: {
  6. type: String,
  7. default: 'bar'
  8. },
  9. data: {
  10. type: Array,
  11. default: function () {
  12. return [
  13. {
  14. name: '1月',
  15. value: '10',
  16. data: []
  17. },
  18. {
  19. name: '2月',
  20. value: '19',
  21. data: []
  22. }
  23. ];
  24. }
  25. },
  26. unit: {
  27. type: String,
  28. default: '单位:万元'
  29. },
  30. interval: {
  31. type: Number,
  32. default: null
  33. },
  34. serverName: {
  35. type: String,
  36. default: '面积'
  37. },
  38. color: {
  39. type: Array,
  40. default: function () {
  41. return ['rgba(15, 252, 252, 1)', 'rgba(53, 197, 124, 1)']
  42. }
  43. },
  44. barBorderRadius: {
  45. type: Array,
  46. default: function () {
  47. return [0, 0, 0, 0]
  48. }
  49. }
  50. },
  51. data () {
  52. return {
  53. chart: null,
  54. rotate: 0
  55. };
  56. },
  57. watch: {
  58. data: {
  59. handler: function (val) {
  60. this.$nextTick(function () {
  61. setTimeout(() => {
  62. this.initChart();
  63. }, 2000)
  64. });
  65. },
  66. immediate: true,
  67. deep: true
  68. }
  69. },
  70. create () {
  71. },
  72. mounted () {
  73. // setTimeout(()=>{
  74. // this.initChart();
  75. // },2000)
  76. },
  77. computed: {
  78. },
  79. methods: {
  80. // 设置监听器 页面尺寸变化重新绘制图表
  81. initResizeCallBack () {
  82. const erd = elementResizeDetectorMaker();
  83. erd.listenTo(document.getElementById(this.id), () => {
  84. this.$nextTick(() => {
  85. if (document.getElementById(this.id).offsetWidth > 500){
  86. this.rotate = 60;
  87. }else{
  88. this.rotate = 0;
  89. }
  90. this.chart.resize();
  91. this.chart.setOption({
  92. // 新的配置项,例如:
  93. xAxis: {
  94. axisLabel: {
  95. rotate: this.rotate
  96. },
  97. }
  98. });
  99. });
  100. });
  101. },
  102. initChart () {
  103. this.chart = echarts.init(document.getElementById(this.id));
  104. this.chartSetOption();
  105. },
  106. chartSetOption () {
  107. let xAxisData = [];
  108. let data = [];
  109. this.data[0]?.data.forEach(child =>{
  110. data.push({
  111. name: child.dataNum1,
  112. barWidth: 20,
  113. type: 'bar',
  114. stack: 'Ad',
  115. emphasis: {
  116. focus: 'series'
  117. },
  118. data: []
  119. })
  120. })
  121. this.data.forEach(item => {
  122. xAxisData.push(item.deptName);
  123. item.data.forEach(child =>{
  124. data.forEach(son =>{
  125. if (child.dataNum1 == son.name ){
  126. son.data.push(child.dataNum2)
  127. }
  128. })
  129. })
  130. });
  131. const option = {
  132. color: ["#2195fe","#04e26f","#f7cc3a","#e23ff7","#f77e3f"],
  133. tooltip: {
  134. trigger: "axis",
  135. axisPointer: {
  136. type: "line",
  137. lineStyle: {
  138. opacity: 0,
  139. },
  140. }
  141. },
  142. legend: {
  143. top: '0%',
  144. right: '2%',
  145. textStyle: {
  146. color: 'rgba(210, 238, 255, 1)',
  147. },
  148. },
  149. grid: {
  150. left: "5%",
  151. right: "2%",
  152. bottom: "5%",
  153. top: "15%",
  154. containLabel: true,
  155. z: 22,
  156. },
  157. xAxis: [
  158. {
  159. splitArea: {
  160. show: false,
  161. areaStyle: {
  162. color: ['RGBA(13, 31, 64, 1)']
  163. }
  164. },
  165. splitLine: {
  166. show: false,
  167. lineStyle: {
  168. color: ['rgba(18, 40, 83, 1)'],
  169. width: 100
  170. }
  171. },
  172. type: "category",
  173. gridIndex: 0,
  174. data: xAxisData,//xAxisData
  175. axisTick: {
  176. alignWithLabel: true,
  177. },
  178. axisLine: {
  179. lineStyle: {
  180. color: "#0c3b71",
  181. },
  182. },
  183. axisLabel: {
  184. show: true,
  185. rotate: this.rotate,
  186. color: 'rgba(185, 211, 235, 1)'
  187. },
  188. },
  189. ],
  190. yAxis: [
  191. {
  192. type: "value",
  193. name: this.unit,
  194. nameTextStyle: {
  195. color: 'rgba(185, 211, 235, 1)'
  196. },
  197. axisLabel: {
  198. formatter: "{value}",
  199. textStyle: {
  200. color: "rgba(185, 211, 235, 1)",
  201. },
  202. },
  203. axisLine: {
  204. lineStyle: {
  205. color: "#27b4c2",
  206. },
  207. },
  208. axisTick: {
  209. show: false,
  210. },
  211. splitLine: {
  212. show: true,
  213. lineStyle: {
  214. color: "#11366e",
  215. },
  216. },
  217. },
  218. {
  219. type: "value",
  220. gridIndex: 0,
  221. max: 100,
  222. splitNumber: 12,
  223. splitLine: {
  224. show: false,
  225. },
  226. axisLine: {
  227. show: false,
  228. },
  229. axisTick: {
  230. show: false,
  231. },
  232. axisLabel: {
  233. show: false,
  234. }
  235. },
  236. ],
  237. series: data
  238. };
  239. this.chart.setOption(option);
  240. this.initResizeCallBack();
  241. }
  242. }
  243. };