chart_theme.go 22 KB

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