Browse Source

同比增速增加平均同比增速

littleblue55 1 month ago
parent
commit
2c5b84c68d
1 changed files with 56 additions and 17 deletions
  1. 56 17
      src/views/growthRate/index.vue

+ 56 - 17
src/views/growthRate/index.vue

@@ -191,7 +191,20 @@
         prop="rate"
         label="同比增速"
         width="200"
-      ></el-table-column>
+      >
+        <template v-slot="scope">
+          <span>{{ scope.row.rate.toFixed(2) }}%</span>
+        </template>
+      </el-table-column>
+      <el-table-column
+        prop="rate"
+        label="平均同比增速"
+        width="200"
+      >
+        <template v-slot="scope">
+          <span>{{ scope.row.average.toFixed(2) }}%</span>
+        </template>
+      </el-table-column>
       <el-table-column
         prop="location"
         label="坐落地"
@@ -464,6 +477,7 @@ export default {
           console.log(res);
         })
         .catch((err) => {
+          console.log(err,"测试")
           this.$message.error("请求数据失败");
           return;
         });
@@ -475,14 +489,18 @@ export default {
       }
       const categories = [];
       const seriesData = [];
-      let sortedData = [];
-      sortedData = rawData.map((item) => ({
-        ...item, // 扩展其他属性
-        rate: isNaN(parseFloat(item.rate))
-          ? null
-          : (parseFloat(item.rate) * 100).toFixed(2),
+      // let sortedData = [];
+      // sortedData = rawData.map((item) => ({
+      //   ...item, // 扩展其他属性
+      //   rate: isNaN(parseFloat(item.rate))
+      //     ? null
+      //     : (parseFloat(item.rate) * 100).toFixed(2),
+      // }));
+      let sortedData = rawData.map((item) => ({
+        ...item,
+        rate: isNaN(parseFloat(item.rate)) ? null : parseFloat(item.rate) * 100,
       }));
-      console.log(rawData, sortedData, "sortedData");
+      // console.log(rawData, sortedData, "sortedData");
       if (this.sortActive === "asc") {
         sortedData.sort((a, b) => {
           if (a.rate === null && b.rate === null) return 0;
@@ -507,16 +525,18 @@ export default {
       } else if (this.sortActive === "location") {
         sortedData.sort((a, b) => a.location.localeCompare(b.location));
       }
-      // 处理数据
-      // if (this.sortActive === "asc") {
-      //   sortedData.sort((a, b) => parseFloat(a.rate) - parseFloat(b.rate));
-      // } else if (this.sortActive === "desc") {
-      //   sortedData.sort((a, b) => parseFloat(b.rate) - parseFloat(a.rate));
-      // } else if (this.sortActive === "location") {
-      //   sortedData.sort((a, b) => a.location.localeCompare(b.location));
-      // }
+      const validRates = sortedData
+        .map((item) => item.rate)
+        .filter((rate) => rate !== null && isFinite(rate));
+
+      const average =
+        validRates.length > 0
+          ? validRates.reduce((a, b) => a + b, 0) / validRates.length
+          : null;
 
       sortedData.forEach((item) => {
+        item.average = average;
+        // ietm.rate = item.rate.toFixed(2);
         categories.push(item.enterpriseName);
         if (!item.rate || item.rate === "Infinity") {
           seriesData.push({
@@ -527,13 +547,30 @@ export default {
           });
         } else {
           seriesData.push({
-            value: item.rate,
+            value: item.rate.toFixed(2),
             itemStyle: {
               color: item.rate >= 0 ? "#5470C6" : "#91CC75",
             },
           });
         }
       });
+      const markLine =
+        average !== null
+          ? {
+              markLine: {
+                data: [{ yAxis: average, name: "平均值" }],
+                label: {
+                  formatter: `平均值: ${average.toFixed(2)}%`,
+                  position: "right",
+                },
+                lineStyle: {
+                  type: "dashed",
+                  color: "#ff4500",
+                },
+                symbol: 'none'
+              },
+            }
+          : {};
       that.tableData = sortedData;
       // console.log(sortedData);
       const values = seriesData.map((s) => s.value || 0);
@@ -593,7 +630,9 @@ export default {
                 return params.value === null ? "异常" : params.value + "%";
               },
             },
+            ...markLine
           },
+          
         ],
         dataZoom: [
           {