Browse Source

[Mod 0000] 优化echarts 纵坐标最小值设置范围

hongrunxia 1 week ago
parent
commit
b9294ea75b
1 changed files with 59 additions and 1 deletions
  1. 59 1
      src/components/chart/BarAndLine.vue

+ 59 - 1
src/components/chart/BarAndLine.vue

@@ -65,6 +65,7 @@
       const chartData = props.chartsColumnsType ? getTableHeaderColumns(props.chartsColumnsType) : [];
       let chartsColumns = (props.chartsColumns.length > 0 ? props.chartsColumns : chartData) as ChartColumn[];
       let tempYmax: number[] = [];
+      let tempYmin: number[] = [];
       // let groupedByColumns = {};
       const option = reactive({
         name: '',
@@ -130,8 +131,11 @@
             // console.log('nnn', option.series[index].data);
             // 这里动态计算echarts y轴最大值
             const max = Math.max(...option.series[index].data);
+            const min = Math.min(...option.series[index].data);
             const digitCount = max.toFixed(0).length;
-            let yMax = 0;
+            const minDigitCount = min.toFixed(0).length;
+            let yMax = 0,
+              yMin = 0;
             if (digitCount < 2) {
               if (max < 0.5) {
                 yMax = 1;
@@ -172,9 +176,52 @@
               }
             }
 
+            if (minDigitCount < 2) {
+              if (min > 0.5) {
+                yMin = 0.5;
+              } else if (min > 1.5) {
+                yMin = 1.0;
+              } else if (min > 10) {
+                yMin = 5;
+              } else {
+                yMin = 15;
+              }
+            } else if (minDigitCount < 3) {
+              const n = Number((Number(min.toFixed(0)) / 10).toFixed(0));
+              if (n > 1) {
+                yMin = (n - 1) * 10;
+              } else {
+                yMin = 10;
+              }
+            } else if (digitCount < 4) {
+              const n = Number((Number(min.toFixed(0)) / 100).toFixed(0));
+              if (n > 1) {
+                yMin = (n - 1) * 100;
+              } else {
+                yMin = 100;
+              }
+            } else if (digitCount < 5) {
+              const n = Number((Number(min.toFixed(0)) / 1000).toFixed(0));
+              if (n > 1) {
+                yMin = (n - 1) * 1000;
+              } else {
+                yMin = 1000;
+              }
+            } else if (digitCount < 6) {
+              const n = Number((Number(min.toFixed(0)) / 10000).toFixed(0));
+              if (n > 1) {
+                yMin = (n - 1) * 10000;
+              } else {
+                yMin = 10000;
+              }
+            }
+
             if (yMax != 0 && yMax !== propType?.ymax) {
               propType.ymax = yMax;
             }
+            if (yMin != 0 && yMin !== propType?.yMin) {
+              propType.ymin = yMin;
+            }
             return true;
           });
           // 根据sort分组
@@ -192,17 +239,28 @@
           for (let sortId in groupedBy) {
             const group = groupedBy[sortId];
             let ymax = group[0].ymax;
+            let ymin = group[0].ymin;
             for (let i = 1; i < group.length; i++) {
               if (group[i].ymax > ymax) {
                 ymax = group[i].ymax;
               }
             }
+            for (let i = 1; i < group.length; i++) {
+              if (group[i].ymin < ymin) {
+                ymin = group[i].ymin;
+              }
+            }
             if (!tempYmax[index] || tempYmax[index] != ymax) {
               tempYmax[index] = ymax;
               isFresh = true;
             }
+            if (!tempYmin[index] || tempYmin[index] != ymin) {
+              tempYmin[index] = ymin;
+              isFresh = true;
+            }
             for (let i = 0; i < group.length; i++) {
               group[i].ymax = ymax;
+              group[i].ymin = ymin;
               newChartsColumns.push(group[i]);
             }
             ++index;