Kaynağa Gözat

柱状图取数调整

chenw 7 yıl önce
ebeveyn
işleme
60a3eb2e13

+ 47 - 4
bi-server/src/main/java/com/usoftchina/bi/server/dao/chart/ShowChartsMapper.java

@@ -29,10 +29,16 @@ public interface ShowChartsMapper {
 
     //x轴数据
     @Select("select ${dataType}(${yAxisName}) from ${tableName} where ${xColumn} = #{xdata} ${screen}")
-    String getValues(@Param("yAxisName") String yAxisName, @Param("xColumn") String xColumn,
+    String getXValue(@Param("yAxisName") String yAxisName, @Param("xColumn") String xColumn,
                      @Param("tableName") String tableName, @Param("dataType") String dataType,
                      @Param("xdata") String xdata, @Param("screen") String screen);
 
+    //x轴数据 -> 优化后
+    @Select("select ${dataType}(${yAxisName}) from ${tableName} where 1=1 ${screen} GROUP BY ${xColumn}")
+    List<Double> getValues(@Param("yAxisName") String yAxisName, @Param("xColumn") String xColumn,
+                    @Param("tableName") String tableName, @Param("dataType") String dataType,
+                    @Param("screen") String screen);
+
     //x轴为空时的数据
     @Select("select ${dataType}(${yAxisName}) from ${tableName} where ${xColumn} is null ${screen}")
     String getValuesIsNull(@Param("yAxisName") String yAxisName, @Param("xColumn") String xColumn,
@@ -42,7 +48,7 @@ public interface ShowChartsMapper {
 
 
     //查询分组系列
-    @Select("select ${groupsBy} from ${tableName} where 1=1 ${screen} and rownum<=#{count} group by ${groupsBy}")
+    @Select("SELECT * FROM (select ${groupsBy} from ${tableName} where ${groupsBy} is not null ${screen} group by ${groupsBy}) WHERE rownum<=#{count}")
     List<String> getGroups(@Param("groupsBy") String groupsBy, @Param("tableName") String tableName, @Param("screen") String screen, @Param("count") int count);
 
     //查询分组系列个数
@@ -57,6 +63,10 @@ public interface ShowChartsMapper {
                           @Param("xColumn") String xColumn,
                           @Param("screen") String screen);
 
+    //查询分组的值(柱状图)->优化后
+    @Select("select ${dataType}(${yAxisName}) from ${tableName} where ${groupsColumn} and ${xColumn} ${screen}")
+    List<Double> getGroupsValues(@Param("dataType") String dataType, @Param("yAxisName") String yAxisName, @Param("tableName") String tableName,
+                                 @Param("groupsColumn") String groupsColumn, @Param("xColumn") String xColumn, @Param("screen") String screen);
 
     //时间类型的X轴
     @Select("select distinct to_char(${xAxisName},${timeType})time from ${tableName} ${screen} ORDER by to_char(${xAxisName},${timeType}) ASC")
@@ -70,7 +80,7 @@ public interface ShowChartsMapper {
 
     //半年
     @Select("select distinct case when to_char(${xAxisName},'MM')<=6 then to_char(${xAxisName},'YYYY')|| '-H1'" +
-            "else to_char(${xAxisName},'YYYY')||'-H2' end time from ${tableName} ${screen} order by time")
+            "else to_char(${xAxisName},'YYYY')||'-H2' end time from ${tableName} ${screen} and ${xAxisName} is not null order by time")
     List<String> getTimeYear(@Param("xAxisName") String xAxisName, @Param("tableName") String tableName, @Param("screen") String screen);
     
     //无分组时间类型值
@@ -86,12 +96,33 @@ public interface ShowChartsMapper {
                                  @Param("tableName") String tableName, @Param("xAxisName") String xAxisName,
                                  @Param("screen") String screen);
 
+    //无分组时间类型值(柱状图)->优化后
+    @Select("select ${dataType}(${yAxisName}) from ${tableName} WHERE 1=1 ${screen} GROUP BY (to_char(${xAxisName},${timeType})) ORDER BY (to_char(${xAxisName},${timeType}))")
+    List<Double> getHistogramTimeValue(@Param("dataType") String dataType, @Param("yAxisName") String yAxisName,
+                                       @Param("tableName") String tableName, @Param("xAxisName") String xAxisName,
+                                       @Param("timeType") String timeType, @Param("screen") String screen);
+
     //无分组半年时间值
     @Select("select ${dataType}(${yAxisName}) from ${tableName} where TO_CHAR(${xAxisName},'YYYY-MM') between #{firstIndex} and #{afterIndex} ${screen}")
     String getTimeValueYear(@Param("dataType") String dataType, @Param("yAxisName") String yAxisName, @Param("tableName") String tableName,
                             @Param("xAxisName") String xAxisName, @Param("firstIndex") String firstIndex,
                             @Param("afterIndex") String afterIndex, @Param("screen") String screen);
 
+    /**
+     * 无分组半年时间值(柱状图)
+     * @param dataType
+     * @param yAxisName
+     * @param tableName
+     * @param xAxisName
+     * @param firstIndex
+     * @param afterIndex
+     * @param screen
+     * @return
+     */
+    @Select("select ${dataType}(${yAxisName}) from ${tableName} where TO_CHAR(${xAxisName},'YYYY-MM') between #{firstIndex} and #{afterIndex} ${screen}")
+    Double getHistogramTimeValueYear(@Param("dataType") String dataType, @Param("yAxisName") String yAxisName, @Param("tableName") String tableName,
+                                     @Param("xAxisName") String xAxisName, @Param("firstIndex") String firstIndex,
+                                     @Param("afterIndex") String afterIndex, @Param("screen") String screen);
     //时间为空时间值
     @Select("select ${dataType}(${yAxisName}) from ${tableName} where ${xAxisName} is null ${screen}")
     String getTimeIsNull(@Param("dataType") String dataType, @Param("yAxisName") String yAxisName, @Param("tableName") String tableName,
@@ -105,13 +136,19 @@ public interface ShowChartsMapper {
                           @Param("xColumn") String xColumn, @Param("timeType") String timeType, @Param("xColumnIndex") String xColumnIndex
                            );
 
-    //查询时间类型分组的值->优化后
+    //查询时间类型分组的值->优化后(折线图)
     @Select("select ${dataType}(${yAxisName}) as \"value\",TO_CHAR(${xColumn},${timeType}) as \"date\" from ${tableName} where ${groupsColumn}=#{groupsIndex} GROUP BY TO_CHAR(${xColumn},${timeType}) ")
     List<LineSeriesMap> getGroupValuesTime(@Param("dataType") String dataType, @Param("yAxisName") String yAxisName, @Param("tableName") String tableName,
                               @Param("groupsColumn") String groupsColumn, @Param("groupsIndex") String groupsIndex,
                               @Param("xColumn") String xColumn, @Param("timeType") String timeType
     );
 
+    //查询时间类型分组的值->优化后(柱状图)
+    @Select("select ${dataType}(${yAxisName}) from ${tableName} where ${groupsColumn}=#{groupsIndex} GROUP BY TO_CHAR(${xColumn},${timeType}) GROUP BY TO_CHAR(${xColumn},${timeType})")
+    List<Double> getHistogramTimeValueGroupValuesTime(@Param("dataType") String dataType, @Param("yAxisName") String yAxisName, @Param("tableName") String tableName,
+                                                      @Param("groupsColumn") String groupsColumn, @Param("groupsIndex") String groupsIndex,
+                                                      @Param("xColumn") String xColumn, @Param("timeType") String timeType);
+
     //查询时间类型分组的值->优化后
     @Select("select ${dataType}(${yAxisName}) as \"value\",TO_CHAR(${xColumn},${timeType}) as \"date\",${groupsColumn} as \"group\" from ${tableName} where ${groupsColumn} IN (SELECT ${groupsColumn} FROM ${tableName} GROUP BY ${groupsColumn}) GROUP BY TO_CHAR(${xColumn},${timeType}),${groupsColumn} ")
     List<LineSeriesGroupMap> getGroupsValuesTime(@Param("dataType") String dataType, @Param("yAxisName") String yAxisName, @Param("tableName") String tableName,
@@ -135,6 +172,12 @@ public interface ShowChartsMapper {
                             @Param("xAxisName") String xAxisName, @Param("firstIndex") String firstIndex, @Param("afterIndex") String afterIndex,
                                 @Param("groupsColumn") String groupsColumn, @Param("groupsIndex") String groupsIndex, @Param("screen") String screen);
 
+    //查询分组的值(时间类型半年)  柱状图
+    //分组半年时间值
+    @Select("select ${dataType}(${yAxisName}) from ${tableName} where TO_CHAR(${xAxisName},'YYYY-MM') between #{firstIndex} and #{afterIndex} and ${groupsColumn}=#{groupsIndex} ${screen}")
+    Double getHistogramTimeValueHalfYear (@Param("dataType") String dataType, @Param("yAxisName") String yAxisName, @Param("tableName") String tableName,
+                                          @Param("xAxisName") String xAxisName, @Param("firstIndex") String firstIndex, @Param("afterIndex") String afterIndex,
+                                          @Param("groupsColumn") String groupsColumn, @Param("groupsIndex") String groupsIndex, @Param("screen") String screen);
 
     /*
    总体未分组

+ 26 - 33
bi-server/src/main/java/com/usoftchina/bi/server/service/chart/ShowHistogramService.java

@@ -138,26 +138,10 @@ public class ShowHistogramService {
             }
 
             //无分组时Y值
-            Iterator itX = xAxisData.iterator();
-            while (itX.hasNext()){
-                String xdata = (String)itX.next();
-                double valueOne = 0;
-                String valueOnes = null;
-                if ("time".equals(xColumnType)){
-                    valueOnes = timeConverterUtil.getTimeValueConverter(yColumn, xColumn, tableName, yAxisType, xAxisType, xdata, screenToColumnS);
-                }else {
-                    if (xdata == null || "".equals(xdata)){
-                        valueOnes = showChartsMapper.getValuesIsNull(yColumn, xColumn, tableName, yAxisType, screenToColumnS);
-                    }else {
-                        valueOnes = showChartsMapper.getValues(yColumn, xColumn, tableName, yAxisType, xdata, screenToColumnS);
-                    }
-                }
-                if (valueOnes == null || "".equals(valueOnes)){
-                    valueOne = 0;
-                }else {
-                    valueOne = Double.parseDouble(valueOnes);
-                }
-                value.add(valueOne);
+            if ("time".equals(xColumnType)){        //如果是时间类型
+                value = timeConverterUtil.getHistogramTimeValue(yColumn, xColumn, tableName, yAxisType, xAxisType, xAxisData, screenToColumnS);
+            }else {
+                value = showChartsMapper.getValues(yColumn, xColumn, tableName, yAxisType, screenToColumnS);
             }
 
             series.setName(xColumn);
@@ -167,16 +151,16 @@ public class ShowHistogramService {
             //有分组
             if (groupBy.size() != 0){
                 serieses.remove(0);
-                Iterator itGroup = groupBy.iterator();  //分组数
+                Iterator<String> itGroup = groupBy.iterator();  //分组数
 
                 //便利分组
                 while (itGroup.hasNext()){
-                    String groupByName = (String) itGroup.next(); //每个分组的组名
+                    String groupByName = itGroup.next(); //每个分组的组名
                     int counts = showChartsMapper.getGroupsCount(groupByName,tableName, screenToColumnS);
-                    if (counts > 20){
+                    if (counts > histogramConfigInfo.getMaxCount()){
                         chartsDataInfo.setTooMany(true);
                     }
-                    List<String> groupsData = showChartsMapper.getGroups(groupByName,tableName, screenToColumnS, 20); //查询每个分组系列
+                    List<String> groupsData = showChartsMapper.getGroups(groupByName,tableName, screenToColumnS, histogramConfigInfo.getMaxCount()); //查询每个分组系列
                     Iterator itGroupsData = groupsData.iterator();
 
                     //每个分组得到得每个系列
@@ -195,14 +179,13 @@ public class ShowHistogramService {
                             double groupsValueOne = 0;
                             String groupsValueOnes = null;
                             if ("time".equals(xColumnType)){
-                                groupsValueOnes = timeConverterUtil.getGroupTimeConverter(yAxisType, yColumn, tableName, groupByName, xAxisType,
-                                        groupsName, xColumn, xAxisDataOne, screenToColumnS);
+                                groupsValue = timeConverterUtil.getHistogramGroupTime(yAxisType, yColumn, tableName, groupByName, xAxisType,
+                                        groupsName, xColumn, xAxisData, screenToColumnS);
                             }else {
                                 String xColumnKey = "";
                                 if (xAxisDataOne == null || "".equals(xAxisDataOne)){
                                     xColumnKey  = xColumn + " is null";
-                                }else
-                                {
+                                }else {
                                     xColumnKey = xColumn + " = '" +xAxisDataOne +"'";
                                 }
 
@@ -212,6 +195,7 @@ public class ShowHistogramService {
                                 }else {
                                     groupByKey = groupByName + " = '" +groupsName +"'";
                                 }
+                                //groupsValue = showChartsMapper.getGroupsValue(yAxisType, yColumn, tableName, groupByKey, xColumnKey, screenToColumnS);
                                 groupsValueOnes = showChartsMapper.getGroupsValue(yAxisType, yColumn, tableName, groupByKey, xColumnKey, screenToColumnS);
                             }
                             if ("".equals(groupsValueOnes) || groupsValueOnes == null){
@@ -222,6 +206,15 @@ public class ShowHistogramService {
                             groupsValue.add(groupsValueOne);
                         }
 
+                        /*if ("time".equals(xColumnType)){
+                            groupsValue = timeConverterUtil.getHistogramGroupTime(yAxisType, yColumn, tableName, groupByName, xAxisType,
+                                    groupsName, xColumn, xAxisData, screenToColumnS);
+                        }else {
+                            String xColumnKey = xColumn;
+                            String groupByKey =groupByName + " = '" +groupsName +"'";
+                            groupsValue = showChartsMapper.getGroupsValues(yAxisType, yColumn, tableName, groupByKey, xColumnKey, screenToColumnS);
+                        }*/
+
                         ne.setValue(groupsValue);
                         serieses.add(ne);
                     }
@@ -230,11 +223,11 @@ public class ShowHistogramService {
 
             chartsDataInfo.setSerieses(serieses);
         }catch (Exception e){
-        e.printStackTrace();
-        return new RepEntity(RepCode.erro);
-    }finally {
-        DynamicDataSourceContextHolder.clearDataSourceType();
-    }
+            e.printStackTrace();
+            return new RepEntity(RepCode.erro.getCode(), e.getCause().getMessage(), null);
+        }finally {
+            DynamicDataSourceContextHolder.clearDataSourceType();
+        }
 
         return new RepEntity(RepCode.success, chartsDataInfo);
     }

+ 2 - 2
bi-server/src/main/java/com/usoftchina/bi/server/service/chart/ShowPieService.java

@@ -145,7 +145,7 @@ public class ShowPieService {
                 if (xdata == null || "".equals(xdata)){
                     valueOnes = showChartsMapper.getValuesIsNull(yColumn, xColumn, tableName, yAxisType, screenToColumnS);
                 }else {
-                    valueOnes = showChartsMapper.getValues(yColumn, xColumn, tableName, yAxisType, xdata, screenToColumnS);
+                    valueOnes = showChartsMapper.getXValue(yColumn, xColumn, tableName, yAxisType, xdata, screenToColumnS);
                 }
             }
             if (valueOnes == null || "".equals(valueOnes)){
@@ -165,7 +165,7 @@ public class ShowPieService {
         chartsDataInfo.setSerieses(serieses);
         }catch (Exception e){
             e.printStackTrace();
-            return new RepEntity(RepCode.erro);
+            return new RepEntity(RepCode.erro.getCode(), e.getCause().getMessage(), null);
         }finally {
             DynamicDataSourceContextHolder.clearDataSourceType();
         }

+ 2 - 2
bi-server/src/main/java/com/usoftchina/bi/server/service/chart/ShowScatterService.java

@@ -142,7 +142,7 @@ public class ShowScatterService {
                 if (xdata == null || "".equals(xdata)){
                     valueOnes = showChartsMapper.getValuesIsNull(yColumn, xColumn, tableName, yAxisType, screenToColumnS);
                 }else {
-                    valueOnes = showChartsMapper.getValues(yColumn, xColumn, tableName, yAxisType, xdata ,screenToColumnS);
+                    valueOnes = showChartsMapper.getXValue(yColumn, xColumn, tableName, yAxisType, xdata ,screenToColumnS);
                 }
             }
             if (valueOnes == null || "".equals(valueOnes)){
@@ -233,7 +233,7 @@ public class ShowScatterService {
         chartsDataInfo.setSerieses(serieses);
         }catch (Exception e){
             e.printStackTrace();
-            return new RepEntity(RepCode.erro);
+            return new RepEntity(RepCode.erro.getCode(), e.getCause().getMessage(), null);
         }finally {
             DynamicDataSourceContextHolder.clearDataSourceType();
         }

+ 91 - 0
bi-server/src/main/java/com/usoftchina/bi/server/service/chart/TimeConverterUtil.java

@@ -154,6 +154,51 @@ public class TimeConverterUtil {
         return value;
     }
 
+    /**
+     * 无分组时间值(柱状图)
+     * @param yColumn
+     * @param xColumn
+     * @param tableName
+     * @param dataType
+     * @param timeType
+     * @param xAxisData
+     * @param screen
+     * @return
+     */
+    public List<Double> getHistogramTimeValue(String yColumn, String xColumn, String tableName, String dataType, String timeType, List<String> xAxisData, String screen){
+        String timeGroup = "'YYYY-MM-DD'";
+        List<Double> result = new ArrayList<>();
+        Double value = null;
+        //判断时间类型
+        if ("halfYear".equals(timeType)) {
+            for(String xData : xAxisData) {
+                String firstIndex = null;
+                String afterIndex = null;
+                String[] str = xData.split("-");
+                if ("H1".equals(str[1])) {
+                    firstIndex = str[0] + "-01";
+                    afterIndex = str[0] + "-06";
+                } else {
+                    firstIndex = str[0] + "-07";
+                    afterIndex = str[0] + "-12";
+                }
+                value = showChartsMapper.getHistogramTimeValueYear(dataType, yColumn, tableName, xColumn, firstIndex, afterIndex, screen);
+                result.add(value);
+            }
+            return result;
+        } else if ("year".equals(timeType)) {
+            timeGroup = "'YYYY'";
+        } else if ("month".equals(timeType)) {
+            timeGroup = "'YYYY-MM'";
+        } else if ("day".equals(timeType) || "".equals(timeType)) {
+        } else if ("quarter".equals(timeType)) {
+            timeGroup = "'YYYY-Q'";
+        } else if ("week".equals(timeType)) {
+            timeGroup = "'YYYY-WW'";
+        }
+        return showChartsMapper.getHistogramTimeValue(dataType, yColumn, tableName, xColumn, timeGroup, screen);
+    }
+
     //有分组时间类型值处理
     public String getGroupTimeConverter(String dataType, String yColumn, String tableName, String groupByName, String timeType,
                                         String groupsName, String xColumn, String xAxisDataOne, String screen){
@@ -200,4 +245,50 @@ public class TimeConverterUtil {
         }
         return value;
     }
+
+    /**
+     * 有分组时间类型值处理(柱状图)
+     * @param dataType
+     * @param yColumn
+     * @param tableName
+     * @param groupByName
+     * @param timeType
+     * @param groupsName
+     * @param xColumn
+     * @param xAxisData
+     * @param screen
+     * @return
+     */
+    public List<Double> getHistogramGroupTime(String dataType, String yColumn, String tableName, String groupByName, String timeType,
+                                        String groupsName, String xColumn, List<String> xAxisData, String screen){
+        String timeGroup = "'YYYY-MM-DD'";
+        Double value = null;
+        List<Double> result = new ArrayList<>();
+        if ("halfYear".equals(timeType)) {
+            for (String xAxisDataOne : xAxisData) {
+                String firstIndex = null;
+                String afterIndex = null;
+                String[] str = xAxisDataOne.split("-");
+                if ("上半年".equals(str[1])) {
+                    firstIndex = str[0] + "-01";
+                    afterIndex = str[0] + "-06";
+                } else {
+                    firstIndex = str[0] + "-07";
+                    afterIndex = str[0] + "-12";
+                }
+                value = showChartsMapper.getHistogramTimeValueHalfYear(dataType, yColumn, tableName, xColumn, firstIndex, afterIndex, groupByName, groupsName, screen);
+                result.add(value);
+            }
+            return result;
+        } else if ("year".equals(timeType)) {
+            timeGroup = "'YYYY'";
+        } else if ("month".equals(timeType)) {
+            timeGroup = "'YYYY-MM'";
+        } else if ("quarter".equals(timeType)) {
+            timeGroup = "'YYYY-Q'";
+        } else if ("week".equals(timeType)) {
+            timeGroup = "'YYYY-WW'";
+        }
+        return showChartsMapper.getHistogramTimeValueGroupValuesTime(dataType, yColumn, tableName, groupByName, groupsName, xColumn, timeGroup);
+    }
 }