Browse Source

根据系列里的指标值进行排序

xyxie 8 months ago
parent
commit
ea8bbeb6e9
2 changed files with 45 additions and 7 deletions
  1. 32 0
      models/data_manage/chart_series.go
  2. 13 7
      services/data/chart_extra_config.go

+ 32 - 0
models/data_manage/chart_series.go

@@ -340,3 +340,35 @@ func CopyChartSeriesAndEdbMapping(seriesList []*ChartSeries, seriesEdbInfoList [
 	}
 	return
 }
+
+type ChartSectionSeriesValSortAsc []ChartSectionSeriesValSort
+type ChartSectionSeriesValSortDesc []ChartSectionSeriesValSort
+
+type ChartSectionSeriesValSort struct {
+	Index int
+	Value float64
+}
+
+func (s ChartSectionSeriesValSortAsc) Len() int {
+	return len(s)
+}
+
+func (s ChartSectionSeriesValSortAsc) Less(i, j int) bool {
+	return s[i].Value < s[j].Value // 升序排序,如果想要降序则改为 s[i].Value > s[j].Value
+}
+
+func (s ChartSectionSeriesValSortAsc) Swap(i, j int) {
+	s[i], s[j] = s[j], s[i]
+}
+
+func (s ChartSectionSeriesValSortDesc) Len() int {
+	return len(s)
+}
+
+func (s ChartSectionSeriesValSortDesc) Less(i, j int) bool {
+	return s[i].Value > s[j].Value // 升序排序,如果想要降序则改为 s[i].Value > s[j].Value
+}
+
+func (s ChartSectionSeriesValSortDesc) Swap(i, j int) {
+	s[i], s[j] = s[j], s[i]
+}

+ 13 - 7
services/data/chart_extra_config.go

@@ -901,15 +901,21 @@ func SortChartSeriesDataSet(baseName string, baseDataList []float64, baseSeriesN
 
 	}
 	length := len(indices)
-	// 根据Group1的数据进行排序,asc决定是升序还是降序
+	baseDataSortList := make([]data_manage.ChartSectionSeriesValSort, length)
+	for i, value := range baseDataList {
+		if i < length {
+			baseDataSortList[i] = data_manage.ChartSectionSeriesValSort{Index: i, Value: value}
+		}
+	}
 	if asc == 1 {
-		sort.Slice(indices, func(i, j int) bool {
-			return baseDataList[i] < baseDataList[j]
-		})
+		// 使用sort.Sort进行排序
+		sort.Sort(data_manage.ChartSectionSeriesValSortAsc(baseDataSortList))
 	} else {
-		sort.Slice(indices, func(i, j int) bool {
-			return baseDataList[i] > baseDataList[j]
-		})
+		sort.Sort(data_manage.ChartSectionSeriesValSortDesc(baseDataSortList))
+	}
+
+	for k, v := range baseDataSortList {
+		indices[k] = v.Index
 	}
 
 	for i := range newIndices {