index.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <script setup>
  2. import request from "@/utils/request.js";
  3. // 引入ECharts
  4. import * as echarts from 'echarts';
  5. import { ref, onMounted, watch } from 'vue';
  6. const getData = async () => {
  7. const res = await request.get('/basicData/xcrPayTaxes/of-year-list');
  8. // 处理数据并更新图表
  9. updateChart(res.data.ofYearDetail, 'year');
  10. }
  11. const selectedQuarter = ref('1'); // 默认选择第一个季度
  12. const getQuarterData = async () => {
  13. console.log(selectedQuarter.value);
  14. const res = await request.get('/basicData/xcrPayTaxes/of-quarter-list', {
  15. params: {
  16. quarter: selectedQuarter.value // 使用选中的季度
  17. }
  18. });
  19. // 处理数据并更新图表
  20. updateChart(res.data.ofQuarterDetail, 'quarter');
  21. }
  22. let yearChartInstance = null;
  23. let quarterChartInstance = null;
  24. const updateChart = (data, type) => {
  25. if (type === 'year') {
  26. if (!yearChartInstance) {
  27. yearChartInstance = echarts.init(document.getElementById('yearChart'));
  28. }
  29. } else if (type === 'quarter') {
  30. if (!quarterChartInstance) {
  31. quarterChartInstance = echarts.init(document.getElementById('quarterChart'));
  32. }
  33. }
  34. // 处理数据
  35. const taxpayerNames = data.map(item => item.taxpayerName);
  36. const lastYearTaxes = data.map(item => item[type === 'year' ? 'lastYearTaxesTotal' : 'lastYearOfQuarterTaxesTotal']);
  37. const thisYearTaxes = data.map(item => item[type === 'year' ? 'thisYearTaxesTotal' : 'thisYearOfQuarterTaxesTotal']);
  38. // 设置图表配置
  39. const option = {
  40. title: {
  41. text: type === 'year' ? '年度税收对比' : '季度税收对比'
  42. },
  43. tooltip: {
  44. trigger: 'axis'
  45. },
  46. legend: {
  47. data: ['去年税收', '今年税收']
  48. },
  49. xAxis: {
  50. type: 'value'
  51. },
  52. yAxis: {
  53. type: 'category',
  54. data: taxpayerNames
  55. },
  56. series: [
  57. {
  58. name: '去年税收',
  59. type: 'bar',
  60. data: lastYearTaxes,
  61. },
  62. {
  63. name: '今年税收',
  64. type: 'bar',
  65. data: thisYearTaxes,
  66. }
  67. ],
  68. dataZoom: [{ // 这是一个 dataZoom 组件,可以控制 x 轴或 y 轴的区域缩放
  69. type: 'inside', // 这里设置为 slider 类型,表示使用滑动条形式的 dataZoom 组件
  70. yAxisIndex: 0, // 表示这个 dataZoom 组件控制第一个 y 轴
  71. start: 100, // 数据窗口范围的起始百分比, 表示从10%的位置开始显示
  72. end: 99, // 数据窗口范围的结束百分比, 表示到60%的位置结束
  73. moveOnMouseWheel: true, // 启用滚轮滚动
  74. moveOnMouseMove: false,
  75. zoomOnMouseWheel: false,
  76. }]
  77. };
  78. // 更新图表数据
  79. if (type === 'year') {
  80. yearChartInstance.setOption(option);
  81. } else if (type === 'quarter') {
  82. quarterChartInstance.setOption(option);
  83. }
  84. }
  85. const activeTab = ref('year'); // 添加一个变量来记录当前激活的tab
  86. onMounted(() => {
  87. getData();
  88. getQuarterData();
  89. })
  90. watch(selectedQuarter, () => {
  91. getQuarterData(); // 当季度选择改变时,重新获取数据
  92. })
  93. // 添加tab-change事件监听
  94. const handleTabChange = (tab) => {
  95. if (tab.props.label === '年度税收对比') {
  96. getData();
  97. } else if (tab.props.label === '季度税收对比') {
  98. getQuarterData();
  99. }
  100. }
  101. </script>
  102. <template>
  103. <div>
  104. <el-tabs @tab-change="handleTabChange">
  105. <el-tab-pane label="季度税收对比">
  106. <el-select v-model="selectedQuarter" placeholder="请选择季度">
  107. <el-option label="第一季度" value="1"></el-option>
  108. <el-option label="第二季度" value="2"></el-option>
  109. <el-option label="第三季度" value="3"></el-option>
  110. <el-option label="第四季度" value="4"></el-option>
  111. </el-select>
  112. <div id="quarterChart" style="width: 90vw; height: 90vh;"></div>
  113. </el-tab-pane>
  114. <el-tab-pane label="年度税收对比">
  115. <div id="yearChart" style="width: 90vw; height: 90vh;"></div>
  116. </el-tab-pane>
  117. </el-tabs>
  118. </div>
  119. </template>