chart_extra_config.go 21 KB

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