chart_theme.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package data
  2. import (
  3. "encoding/json"
  4. "eta/eta_chart_lib/models/data_manage/chart_theme"
  5. "eta/eta_chart_lib/utils"
  6. )
  7. // GetChartThemeConfig
  8. // @Description: 根据主题id获取主题信息,如果获取不到的话,那么就获取默认的主题
  9. // @author: Roc
  10. // @datetime 2023-12-19 14:31:17
  11. // @param chartThemeId int
  12. // @param chartType int
  13. // @param source int
  14. // @return chartTheme *chart_theme.ChartTheme
  15. // @return err error
  16. func GetChartThemeConfig(chartThemeId, source, chartType int) (chartTheme *chart_theme.ChartTheme, err error) {
  17. chartTheme, err = chart_theme.GetChartThemeId(chartThemeId)
  18. if err != nil && err.Error() != utils.ErrNoRow() {
  19. return
  20. }
  21. err = nil
  22. // 如果找到了,那么就返回
  23. if chartTheme != nil {
  24. // 兼容历史数据,加入新字段LineOptionList
  25. newConfig, e := ConvertOldChartOptions(chartTheme.Config)
  26. if e == nil {
  27. chartTheme.Config = newConfig
  28. } else {
  29. err = e
  30. return
  31. }
  32. return
  33. }
  34. // 没有找到的话,那么就找默认的主题
  35. // 查找主题类型id
  36. chartThemeType, err := chart_theme.GetChartThemeTypeByChartTypeAndSource(chartType, source)
  37. if err != nil {
  38. return
  39. }
  40. // 寻找默认的主题id
  41. chartTheme, err = chart_theme.GetChartThemeId(chartThemeType.DefaultChartThemeId)
  42. if err != nil && err.Error() != utils.ErrNoRow() {
  43. return
  44. }
  45. err = nil
  46. // 如果找到了,那么就返回
  47. if chartTheme != nil {
  48. // 兼容历史数据,加入新字段LineOptionList
  49. newConfig, e := ConvertOldChartOptions(chartTheme.Config)
  50. if e == nil {
  51. chartTheme.Config = newConfig
  52. } else {
  53. err = e
  54. return
  55. }
  56. return
  57. }
  58. // 如果还是没找到,那就系统的主题id
  59. chartTheme, err = chart_theme.GetSystemChartTheme(chartThemeType.ChartThemeTypeId)
  60. // 兼容历史数据,加入新字段LineOptionList
  61. newConfig, e := ConvertOldChartOptions(chartTheme.Config)
  62. if e == nil {
  63. chartTheme.Config = newConfig
  64. } else {
  65. err = e
  66. return
  67. }
  68. return
  69. }
  70. // 兼容历史数据,加入新字段LineOptionList
  71. func ConvertOldChartOptions(config string) (newConfig string, err error) {
  72. var oldTheme chart_theme.OldChartOptions
  73. var newTheme chart_theme.NewChartOptions
  74. err = json.Unmarshal([]byte(config), &oldTheme)
  75. if err != nil {
  76. return
  77. }
  78. if oldTheme.LineOptionList != nil && len(oldTheme.LineOptionList) > 0 {
  79. newConfig = config
  80. return
  81. }
  82. newTheme.OldChartOptions = oldTheme
  83. for i := 0; i < 10; i++ {
  84. newLineOption := chart_theme.NewLineOptions{
  85. LineOptions: oldTheme.LineOptions,
  86. Color: oldTheme.ColorsOptions[i],
  87. DataMark: "none",
  88. MarkType: "square",
  89. MarkSize: 5,
  90. MarkColor: oldTheme.ColorsOptions[i],
  91. }
  92. newTheme.LineOptionList = append(newTheme.LineOptionList, newLineOption)
  93. }
  94. newThemeStr, _ := json.Marshal(newTheme)
  95. newConfig = string(newThemeStr)
  96. return
  97. }