瀏覽代碼

fix;修复相关性图表0轴的数据

Roc 2 年之前
父節點
當前提交
6e62bf5641
共有 1 個文件被更改,包括 40 次插入38 次删除
  1. 40 38
      services/data/correlation/chart_info.go

+ 40 - 38
services/data/correlation/chart_info.go

@@ -202,6 +202,10 @@ func GetChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB *models.ChartEdbInfo
 		}
 	}
 
+	// 计算窗口,不包含第一天
+	startDateTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
+	startDate = startDateTime.AddDate(0, 0, 1).Format(utils.FormatDate)
+
 	//// 2023-03-02 时间序列始终以指标B为基准, 始终是A进行平移
 	//baseEdbInfo := edbInfoMappingB
 	//changeEdbInfo := edbInfoMappingA
@@ -221,19 +225,6 @@ func GetChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB *models.ChartEdbInfo
 		return
 	}
 
-	// 移除多余的一组数据(开始日期的数据不能用作计算)
-	{
-		divDay, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, endDate, startDate)
-		if tmpErr != nil {
-			err = tmpErr
-			return
-		}
-		lenAData := len(aDataList)
-		if lenAData > divDay {
-			aDataList = aDataList[lenAData-divDay:]
-		}
-	}
-
 	// 获取变频指标所有日期的值, 插值法完善数据
 	bDataList := make([]*models.EdbDataList, 0)
 	switch changeEdbInfo.EdbInfoCategoryType {
@@ -254,6 +245,7 @@ func GetChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB *models.ChartEdbInfo
 
 	// 2023-03-17 时间序列始终以指标A为基准, 始终是B进行平移
 	baseDataList := make([]*models.EdbDataList, 0)
+	baseDataMap := make(map[string]float64)
 	changeDataList := make([]*models.EdbDataList, 0)
 	changeDataMap := make(map[string]float64)
 
@@ -270,8 +262,7 @@ func GetChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB *models.ChartEdbInfo
 
 		// 如果A指标是高频,那么就需要对B指标进行升频
 		if frequencyIntMap[edbInfoMappingA.Frequency] < frequencyIntMap[edbInfoMappingB.Frequency] {
-			aDataMap := make(map[string]float64)
-			tmpNewChangeDataList, e := HandleDataByLinearRegression(aDataList, aDataMap)
+			tmpNewChangeDataList, e := HandleDataByLinearRegression(aDataList, baseDataMap)
 			if e != nil {
 				err = fmt.Errorf("获取变频指标插值法Map失败, Err: %s", e.Error())
 				return
@@ -279,6 +270,9 @@ func GetChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB *models.ChartEdbInfo
 			baseDataList = tmpNewChangeDataList
 		} else {
 			baseDataList = aDataList
+			for _, v := range baseDataList {
+				baseDataMap[v.DataTime] = v.Value
+			}
 		}
 		// 如果B指标是高频,那么就需要对A指标进行升频
 		if frequencyIntMap[edbInfoMappingA.Frequency] > frequencyIntMap[edbInfoMappingB.Frequency] {
@@ -290,10 +284,11 @@ func GetChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB *models.ChartEdbInfo
 			changeDataList = tmpNewChangeDataList
 		} else {
 			changeDataList = bDataList
-			for _, v := range bDataList {
+			for _, v := range changeDataList {
 				changeDataMap[v.DataTime] = v.Value
 			}
 		}
+
 	}
 
 	// 计算不领先也不滞后时的相关系数
@@ -303,15 +298,22 @@ func GetChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB *models.ChartEdbInfo
 		baseDataTimeArr = append(baseDataTimeArr, baseDataList[i].DataTime)
 		baseCalculateData = append(baseCalculateData, baseDataList[i].Value)
 	}
+
+	zeroBaseData := make([]float64, 0)
 	zeroCalculateData := make([]float64, 0)
 	for i := range baseDataTimeArr {
-		zeroCalculateData = append(zeroCalculateData, changeDataMap[baseDataTimeArr[i]])
+		tmpBaseVal, ok1 := baseDataMap[baseDataTimeArr[i]]
+		tmpCalculateVal, ok2 := changeDataMap[baseDataTimeArr[i]]
+		if ok1 && ok2 {
+			zeroBaseData = append(zeroBaseData, tmpBaseVal)
+			zeroCalculateData = append(zeroCalculateData, tmpCalculateVal)
+		}
 	}
-	if len(baseCalculateData) != len(zeroCalculateData) {
+	if len(zeroBaseData) != len(zeroCalculateData) {
 		err = fmt.Errorf("相关系数两组序列元素数不一致, %d-%d", len(baseCalculateData), len(zeroCalculateData))
 		return
 	}
-	zeroRatio := utils.CalculateCorrelationByIntArr(baseCalculateData, zeroCalculateData)
+	zeroRatio := utils.CalculateCorrelationByIntArr(zeroBaseData, zeroCalculateData)
 	if leadValue == 0 {
 		yData = append(yData, zeroRatio)
 	}
@@ -341,12 +343,14 @@ func GetChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB *models.ChartEdbInfo
 				}
 			}
 			if len(yCalculateData) <= 0 {
-				// 领先滞后后,没有可以计算的数据了,那么退出当前循环
+				//err = fmt.Errorf("领先滞后相关系数两组序列元素数不一致, %d-%d", len(baseCalculateData), len(yCalculateData))
+				//return
+				// 领先滞后后,没有可以计算的数据了
 				continue
 			}
 
 			// 公式计算出领先/滞后频度对应点的相关性系数
-			ratio := utils.CalculateCorrelationByIntArr(baseCalculateData, yCalculateData)
+			ratio := utils.CalculateCorrelationByIntArr(xCalculateData, yCalculateData)
 			yData = append(yData, ratio)
 		}
 	}
@@ -366,6 +370,10 @@ func GetRollingCorrelationChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB *m
 	xDateTimeValue = make([]string, 0)
 	yData := make([]float64, 0)
 
+	// 计算窗口,不包含第一天
+	startDateTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
+	startDate = startDateTime.AddDate(0, 0, 1).Format(utils.FormatDate)
+
 	baseEdbInfo := edbInfoMappingA
 	changeEdbInfo := edbInfoMappingB
 
@@ -381,19 +389,6 @@ func GetRollingCorrelationChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB *m
 		return
 	}
 
-	// 移除多余的一组数据(开始日期的数据不能用作计算)
-	{
-		divDay, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, endDate, startDate)
-		if tmpErr != nil {
-			err = tmpErr
-			return
-		}
-		lenAData := len(aDataList)
-		if lenAData > divDay {
-			aDataList = aDataList[lenAData-divDay:]
-		}
-	}
-
 	// 获取变频指标所有日期的值, 插值法完善数据
 	bDataList := make([]*models.EdbDataList, 0)
 	switch changeEdbInfo.EdbInfoCategoryType {
@@ -422,7 +417,7 @@ func GetRollingCorrelationChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB *m
 		}
 		//baseDataList = tmpNewChangeDataList
 	}
-	// A指标不管三七二十一,先变个频再说
+	// B指标不管三七二十一,先变个频再说
 	{
 		tmpNewChangeDataList, e := HandleDataByLinearRegression(bDataList, changeDataMap)
 		if e != nil {
@@ -451,8 +446,15 @@ func GetRollingCorrelationChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB *m
 			// 取出对应的基准日期的值
 			for i := 0; i < calculateDay; i++ {
 				iDay := currDay.AddDate(0, 0, i).Format(utils.FormatDate)
-				baseCalculateData = append(baseCalculateData, baseDataMap[iDay])
-				yCalculateData = append(yCalculateData, changeDataMap[iDay])
+
+				tmpBaseValue, ok1 := baseDataMap[iDay]
+				tmpChangeValue, ok2 := changeDataMap[iDay]
+				if ok1 && ok2 {
+					baseCalculateData = append(baseCalculateData, tmpBaseValue)
+					yCalculateData = append(yCalculateData, tmpChangeValue)
+				} else {
+					continue
+				}
 			}
 
 			// 公式计算出领先/滞后频度对应点的相关性系数
@@ -461,7 +463,7 @@ func GetRollingCorrelationChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB *m
 				ratio = utils.CalculateCorrelationByIntArr(baseCalculateData, yCalculateData)
 			}
 			yData = append(yData, ratio)
-			xDateTimeValue = append(xDateTimeValue, currDay.AddDate(0, 0, calculateDay).Format(utils.FormatDate))
+			xDateTimeValue = append(xDateTimeValue, currDay.AddDate(0, 0, calculateDay-1).Format(utils.FormatDate))
 		}
 	}