chart_theme.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. package data
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_mobile/models/data_manage"
  6. "eta/eta_mobile/models/data_manage/chart_theme"
  7. "eta/eta_mobile/models/data_manage/chart_theme/request"
  8. "eta/eta_mobile/utils"
  9. "fmt"
  10. "github.com/shopspring/decimal"
  11. "math"
  12. "time"
  13. )
  14. // GetThemePreviewChartEdbData 获取图表的指标数据
  15. func GetThemePreviewChartEdbData(chartType int, calendar, startDate, endDate string, mappingList []*data_manage.ChartEdbInfoMapping, extraConfigStr string, seasonExtraConfig string) (edbList []*data_manage.ChartEdbInfoMapping, xEdbIdValue []int, yDataList []data_manage.YData, dataResp interface{}, err error, errMsg string) {
  16. edbList = make([]*data_manage.ChartEdbInfoMapping, 0)
  17. xEdbIdValue = make([]int, 0)
  18. yDataList = make([]data_manage.YData, 0)
  19. var extraConfig interface{}
  20. switch chartType {
  21. case 7: // 柱形图
  22. var barConfig data_manage.BarChartInfoReq
  23. if extraConfigStr == `` {
  24. errMsg = "柱方图未配置"
  25. err = errors.New(errMsg)
  26. return
  27. }
  28. err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
  29. if err != nil {
  30. errMsg = "柱方图配置异常"
  31. err = errors.New(errMsg)
  32. return
  33. }
  34. extraConfig = barConfig
  35. case 10: // 截面散点图
  36. var tmpExtraConfig data_manage.SectionScatterReq
  37. if extraConfigStr == `` {
  38. errMsg = "截面散点图未配置"
  39. err = errors.New(errMsg)
  40. return
  41. }
  42. err = json.Unmarshal([]byte(extraConfigStr), &tmpExtraConfig)
  43. if err != nil {
  44. errMsg = "截面散点配置异常"
  45. err = errors.New(errMsg)
  46. return
  47. }
  48. extraConfig = tmpExtraConfig
  49. case utils.CHART_TYPE_RADAR:
  50. var barConfig data_manage.RadarChartInfoReq
  51. if extraConfigStr == `` {
  52. errMsg = "雷达图未配置"
  53. err = errors.New(errMsg)
  54. return
  55. }
  56. err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
  57. if err != nil {
  58. errMsg = "雷达图配置异常"
  59. err = errors.New(errMsg)
  60. return
  61. }
  62. extraConfig = barConfig
  63. default:
  64. xEdbIdValue = make([]int, 0)
  65. yDataList = make([]data_manage.YData, 0)
  66. }
  67. // 指标对应的所有数据
  68. edbDataListMap, edbList, err := getThemePreviewEdbDataMapList(chartType, calendar, startDate, endDate, mappingList, seasonExtraConfig)
  69. if err != nil {
  70. return
  71. }
  72. // 特殊图形数据处理
  73. switch chartType {
  74. case 2: // 季节性图
  75. if seasonExtraConfig != "" {
  76. // 季节性图计算不管图上数据时间,拿所有数据
  77. _, tempEdbList, e := getEdbDataMapList(0, chartType, calendar, "1990-01-01", "", mappingList, seasonExtraConfig)
  78. if e != nil {
  79. err = e
  80. return
  81. }
  82. dataResp, err = SeasonChartData(tempEdbList, seasonExtraConfig)
  83. } else {
  84. // 兼容无配置的老图
  85. dataResp = new(data_manage.SeasonChartResp)
  86. }
  87. case 7: // 柱形图
  88. barChartConf := extraConfig.(data_manage.BarChartInfoReq)
  89. xEdbIdValue, yDataList, err = BarChartData(mappingList, edbDataListMap, barChartConf.DateList, barChartConf.Sort)
  90. for k := range yDataList {
  91. yDataList[k].Unit = barChartConf.Unit
  92. yDataList[k].UnitEn = barChartConf.UnitEn
  93. }
  94. for _, v := range edbList {
  95. // 指标别名
  96. if barChartConf.EdbInfoIdList != nil && len(barChartConf.EdbInfoIdList) > 0 {
  97. for _, reqEdb := range barChartConf.EdbInfoIdList {
  98. if v.EdbInfoId == reqEdb.EdbInfoId {
  99. v.EdbAliasName = reqEdb.Name
  100. }
  101. }
  102. }
  103. }
  104. case 10: // 截面散点图
  105. sectionScatterConf := extraConfig.(data_manage.SectionScatterReq)
  106. xEdbIdValue, dataResp, err = getThemePreviewSectionScatterChartData(mappingList, edbDataListMap, sectionScatterConf)
  107. var tmpExtraConfig data_manage.SectionScatterReq
  108. if extraConfigStr == `` {
  109. errMsg = "截面散点图未配置"
  110. err = errors.New(errMsg)
  111. return
  112. }
  113. err = json.Unmarshal([]byte(extraConfigStr), &tmpExtraConfig)
  114. if err != nil {
  115. errMsg = "截面散点配置异常"
  116. err = errors.New(errMsg)
  117. return
  118. }
  119. // 这个数据没有必要返回给前端
  120. for _, v := range edbList {
  121. v.DataList = nil
  122. }
  123. case utils.CHART_TYPE_RADAR: //雷达图
  124. radarConf := extraConfig.(data_manage.RadarChartInfoReq)
  125. xEdbIdValue, dataResp, err = RadarChartData(mappingList, edbDataListMap, radarConf)
  126. }
  127. return
  128. }
  129. func getThemePreviewEdbDataMapList(chartType int, calendar, startDate, endDate string, mappingList []*data_manage.ChartEdbInfoMapping, seasonExtraConfig string) (edbDataListMap map[int][]*data_manage.EdbDataList, edbList []*data_manage.ChartEdbInfoMapping, err error) {
  130. // 指标对应的所有数据
  131. edbDataListMap = make(map[int][]*data_manage.EdbDataList)
  132. for _, v := range mappingList {
  133. //fmt.Println("v:", v.EdbInfoId)
  134. item := new(data_manage.ChartEdbInfoMapping)
  135. item.EdbInfoId = v.EdbInfoId
  136. item.SourceName = v.SourceName
  137. item.Source = v.Source
  138. item.EdbCode = v.EdbCode
  139. item.EdbName = v.EdbName
  140. item.EdbNameEn = v.EdbNameEn
  141. item.Frequency = v.Frequency
  142. item.EdbType = v.EdbType
  143. item.FrequencyEn = GetFrequencyEn(v.Frequency)
  144. if v.Unit != `无` {
  145. item.Unit = v.Unit
  146. }
  147. item.UnitEn = v.UnitEn
  148. item.StartDate = v.StartDate
  149. item.EndDate = v.EndDate
  150. item.ModifyTime = v.ModifyTime
  151. item.EdbInfoCategoryType = v.EdbInfoCategoryType
  152. item.PredictChartColor = v.PredictChartColor
  153. item.ClassifyId = v.ClassifyId
  154. item.IsAxis = 1
  155. item.EdbInfoType = 1
  156. item.LeadValue = v.LeadValue
  157. item.LeadUnit = v.LeadUnit
  158. item.LeadUnitEn = GetLeadUnitEn(v.LeadUnit)
  159. item.ChartEdbMappingId = v.ChartEdbMappingId
  160. item.ChartInfoId = v.ChartInfoId
  161. item.ChartStyle = v.ChartStyle
  162. item.ChartColor = v.ChartColor
  163. item.ChartWidth = 0
  164. item.IsOrder = false
  165. item.MaxData = v.MaxValue
  166. item.MinData = v.MinValue
  167. item.LatestValue = v.LatestValue
  168. item.LatestDate = v.LatestDate
  169. item.UniqueCode = v.UniqueCode
  170. item.MoveLatestDate = v.LatestDate
  171. var startDateReal string
  172. var diffSeconds int64
  173. if chartType == 2 { //季节性图
  174. startDateReal = startDate
  175. if len(mappingList) > 1 {
  176. item.IsAxis = v.IsAxis
  177. }
  178. } else {
  179. if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 { //领先指标
  180. var startTimeRealTemp time.Time
  181. startDateParse, _ := time.Parse(utils.FormatDate, startDate)
  182. switch v.LeadUnit {
  183. case "天":
  184. startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue)
  185. case "月":
  186. startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0)
  187. case "季":
  188. startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0)
  189. case "周":
  190. startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue)
  191. case "年":
  192. startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0)
  193. }
  194. if startTimeRealTemp.Before(startDateParse) {
  195. startDateReal = startTimeRealTemp.Format(utils.FormatDate)
  196. diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
  197. } else {
  198. startDateReal = startDate
  199. diffSeconds = 0
  200. }
  201. // 预测指标的开始日期也要偏移
  202. {
  203. day, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, startDate, startDateReal)
  204. if tmpErr != nil {
  205. err = tmpErr
  206. return
  207. }
  208. moveLatestDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, item.MoveLatestDate, time.Local)
  209. if tmpErr != nil {
  210. err = tmpErr
  211. return
  212. }
  213. item.MoveLatestDate = moveLatestDateTime.AddDate(0, 0, day).Format(utils.FormatDate)
  214. }
  215. } else {
  216. startDateReal = startDate
  217. }
  218. }
  219. dataList := make([]*data_manage.EdbDataList, 0)
  220. dataList, err = chart_theme.GetChartThemeDefaultDataItemList(v.EdbInfoId, startDateReal)
  221. if err != nil {
  222. return
  223. }
  224. edbDataListMap[v.EdbInfoId] = dataList
  225. if diffSeconds != 0 && v.EdbInfoType == 0 {
  226. dataListLen := len(dataList)
  227. for i := 0; i < dataListLen; i++ {
  228. dataList[i].DataTimestamp = dataList[i].DataTimestamp - diffSeconds
  229. }
  230. }
  231. if chartType == 2 {
  232. latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
  233. if tmpErr != nil {
  234. //item.DataList = dataList
  235. item.IsNullData = true
  236. edbList = append(edbList, item)
  237. continue
  238. }
  239. quarterDataList, tErr := GetSeasonEdbInfoDataListByXDate(dataList, latestDate, seasonExtraConfig)
  240. if tErr != nil {
  241. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  242. return
  243. }
  244. item.DataList = quarterDataList
  245. } else if chartType == 7 || chartType == utils.CHART_TYPE_RADAR { //柱方图
  246. //item.DataList = dataList
  247. } else {
  248. item.DataList = dataList
  249. }
  250. edbList = append(edbList, item)
  251. }
  252. return
  253. }
  254. func getThemePreviewSectionScatterChartData(mappingList []*data_manage.ChartEdbInfoMapping, edbDataListMap map[int][]*data_manage.EdbDataList, extraConfig data_manage.SectionScatterReq) (edbIdList []int, chartDataResp data_manage.SectionScatterInfoResp, err error) {
  255. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  256. edbDataMap := make(map[int]map[string]float64)
  257. for edbInfoId, edbDataList := range edbDataListMap {
  258. edbDateData := make(map[string]float64)
  259. for _, edbData := range edbDataList {
  260. edbDateData[edbData.DataTime] = edbData.Value
  261. }
  262. edbDataMap[edbInfoId] = edbDateData
  263. }
  264. // edbIdList 指标展示顺序;x轴的指标顺序
  265. edbIdList = make([]int, 0)
  266. edbMappingMap := make(map[int]*data_manage.ChartEdbInfoMapping)
  267. for _, v := range mappingList {
  268. edbIdList = append(edbIdList, v.EdbInfoId)
  269. edbMappingMap[v.EdbInfoId] = v
  270. }
  271. //SectionScatterSeriesInfoResp
  272. dataListResp := make([]data_manage.SectionScatterSeriesItemResp, 0) //y轴的数据列表
  273. for _, seriesItem := range extraConfig.SeriesList {
  274. var maxDate time.Time
  275. // 系列中的指标数据
  276. tmpSeriesEdbInfoList := make([]data_manage.SectionScatterEdbItemResp, 0)
  277. var minXVal, maxXVal, minYVal, maxYVal float64
  278. for _, edbConf := range seriesItem.EdbInfoList {
  279. tmpItem := data_manage.SectionScatterEdbItemResp{
  280. IsShow: edbConf.IsShow,
  281. Name: edbConf.Name,
  282. NameEn: edbConf.NameEn,
  283. } //单个坐标点的数据
  284. //X轴的数据
  285. {
  286. edbInfoId := edbConf.XEdbInfoId //X轴的指标
  287. edbMappingInfo, ok := edbMappingMap[edbInfoId]
  288. if !ok {
  289. continue
  290. }
  291. findDate := edbConf.XDate //需要的日期值
  292. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  293. if len(dataList) <= 0 {
  294. // 没有数据的指标id
  295. //findDataList = append(findDataList, 0)
  296. continue
  297. }
  298. tmpItem.XEdbInfoId = edbInfoId
  299. tmpItem.XName = edbMappingInfo.EdbName
  300. tmpItem.XNameEn = edbMappingInfo.EdbNameEn
  301. switch edbConf.XDateType {
  302. case 1: //最新值
  303. findDate = dataList[len(dataList)-1].DataTime
  304. case 2: //近期几天
  305. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  306. if tmpErr != nil {
  307. err = tmpErr
  308. return
  309. }
  310. findDateTime = findDateTime.AddDate(0, 0, -edbConf.XDateValue)
  311. lenData := len(dataList) - 1
  312. for i := lenData; i >= 0; i-- {
  313. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  314. if tmpErr != nil {
  315. err = tmpErr
  316. return
  317. }
  318. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  319. findDate = dataList[i].DataTime
  320. break
  321. }
  322. }
  323. case 3: // 固定日期
  324. //最早的日期
  325. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  326. if tmpErr != nil {
  327. err = tmpErr
  328. return
  329. }
  330. //寻找固定日期的数据
  331. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, edbConf.XDate, time.Local)
  332. if tmpErr != nil {
  333. err = tmpErr
  334. return
  335. }
  336. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  337. tmpDate := tmpDateTime.Format(utils.FormatDate)
  338. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  339. findDate = tmpDate
  340. break
  341. }
  342. }
  343. default:
  344. err = errors.New(fmt.Sprint("日期类型异常,Type:", edbConf.XDate))
  345. return
  346. }
  347. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  348. if maxDate.IsZero() {
  349. maxDate = findDateTime
  350. } else {
  351. if findDateTime.After(maxDate) {
  352. maxDate = findDateTime
  353. }
  354. }
  355. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  356. tmpItem.XDate = findDate
  357. tmpItem.XValue = tmpValue
  358. } else {
  359. continue
  360. }
  361. }
  362. //Y轴的数据
  363. {
  364. edbInfoId := edbConf.YEdbInfoId //Y轴的指标
  365. edbMappingInfo, ok := edbMappingMap[edbInfoId]
  366. if !ok {
  367. continue
  368. }
  369. findDate := edbConf.YDate //需要的日期值
  370. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  371. if len(dataList) <= 0 {
  372. // 没有数据的指标id
  373. //findDataList = append(findDataList, 0)
  374. continue
  375. }
  376. tmpItem.YEdbInfoId = edbInfoId
  377. tmpItem.YName = edbMappingInfo.EdbName
  378. tmpItem.YNameEn = edbMappingInfo.EdbNameEn
  379. switch edbConf.YDateType {
  380. case 1: //最新值
  381. findDate = dataList[len(dataList)-1].DataTime
  382. case 2: //近期几天
  383. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  384. if tmpErr != nil {
  385. err = tmpErr
  386. return
  387. }
  388. findDateTime = findDateTime.AddDate(0, 0, -edbConf.YDateValue)
  389. lenData := len(dataList) - 1
  390. for i := lenData; i >= 0; i-- {
  391. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  392. if tmpErr != nil {
  393. err = tmpErr
  394. return
  395. }
  396. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  397. findDate = dataList[i].DataTime
  398. break
  399. }
  400. }
  401. case 3: // 固定日期
  402. //最早的日期
  403. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  404. if tmpErr != nil {
  405. err = tmpErr
  406. return
  407. }
  408. //寻找固定日期的数据
  409. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, edbConf.YDate, time.Local)
  410. if tmpErr != nil {
  411. err = tmpErr
  412. return
  413. }
  414. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  415. tmpDate := tmpDateTime.Format(utils.FormatDate)
  416. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  417. findDate = tmpDate
  418. break
  419. }
  420. }
  421. default:
  422. err = errors.New(fmt.Sprint("日期类型异常,Type:", edbConf.YDate))
  423. return
  424. }
  425. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  426. if maxDate.IsZero() {
  427. maxDate = findDateTime
  428. } else {
  429. if findDateTime.After(maxDate) {
  430. maxDate = findDateTime
  431. }
  432. }
  433. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  434. tmpItem.YDate = findDate
  435. tmpItem.YValue = tmpValue
  436. } else {
  437. continue
  438. }
  439. }
  440. // 获取当前系列的X轴的最大最小值
  441. {
  442. if tmpItem.XValue < minXVal {
  443. minXVal = tmpItem.XValue
  444. }
  445. if tmpItem.XValue > maxXVal {
  446. maxXVal = tmpItem.XValue
  447. }
  448. if tmpItem.YValue < minYVal {
  449. minYVal = tmpItem.YValue
  450. }
  451. if tmpItem.YValue > maxYVal {
  452. maxYVal = tmpItem.YValue
  453. }
  454. }
  455. tmpSeriesEdbInfoList = append(tmpSeriesEdbInfoList, tmpItem)
  456. }
  457. trendLimitData := make([]data_manage.CoordinatePoint, 0) //趋势线的前后坐标点
  458. var trendLine, rSquare string
  459. // 生成线性方程式
  460. var a, b float64
  461. {
  462. coordinateData := make([]utils.Coordinate, 0)
  463. for _, tmpSeriesEdbInfo := range tmpSeriesEdbInfoList {
  464. tmpCoordinate1 := utils.Coordinate{
  465. X: tmpSeriesEdbInfo.XValue,
  466. Y: tmpSeriesEdbInfo.YValue,
  467. }
  468. coordinateData = append(coordinateData, tmpCoordinate1)
  469. }
  470. // 只有存在两个坐标点的时候,才能去计算线性方程和R平方
  471. if len(coordinateData) >= 2 {
  472. a, b = utils.GetLinearResult(coordinateData)
  473. if !math.IsNaN(a) && !math.IsNaN(b) {
  474. if b > 0 {
  475. trendLine = fmt.Sprintf("y=%sx+%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4))
  476. } else {
  477. trendLine = fmt.Sprintf("y=%sx%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4))
  478. }
  479. minYVal, _ = decimal.NewFromFloat(a).Mul(decimal.NewFromFloat(minXVal)).Add(decimal.NewFromFloat(b)).Round(4).Float64()
  480. maxYVal, _ = decimal.NewFromFloat(a).Mul(decimal.NewFromFloat(maxXVal)).Add(decimal.NewFromFloat(b)).Round(4).Float64()
  481. }
  482. // 计算R平方
  483. rSquare = fmt.Sprint(utils.CalculationDecisive(coordinateData))
  484. }
  485. trendLimitData = append(trendLimitData, data_manage.CoordinatePoint{
  486. X: minXVal,
  487. Y: minYVal,
  488. }, data_manage.CoordinatePoint{
  489. X: maxXVal,
  490. Y: maxYVal,
  491. })
  492. }
  493. dataListResp = append(dataListResp, data_manage.SectionScatterSeriesItemResp{
  494. Name: seriesItem.Name,
  495. NameEn: seriesItem.NameEn,
  496. IsNameDefault: seriesItem.IsNameDefault,
  497. Color: seriesItem.Color,
  498. EdbInfoList: tmpSeriesEdbInfoList,
  499. ShowTrendLine: seriesItem.ShowTrendLine,
  500. ShowFitEquation: seriesItem.ShowFitEquation,
  501. ShowRSquare: seriesItem.ShowRSquare,
  502. TrendLine: trendLine,
  503. RSquare: rSquare,
  504. TrendLimitData: trendLimitData,
  505. })
  506. }
  507. // 截面散点图点击详情时自动更新系列名
  508. if len(extraConfig.SeriesList) > 0 {
  509. // 默认名字的时候才自动更新
  510. if extraConfig.SeriesList[0].IsNameDefault {
  511. firstXEdbInfoId := extraConfig.SeriesList[0].EdbInfoList[0].XEdbInfoId
  512. if v, ok := edbMappingMap[firstXEdbInfoId]; ok {
  513. extraConfig.SeriesList[0].Name = v.LatestDate
  514. extraConfig.SeriesList[0].NameEn = v.LatestDate
  515. dataListResp[0].Name = v.LatestDate
  516. dataListResp[0].NameEn = v.LatestDate
  517. }
  518. }
  519. }
  520. chartDataResp = data_manage.SectionScatterInfoResp{
  521. XName: extraConfig.XName,
  522. XNameEn: extraConfig.XNameEn,
  523. XUnitName: extraConfig.XUnitName,
  524. XUnitNameEn: extraConfig.XUnitNameEn,
  525. YName: extraConfig.YName,
  526. YNameEn: extraConfig.YNameEn,
  527. YUnitName: extraConfig.YUnitName,
  528. YUnitNameEn: extraConfig.YUnitNameEn,
  529. XMinValue: extraConfig.XMinValue,
  530. XMaxValue: extraConfig.XMaxValue,
  531. YMinValue: extraConfig.YMinValue,
  532. YMaxValue: extraConfig.YMaxValue,
  533. DataList: dataListResp,
  534. }
  535. return
  536. }
  537. // GetChartThemeConfig
  538. // @Description: 根据主题id获取主题信息,如果获取不到的话,那么就获取默认的主题
  539. // @author: Roc
  540. // @datetime 2023-12-19 14:31:17
  541. // @param chartThemeId int
  542. // @param chartType int
  543. // @param source int
  544. // @return chartTheme *chart_theme.ChartTheme
  545. // @return err error
  546. func GetChartThemeConfig(chartThemeId, source, chartType int) (chartTheme *chart_theme.ChartTheme, err error) {
  547. chartTheme, err = chart_theme.GetChartThemeId(chartThemeId)
  548. if err != nil && err.Error() != utils.ErrNoRow() {
  549. return
  550. }
  551. err = nil
  552. // 如果找到了,那么就返回
  553. if chartTheme != nil {
  554. newConfig, e := ConvertOldChartOptions(chartTheme.Config)
  555. if e == nil {
  556. chartTheme.Config = newConfig
  557. }
  558. return
  559. }
  560. // 没有找到的话,那么就找默认的主题
  561. // 查找主题类型id
  562. chartThemeType, err := chart_theme.GetChartThemeTypeByChartTypeAndSource(chartType, source)
  563. if err != nil {
  564. newConfig, e := ConvertOldChartOptions(chartTheme.Config)
  565. if e == nil {
  566. chartTheme.Config = newConfig
  567. }
  568. return
  569. }
  570. // 寻找默认的主题id
  571. chartTheme, err = chart_theme.GetChartThemeId(chartThemeType.DefaultChartThemeId)
  572. if err != nil && err.Error() != utils.ErrNoRow() {
  573. newConfig, e := ConvertOldChartOptions(chartTheme.Config)
  574. if e == nil {
  575. chartTheme.Config = newConfig
  576. }
  577. return
  578. }
  579. err = nil
  580. // 如果找到了,那么就返回
  581. if chartTheme != nil {
  582. return
  583. }
  584. // 如果还是没找到,那就系统的主题id
  585. chartTheme, err = chart_theme.GetSystemChartTheme(chartThemeType.ChartThemeTypeId)
  586. // 兼容历史数据,加入新字段LineOptionList
  587. newConfig, e := ConvertOldChartOptions(chartTheme.Config)
  588. if e == nil {
  589. chartTheme.Config = newConfig
  590. }
  591. return
  592. }
  593. // 兼容历史数据,加入新字段LineOptionList
  594. func ConvertOldChartOptions(config string) (newConfig string, err error) {
  595. var oldTheme request.OldChartOptions
  596. var newTheme request.NewChartOptions
  597. err = json.Unmarshal([]byte(config), &oldTheme)
  598. if err != nil {
  599. return
  600. }
  601. if oldTheme.LineOptionList != nil && len(oldTheme.LineOptionList) > 0 {
  602. newConfig = config
  603. return
  604. }
  605. newTheme.OldChartOptions = oldTheme
  606. for i := 0; i < 10; i++ {
  607. newLineOption := request.NewLineOptions{
  608. LineOptions: oldTheme.LineOptions,
  609. Color: oldTheme.ColorsOptions[i],
  610. DataMark: "none",
  611. MarkType: "square",
  612. MarkSize: 5,
  613. MarkColor: oldTheme.ColorsOptions[i],
  614. }
  615. newTheme.LineOptionList = append(newTheme.LineOptionList, newLineOption)
  616. }
  617. newThemeStr, _ := json.Marshal(newTheme)
  618. newConfig = string(newThemeStr)
  619. return
  620. }