浏览代码

fix:补充无数据的日期数据(平均值)

Roc 2 年之前
父节点
当前提交
f67380fe8f
共有 3 个文件被更改,包括 54 次插入12 次删除
  1. 6 5
      models/response/chart_info/chart_info.go
  2. 5 1
      services/chart/chart_info.go
  3. 43 6
      services/chart/future_good/chart_info.go

+ 6 - 5
models/response/chart_info/chart_info.go

@@ -16,9 +16,10 @@ type ChartInfoDetailResp struct {
 
 // YData 柱方图的y轴数据
 type YData struct {
-	Date   string    `description:"数据日期"`
-	Color  string    `description:"数据颜色"`
-	Value  []float64 `description:"每个指标的值"`
-	Name   string    `description:"别名"`
-	NameEn string    `description:"英文别名"`
+	Date          string    `description:"数据日期"`
+	Color         string    `description:"数据颜色"`
+	Name          string    `description:"别名"`
+	NameEn        string    `description:"英文别名"`
+	Value         []float64 `description:"每个指标的值"`
+	NoDataEdbList []int     `description:"没有数据的指标列表"`
 }

+ 5 - 1
services/chart/chart_info.go

@@ -855,9 +855,13 @@ func BarChartData(mappingList []*chartEdbMappingModel.ChartEdbInfoMapping, edbDa
 				findDataList = append(findDataList, 0)
 			}
 		}
+		yDate := "0000-00-00"
+		if !maxDate.IsZero() {
+			yDate = maxDate.Format(utils.FormatDate)
+		}
 
 		yDataList = append(yDataList, chart_info.YData{
-			Date:  maxDate.Format(utils.FormatDate),
+			Date:  yDate,
 			Value: findDataList,
 			Color: barChartInfoDate.Color,
 			Name:  barChartInfoDate.Name,

+ 43 - 6
services/chart/future_good/chart_info.go

@@ -3,6 +3,7 @@ package future_good
 import (
 	"errors"
 	"fmt"
+	"github.com/shopspring/decimal"
 	"hongze/hongze_yb/models/request"
 	"hongze/hongze_yb/models/response/chart_info"
 	chartEdbMappingModel "hongze/hongze_yb/models/tables/chart_edb_mapping"
@@ -367,13 +368,17 @@ func BarChartData(mappingList []*chartEdbMappingModel.ChartEdbInfoMappingList, e
 	for _, barChartInfoDate := range barChartInfoDateList {
 		var maxDate time.Time
 
-		findDataList := make([]float64, 0) // 当前日期的数据值
+		findDataList := make([]float64, 0)  // 当前日期的数据值
+		noDataIdList := make([]int, 0)      // 没有数据的指标id
+		noDataIdMap := make(map[int]int, 0) // 没有数据的指标map
 		for _, edbInfoId := range edbIdList {
 			findDate := barChartInfoDate.Date     //需要的日期值
 			dataList := edbDataListMap[edbInfoId] //指标的所有数据值
 			if len(dataList) <= 0 {
 				// 没有数据的指标id
 				findDataList = append(findDataList, 0)
+				noDataIdList = append(noDataIdList, edbInfoId)
+				noDataIdMap[edbInfoId] = edbInfoId
 				continue
 			}
 			switch barChartInfoDate.Type {
@@ -436,6 +441,8 @@ func BarChartData(mappingList []*chartEdbMappingModel.ChartEdbInfoMappingList, e
 				findDataList = append(findDataList, tmpValue)
 			} else {
 				findDataList = append(findDataList, 0)
+				noDataIdList = append(noDataIdList, edbInfoId)
+				noDataIdMap[edbInfoId] = edbInfoId
 			}
 		}
 		yName := barChartInfoDate.Name
@@ -457,12 +464,42 @@ func BarChartData(mappingList []*chartEdbMappingModel.ChartEdbInfoMappingList, e
 		if !maxDate.IsZero() {
 			yDate = maxDate.Format(utils.FormatDate)
 		}
+
+		// 数据处理,将没有数据的下标,赋值平均值
+		{
+			hasDataIndexList := make([]int, 0)
+			for dataK, edbInfoId := range edbIdList {
+				if _, ok := noDataIdMap[edbInfoId]; !ok { // 如果是没有数据的指标id
+					hasDataIndexList = append(hasDataIndexList, dataK)
+				}
+			}
+			lenHasDataIndex := len(hasDataIndexList)
+			if lenHasDataIndex > 0 {
+				for lenHasDataI := 1; lenHasDataI < lenHasDataIndex; lenHasDataI++ {
+					perK := hasDataIndexList[lenHasDataI-1] //上一个有数据的指标下标
+					currK := hasDataIndexList[lenHasDataI]  //当前有数据的指标下标
+					preVal := findDataList[perK]            //上一个有数据的坐标的值
+					currVal := findDataList[currK]          //当前有数据的指标的值
+
+					// 环差值
+					hcValDeci := decimal.NewFromFloat(currVal).Sub(decimal.NewFromFloat(preVal)).Div(decimal.NewFromInt(int64(currK - perK)))
+					var tmpI int64
+					// 将两个中间的数据做平均值补全
+					for hcI := perK + 1; hcI < currK; hcI++ {
+						tmpI++
+						findDataList[hcI], _ = decimal.NewFromFloat(preVal).Add(hcValDeci.Mul(decimal.NewFromInt(tmpI))).RoundCeil(4).Float64()
+					}
+				}
+			}
+		}
+
 		yDataList = append(yDataList, chart_info.YData{
-			Date:   yDate,
-			Value:  findDataList,
-			Color:  barChartInfoDate.Color,
-			Name:   yName,
-			NameEn: yNameEn,
+			Date:          yDate,
+			Value:         findDataList,
+			NoDataEdbList: noDataIdList,
+			Color:         barChartInfoDate.Color,
+			Name:          yName,
+			NameEn:        yNameEn,
 		})
 	}