瀏覽代碼

fix:无效日期取平均值

Roc 2 年之前
父節點
當前提交
808e453759
共有 2 個文件被更改,包括 53 次插入11 次删除
  1. 6 5
      models/chart.go
  2. 47 6
      services/data/future_good/chart_info.go

+ 6 - 5
models/chart.go

@@ -161,11 +161,12 @@ 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:"没有数据的指标列表"`
 }
 
 // 指标季度数据计算(公历转农历)

+ 47 - 6
services/data/future_good/chart_info.go

@@ -3,6 +3,7 @@ package future_good
 import (
 	"errors"
 	"fmt"
+	"github.com/shopspring/decimal"
 	"hongze/hongze_chart_lib/models"
 	"hongze/hongze_chart_lib/models/data_manage"
 	efuture_good "hongze/hongze_chart_lib/models/data_manage/future_good"
@@ -353,13 +354,17 @@ func BarChartData(mappingList []*models.ChartEdbInfoMapping, edbDataListMap map[
 	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 {
@@ -422,6 +427,8 @@ func BarChartData(mappingList []*models.ChartEdbInfoMapping, edbDataListMap map[
 				findDataList = append(findDataList, tmpValue)
 			} else {
 				findDataList = append(findDataList, 0)
+				noDataIdList = append(noDataIdList, edbInfoId)
+				noDataIdMap[edbInfoId] = edbInfoId
 			}
 		}
 		yName := barChartInfoDate.Name
@@ -439,12 +446,46 @@ func BarChartData(mappingList []*models.ChartEdbInfoMapping, edbDataListMap map[
 				yNameEn = maxDate.Format(utils.FormatDate)
 			}
 		}
+		yDate := "0000-00-00"
+		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, models.YData{
-			Date:   maxDate.Format(utils.FormatDate),
-			Value:  findDataList,
-			Color:  barChartInfoDate.Color,
-			Name:   yName,
-			NameEn: yNameEn,
+			Date:          yDate,
+			Value:         findDataList,
+			NoDataEdbList: noDataIdList,
+			Color:         barChartInfoDate.Color,
+			Name:          yName,
+			NameEn:        yNameEn,
 		})
 	}