chart_extra_config.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. package data
  2. import (
  3. "errors"
  4. "eta/eta_chart_lib/models"
  5. "eta/eta_chart_lib/models/data_manage"
  6. "eta/eta_chart_lib/utils"
  7. "fmt"
  8. "sort"
  9. "strconv"
  10. "time"
  11. )
  12. // GetChartSectionCombineData 截面组合图的数据处理
  13. func GetChartSectionCombineData(chartInfo *models.ChartInfo, mappingList []*models.ChartEdbInfoMapping, edbDataListMap map[int][]*models.EdbDataList, extraConfig models.ChartSectionAllExtraConf) (edbIdList []int, dataListResp models.ChartSectionCombineDataResp, err error) {
  14. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  15. edbDataMap := make(map[int]map[string]float64)
  16. for edbInfoId, edbDataList := range edbDataListMap {
  17. edbDateData := make(map[string]float64)
  18. for _, edbData := range edbDataList {
  19. edbDateData[edbData.DataTime] = edbData.Value
  20. }
  21. edbDataMap[edbInfoId] = edbDateData
  22. }
  23. // edbIdList 指标展示顺序;x轴的指标顺序
  24. edbIdList = make([]int, 0)
  25. edbMappingMap := make(map[int]*models.ChartEdbInfoMapping)
  26. for _, v := range mappingList {
  27. edbIdList = append(edbIdList, v.EdbInfoId)
  28. edbMappingMap[v.EdbInfoId] = v
  29. }
  30. // 确定好截面散点图返回的数据格式
  31. // 获取所有的引用日期设置
  32. dateConfListMap := make(map[string]*models.ChartSectionDateConfItem)
  33. dateConfEdbIds := make([]int, 0)
  34. for _, v := range extraConfig.DateConfList {
  35. if v.EdbInfoId > 0 {
  36. dateConfEdbIds = append(dateConfEdbIds, v.EdbInfoId)
  37. }
  38. dateConfListMap[v.DateConfName] = v
  39. }
  40. // 遍历每个系列
  41. // 遍历每个指标,根据选中的日期,进行日期变换得到最终的日期,根据最终的日期获取对应的值
  42. // 组装数据
  43. baseSeries := new(models.ChartSectionSeriesItem) //y轴的系列
  44. var firstUnit, leftUnit, rightUnit, right2Unit *models.XData
  45. var (
  46. LeftMin float64
  47. LeftMax float64
  48. RightMin float64
  49. RightMax float64
  50. Right2Min float64
  51. Right2Max float64
  52. )
  53. seriesDataListMap := make(map[string][]float64)
  54. seriesNoDataIndexMap := make(map[string][]int)
  55. for _, seriesItem := range extraConfig.SeriesList {
  56. var maxDate time.Time
  57. var minVal, maxVal float64
  58. noDataEdbIndex := make([]int, 0)
  59. dataList := make([]float64, len(seriesItem.EdbInfoList))
  60. for index, edbConf := range seriesItem.EdbInfoList {
  61. edbInfoId := edbConf.EdbInfoId //X轴的指标
  62. edbMappingInfo, ok := edbMappingMap[edbInfoId]
  63. if !ok {
  64. continue
  65. }
  66. seriesItem.EdbInfoList[index].EdbName = edbMappingInfo.EdbName
  67. seriesItem.EdbInfoList[index].EdbNameEn = edbMappingInfo.EdbNameEn
  68. seriesItem.EdbInfoList[index].EdbInfoType = edbMappingInfo.EdbInfoCategoryType
  69. seriesItem.EdbInfoList[index].Unit = edbMappingInfo.Unit
  70. seriesItem.EdbInfoList[index].UnitEn = edbMappingInfo.UnitEn
  71. if index == 0 {
  72. firstUnit = &models.XData{
  73. Name: edbMappingInfo.Unit,
  74. NameEn: edbMappingInfo.UnitEn,
  75. }
  76. }
  77. edbDataList, ok3 := edbDataListMap[edbInfoId]
  78. if !ok3 {
  79. err = fmt.Errorf("指标%d的日期数据不存在", edbInfoId)
  80. return
  81. }
  82. //日期变换处理,判断用指标的最新日期还是,直接获取引用日期
  83. var findDate string
  84. if edbConf.DateConfType == 0 {
  85. if edbInfoId == 0 {
  86. err = fmt.Errorf("请选择指标")
  87. return
  88. }
  89. findDate, err = GetChartSectionSeriesDateByDateChange(edbInfoId, edbDataList, edbConf.DateConf.DateChange, edbConf.DateConf.MoveForward)
  90. if err != nil {
  91. err = fmt.Errorf("指标%d的日期变换处理失败", edbInfoId)
  92. return
  93. }
  94. } else {
  95. // 获取日期配置
  96. dateConfItem, ok1 := dateConfListMap[edbConf.DateConfName]
  97. if !ok1 {
  98. err = fmt.Errorf("引用日期配置不存在")
  99. return
  100. }
  101. // todo 根据日期变换得到最终日期
  102. edbDataListTmp := make([]*models.EdbDataList, 0)
  103. if dateConfItem.DateType == 0 {
  104. if dateConfItem.EdbInfoId > 0 {
  105. edbDataListTmp, ok1 = edbDataListMap[dateConfItem.EdbInfoId]
  106. if !ok1 {
  107. err = fmt.Errorf("指标%d的日期数据不存在", dateConfItem.EdbInfoId)
  108. return
  109. }
  110. findDate, err = GetChartSectionSeriesDateByDateChange(dateConfItem.EdbInfoId, edbDataListTmp, dateConfItem.DateChange, dateConfItem.MoveForward)
  111. if err != nil {
  112. err = fmt.Errorf("指标%d的日期变换处理失败", dateConfItem.EdbInfoId)
  113. return
  114. }
  115. } else {
  116. err = fmt.Errorf("请选择指标")
  117. return
  118. }
  119. } else if dateConfItem.DateType == 1 {
  120. findDate, err = GetChartSectionSeriesDateByDateChange(dateConfItem.EdbInfoId, edbDataListTmp, dateConfItem.DateChange, dateConfItem.MoveForward)
  121. if err != nil {
  122. err = fmt.Errorf("指标%d的日期变换处理失败", dateConfItem.EdbInfoId)
  123. return
  124. }
  125. } else if dateConfItem.DateType == 2 {
  126. if dateConfItem.StaticDate == "" {
  127. err = fmt.Errorf("请输入固定日期")
  128. return
  129. }
  130. findDate = dateConfItem.StaticDate
  131. }
  132. }
  133. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  134. if maxDate.IsZero() {
  135. maxDate = findDateTime
  136. } else {
  137. if findDateTime.After(maxDate) {
  138. maxDate = findDateTime
  139. }
  140. }
  141. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  142. dataList[index] = tmpValue
  143. if index == 0 {
  144. minVal = tmpValue
  145. maxVal = tmpValue
  146. } else {
  147. if tmpValue < minVal {
  148. minVal = tmpValue
  149. }
  150. if tmpValue > maxVal {
  151. maxVal = tmpValue
  152. }
  153. }
  154. } else {
  155. dataList[index] = 0
  156. noDataEdbIndex = append(noDataEdbIndex, index)
  157. continue
  158. }
  159. }
  160. seriesDataListMap[seriesItem.SeriesName] = dataList
  161. seriesNoDataIndexMap[seriesItem.SeriesName] = noDataEdbIndex
  162. seriesItem.DataList = dataList
  163. seriesItem.MinData = minVal
  164. seriesItem.MaxData = maxVal
  165. seriesItem.NoDataEdbIndex = noDataEdbIndex
  166. if extraConfig.BaseChartSeriesName == seriesItem.SeriesName {
  167. baseSeries = seriesItem
  168. }
  169. if seriesItem.IsAxis == 1 && leftUnit == nil { //左轴,右轴
  170. leftUnit = firstUnit
  171. } else if seriesItem.IsAxis == 0 && rightUnit == nil {
  172. rightUnit = firstUnit
  173. } else if seriesItem.IsAxis == 2 && right2Unit == nil {
  174. right2Unit = firstUnit
  175. }
  176. //处理上下限
  177. var minData, maxData float64
  178. for _, d := range seriesItem.DataList {
  179. if minData > d {
  180. minData = d
  181. }
  182. if maxData < d {
  183. maxData = d
  184. }
  185. }
  186. if seriesItem.IsAxis == 1 {
  187. if LeftMin > minData {
  188. LeftMin = minData
  189. }
  190. if LeftMax < maxData {
  191. LeftMax = maxData
  192. }
  193. } else if seriesItem.IsAxis == 0 {
  194. if RightMin > minData {
  195. RightMin = minData
  196. }
  197. if RightMax < maxData {
  198. RightMax = maxData
  199. }
  200. } else {
  201. if Right2Min > minData {
  202. Right2Min = minData
  203. }
  204. if Right2Max < maxData {
  205. Right2Max = maxData
  206. }
  207. }
  208. }
  209. // 处理横轴
  210. // 遍历基准系列,判断有几个横轴名称
  211. if baseSeries == nil {
  212. err = fmt.Errorf("基准系列不存在")
  213. return
  214. }
  215. defaultIndexXDataList := make([]int, 0) //默认排序时的横轴
  216. defaultXDataMap := make(map[int]models.XData) //默认排序时的横轴单位
  217. for index, item := range baseSeries.EdbInfoList {
  218. if index == 0 {
  219. firstUnit = &models.XData{
  220. Name: item.Unit,
  221. NameEn: item.UnitEn,
  222. }
  223. }
  224. tmp := models.XData{
  225. Name: item.EdbName,
  226. NameEn: item.EdbNameEn,
  227. }
  228. defaultXDataMap[index] = tmp
  229. defaultIndexXDataList = append(defaultIndexXDataList, index)
  230. }
  231. // 处理系列排序
  232. if extraConfig.SortType > 0 {
  233. newSeriesDataListMap, newSeriesNoDataIndexMap, newIndexXDataList := SortChartSeriesDataSet(baseSeries.SeriesName, baseSeries.DataList, baseSeries.NoDataEdbIndex, seriesDataListMap, seriesNoDataIndexMap, extraConfig.SortType)
  234. for k, item := range extraConfig.SeriesList {
  235. dataList, ok := newSeriesDataListMap[item.SeriesName]
  236. if ok {
  237. extraConfig.SeriesList[k].DataList = dataList
  238. }
  239. noIndex, ok := newSeriesNoDataIndexMap[item.SeriesName]
  240. if ok {
  241. extraConfig.SeriesList[k].NoDataEdbIndex = noIndex
  242. }
  243. }
  244. defaultIndexXDataList = newIndexXDataList
  245. }
  246. xDataList := make([]models.XData, 0)
  247. for index, itemIndex := range defaultIndexXDataList {
  248. nameItem, ok := defaultXDataMap[itemIndex]
  249. if !ok {
  250. err = fmt.Errorf("单位不存在")
  251. return
  252. }
  253. // 如果已经设置了横轴名称,则用设置的名称替换
  254. if len(extraConfig.XDataList) > index {
  255. newItem := extraConfig.XDataList[index]
  256. if newItem.Name != "" {
  257. nameItem = newItem
  258. }
  259. }
  260. xDataList = append(xDataList, nameItem)
  261. }
  262. dataListResp.XDataList = xDataList
  263. unitList := new(models.ChartSectionCombineUnit)
  264. if baseSeries.IsAxis == 1 { //左轴,右轴
  265. leftUnit = firstUnit
  266. } else if baseSeries.IsAxis == 2 {
  267. rightUnit = firstUnit
  268. } else {
  269. right2Unit = firstUnit
  270. }
  271. if leftUnit != nil {
  272. unitList.LeftName = leftUnit.Name
  273. unitList.LeftNameEn = leftUnit.NameEn
  274. }
  275. if rightUnit != nil {
  276. unitList.RightName = rightUnit.Name
  277. unitList.RightNameEn = rightUnit.NameEn
  278. }
  279. if right2Unit != nil {
  280. unitList.RightTwoName = right2Unit.Name
  281. unitList.RightTwoNameEn = right2Unit.NameEn
  282. }
  283. if extraConfig.UnitList.LeftName != "" {
  284. unitList.LeftName = extraConfig.UnitList.LeftName
  285. unitList.LeftNameEn = extraConfig.UnitList.LeftNameEn
  286. }
  287. if extraConfig.UnitList.RightName != "" {
  288. unitList.RightName = extraConfig.UnitList.RightName
  289. unitList.RightNameEn = extraConfig.UnitList.RightNameEn
  290. }
  291. if extraConfig.UnitList.RightTwoName != "" {
  292. unitList.RightTwoName = extraConfig.UnitList.RightTwoName
  293. unitList.RightTwoNameEn = extraConfig.UnitList.RightTwoNameEn
  294. }
  295. if chartInfo != nil && chartInfo.MinMaxSave == 1 {
  296. dataListResp.LeftMin = chartInfo.LeftMin
  297. dataListResp.LeftMax = chartInfo.LeftMax
  298. dataListResp.RightMin = chartInfo.RightMin
  299. dataListResp.RightMax = chartInfo.RightMax
  300. dataListResp.Right2Min = chartInfo.Right2Min
  301. dataListResp.Right2Max = chartInfo.Right2Max
  302. } else {
  303. dataListResp.LeftMin = strconv.FormatFloat(LeftMin, 'f', -1, 64)
  304. dataListResp.LeftMax = strconv.FormatFloat(LeftMax, 'f', -1, 64)
  305. dataListResp.RightMin = strconv.FormatFloat(RightMin, 'f', -1, 64)
  306. dataListResp.RightMax = strconv.FormatFloat(RightMax, 'f', -1, 64)
  307. dataListResp.Right2Min = strconv.FormatFloat(Right2Min, 'f', -1, 64)
  308. dataListResp.Right2Max = strconv.FormatFloat(Right2Max, 'f', -1, 64)
  309. }
  310. // 查询引用日期里的指标信息
  311. if len(dateConfEdbIds) > 0 {
  312. dateConfEdbList, e := data_manage.GetEdbInfoByIdList(dateConfEdbIds)
  313. if e != nil {
  314. err = fmt.Errorf("查询引用日期里的指标信息失败,错误信息:%s", e.Error())
  315. return
  316. }
  317. dateConfEdbMap := make(map[int]*data_manage.EdbInfo)
  318. for _, dateConfEdb := range dateConfEdbList {
  319. dateConfEdbMap[dateConfEdb.EdbInfoId] = dateConfEdb
  320. }
  321. for i, dateConf := range extraConfig.DateConfList {
  322. if dateConf.EdbInfoId > 0 {
  323. edbItem, ok := dateConfEdbMap[dateConf.EdbInfoId]
  324. if ok {
  325. extraConfig.DateConfList[i].EdbName = edbItem.EdbName
  326. extraConfig.DateConfList[i].EdbInfoId = edbItem.EdbInfoId
  327. extraConfig.DateConfList[i].EdbInfoType = edbItem.EdbInfoType
  328. extraConfig.DateConfList[i].Frequency = edbItem.Frequency
  329. extraConfig.DateConfList[i].EndDate = edbItem.EndDate
  330. }
  331. }
  332. }
  333. }
  334. dataListResp.SeriesList = extraConfig.SeriesList
  335. dataListResp.DateConfList = extraConfig.DateConfList
  336. dataListResp.BaseChartSeriesName = extraConfig.BaseChartSeriesName
  337. dataListResp.UnitList = unitList
  338. dataListResp.IsHeap = extraConfig.IsHeap
  339. dataListResp.SortType = extraConfig.SortType
  340. return
  341. }
  342. // GetChartSectionSeriesDateByDateChange 获取日期变换后的日期edbInfoId 1指标日期,2 系统日期
  343. func GetChartSectionSeriesDateByDateChange(edbInfoId int, dataList []*models.EdbDataList, dateChange []*models.ChartSectionDateChange, moveForward int) (newDate string, err error) {
  344. if edbInfoId > 0 { //指标日期
  345. newDate = GetEdbDateByMoveForward(moveForward, dataList)
  346. } else {
  347. //系统日期
  348. newDate = time.Now().Format(utils.FormatDate)
  349. }
  350. if newDate != "" && len(dateChange) > 0 {
  351. newDate, err = HandleChartSectionSeriesDateChange(newDate, dateChange)
  352. }
  353. return
  354. }
  355. func GetEdbDateByMoveForward(moveForward int, edbDataList []*models.EdbDataList) (date string) {
  356. dateList := make([]string, 0)
  357. for _, v := range edbDataList {
  358. dateList = append(dateList, v.DataTime)
  359. }
  360. date = GetEdbDateByMoveForwardByDateList(moveForward, dateList)
  361. return
  362. }
  363. func GetEdbDateByMoveForwardByDateList(moveForward int, dateList []string) (date string) {
  364. // 根据日期进行排序
  365. index := len(dateList) - 1 - moveForward
  366. for k, v := range dateList {
  367. if k == index {
  368. date = v
  369. return
  370. }
  371. }
  372. return
  373. }
  374. // HandleChartSectionSeriesDateChange 处理日期变换
  375. func HandleChartSectionSeriesDateChange(date string, dateChange []*models.ChartSectionDateChange) (newDate string, err error) {
  376. newDate = date
  377. if newDate != "" {
  378. if len(dateChange) > 0 {
  379. var dateTime time.Time
  380. dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
  381. if err != nil {
  382. err = fmt.Errorf("日期解析失败: %s", err.Error())
  383. return
  384. }
  385. for _, v := range dateChange {
  386. if v.ChangeType == 1 {
  387. dateTime = dateTime.AddDate(v.Year, v.Month, v.Day)
  388. newDate = dateTime.Format(utils.FormatDate)
  389. } else if v.ChangeType == 2 {
  390. newDate, err, _ = handleSystemAppointDateT(dateTime, v.FrequencyDay, v.Frequency)
  391. if err != nil {
  392. return
  393. }
  394. dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
  395. if err != nil {
  396. err = fmt.Errorf("日期解析失败: %s", err.Error())
  397. return
  398. }
  399. }
  400. }
  401. }
  402. }
  403. return
  404. }
  405. // handleSystemAppointDateT
  406. // @Description: 处理系统日期相关的指定频率(所在周/旬/月/季/半年/年的最后/最早一天)
  407. // @author: Roc
  408. // @datetime2023-10-27 09:31:35
  409. // @param Frequency string
  410. // @param Day string
  411. // @return date string
  412. // @return err error
  413. // @return errMsg string
  414. func handleSystemAppointDateT(currDate time.Time, appointDay, frequency string) (date string, err error, errMsg string) {
  415. //currDate := time.Now()
  416. switch frequency {
  417. case "本周":
  418. day := int(currDate.Weekday())
  419. if day == 0 { // 周日
  420. day = 7
  421. }
  422. num := 0
  423. switch appointDay {
  424. case "周一":
  425. num = 1
  426. case "周二":
  427. num = 2
  428. case "周三":
  429. num = 3
  430. case "周四":
  431. num = 4
  432. case "周五":
  433. num = 5
  434. case "周六":
  435. num = 6
  436. case "周日":
  437. num = 7
  438. }
  439. day = num - day
  440. date = currDate.AddDate(0, 0, day).Format(utils.FormatDate)
  441. case "本旬":
  442. day := currDate.Day()
  443. var tmpDate time.Time
  444. switch appointDay {
  445. case "第一天":
  446. if day <= 10 {
  447. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  448. } else if day <= 20 {
  449. tmpDate = time.Date(currDate.Year(), currDate.Month(), 11, 0, 0, 0, 0, currDate.Location())
  450. } else {
  451. tmpDate = time.Date(currDate.Year(), currDate.Month(), 21, 0, 0, 0, 0, currDate.Location())
  452. }
  453. case "最后一天":
  454. if day <= 10 {
  455. tmpDate = time.Date(currDate.Year(), currDate.Month(), 10, 0, 0, 0, 0, currDate.Location())
  456. } else if day <= 20 {
  457. tmpDate = time.Date(currDate.Year(), currDate.Month(), 20, 0, 0, 0, 0, currDate.Location())
  458. } else {
  459. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  460. }
  461. }
  462. date = tmpDate.Format(utils.FormatDate)
  463. case "本月":
  464. var tmpDate time.Time
  465. switch appointDay {
  466. case "第一天":
  467. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  468. case "最后一天":
  469. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  470. }
  471. date = tmpDate.Format(utils.FormatDate)
  472. case "本季":
  473. month := currDate.Month()
  474. var tmpDate time.Time
  475. switch appointDay {
  476. case "第一天":
  477. if month <= 3 {
  478. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  479. } else if month <= 6 {
  480. tmpDate = time.Date(currDate.Year(), 4, 1, 0, 0, 0, 0, currDate.Location())
  481. } else if month <= 9 {
  482. tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
  483. } else {
  484. tmpDate = time.Date(currDate.Year(), 10, 1, 0, 0, 0, 0, currDate.Location())
  485. }
  486. case "最后一天":
  487. if month <= 3 {
  488. tmpDate = time.Date(currDate.Year(), 3, 31, 0, 0, 0, 0, currDate.Location())
  489. } else if month <= 6 {
  490. tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
  491. } else if month <= 9 {
  492. tmpDate = time.Date(currDate.Year(), 9, 30, 0, 0, 0, 0, currDate.Location())
  493. } else {
  494. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  495. }
  496. }
  497. date = tmpDate.Format(utils.FormatDate)
  498. case "本半年":
  499. month := currDate.Month()
  500. var tmpDate time.Time
  501. switch appointDay {
  502. case "第一天":
  503. if month <= 6 {
  504. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  505. } else {
  506. tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
  507. }
  508. case "最后一天":
  509. if month <= 6 {
  510. tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
  511. } else {
  512. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  513. }
  514. }
  515. date = tmpDate.Format(utils.FormatDate)
  516. case "本年":
  517. var tmpDate time.Time
  518. switch appointDay {
  519. case "第一天":
  520. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  521. case "最后一天":
  522. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  523. }
  524. date = tmpDate.Format(utils.FormatDate)
  525. default:
  526. errMsg = "错误的日期频度:" + frequency
  527. err = errors.New(errMsg)
  528. return
  529. }
  530. return
  531. }
  532. // sortTripleDataSet 以第一组数据为基准,排序之后,空数组的位置也要同步变更
  533. func SortChartSeriesDataSet(baseName string, baseDataList []float64, baseSeriesNoDataIndexList []int, dataListMap map[string][]float64, noDataListIndexMap map[string][]int, asc int) (newDataListMap map[string][]float64, newNoDataListIndexMap map[string][]int, newIndexXDataList []int) {
  534. newDataListMap = make(map[string][]float64)
  535. newNoDataListIndexMap = make(map[string][]int)
  536. indices := make([]int, len(baseDataList))
  537. newIndices := make([]int, len(baseDataList)-len(baseSeriesNoDataIndexList))
  538. // 初始化indices
  539. for i := range indices {
  540. indices[i] = i
  541. }
  542. if len(baseSeriesNoDataIndexList) > 0 { //把空值移动到最右边
  543. j := 0
  544. for i := range indices {
  545. isEmpty := false
  546. for _, v := range baseSeriesNoDataIndexList {
  547. if i == v {
  548. isEmpty = true
  549. break
  550. }
  551. }
  552. if isEmpty {
  553. continue
  554. }
  555. newIndices[j] = i
  556. j += 1
  557. }
  558. newIndices = append(newIndices, baseSeriesNoDataIndexList...)
  559. // 根据排序后的indices重新排列所有组的数据
  560. for i, idx := range newIndices {
  561. for k, _ := range dataListMap {
  562. if _, ok := newDataListMap[k]; !ok {
  563. newDataListMap[k] = make([]float64, len(baseDataList))
  564. }
  565. if utils.InArrayByInt(noDataListIndexMap[k], idx) { //如果i位置上的数据为空,那么
  566. newNoDataListIndexMap[k] = append(newNoDataListIndexMap[k], i)
  567. }
  568. newDataListMap[k][i] = dataListMap[k][idx]
  569. }
  570. }
  571. dataListMap = newDataListMap
  572. noDataListIndexMap = newNoDataListIndexMap
  573. newDataListMap = make(map[string][]float64)
  574. newNoDataListIndexMap = make(map[string][]int)
  575. baseDataList, _ = dataListMap[baseName]
  576. //先把空的数据移动到最后面
  577. indices = make([]int, len(baseDataList)-len(baseSeriesNoDataIndexList)) //空值不参与排序
  578. newIndices = make([]int, len(baseDataList)) //空值不参与排序
  579. // 初始化indices
  580. for i := range indices {
  581. indices[i] = i
  582. }
  583. }
  584. length := len(indices)
  585. baseDataSortList := make([]models.ChartSectionSeriesValSort, length)
  586. for i, value := range baseDataList {
  587. if i < length {
  588. baseDataSortList[i] = models.ChartSectionSeriesValSort{Index: i, Value: value}
  589. }
  590. }
  591. if asc == 1 {
  592. // 使用sort.Sort进行排序
  593. sort.Sort(models.ChartSectionSeriesValSortAsc(baseDataSortList))
  594. } else {
  595. sort.Sort(models.ChartSectionSeriesValSortDesc(baseDataSortList))
  596. }
  597. for k, v := range baseDataSortList {
  598. indices[k] = v.Index
  599. }
  600. for i := range newIndices {
  601. if i < length {
  602. newIndices[i] = indices[i]
  603. } else {
  604. newIndices[i] = i
  605. }
  606. }
  607. // 根据排序后的indices重新排列所有组的数据
  608. for i, idx := range newIndices {
  609. for k, _ := range dataListMap {
  610. if _, ok := newDataListMap[k]; !ok {
  611. newDataListMap[k] = make([]float64, len(baseDataList))
  612. }
  613. if utils.InArrayByInt(noDataListIndexMap[k], idx) { //如果i位置上的数据为空,那么
  614. newNoDataListIndexMap[k] = append(newNoDataListIndexMap[k], i)
  615. }
  616. newDataListMap[k][i] = dataListMap[k][idx]
  617. }
  618. }
  619. newIndexXDataList = newIndices
  620. return
  621. }