chart_theme.go 2.7 KB

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