123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- package data
- import (
- "encoding/json"
- "eta/eta_chart_lib/models/data_manage/chart_theme"
- "eta/eta_chart_lib/utils"
- )
- // GetChartThemeConfig
- // @Description: 根据主题id获取主题信息,如果获取不到的话,那么就获取默认的主题
- // @author: Roc
- // @datetime 2023-12-19 14:31:17
- // @param chartThemeId int
- // @param chartType int
- // @param source int
- // @return chartTheme *chart_theme.ChartTheme
- // @return err error
- func GetChartThemeConfig(chartThemeId, source, chartType int) (chartTheme *chart_theme.ChartTheme, err error) {
- chartTheme, err = chart_theme.GetChartThemeId(chartThemeId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- return
- }
- err = nil
- // 如果找到了,那么就返回
- if chartTheme != nil {
- // 兼容历史数据,加入新字段LineOptionList
- newConfig, e := ConvertOldChartOptions(chartTheme.Config)
- if e == nil {
- chartTheme.Config = newConfig
- } else {
- err = e
- return
- }
- return
- }
- // 没有找到的话,那么就找默认的主题
- // 查找主题类型id
- chartThemeType, err := chart_theme.GetChartThemeTypeByChartTypeAndSource(chartType, source)
- if err != nil {
- return
- }
- // 寻找默认的主题id
- chartTheme, err = chart_theme.GetChartThemeId(chartThemeType.DefaultChartThemeId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- return
- }
- err = nil
- // 如果找到了,那么就返回
- if chartTheme != nil {
- // 兼容历史数据,加入新字段LineOptionList
- newConfig, e := ConvertOldChartOptions(chartTheme.Config)
- if e == nil {
- chartTheme.Config = newConfig
- } else {
- err = e
- return
- }
- return
- }
- // 如果还是没找到,那就系统的主题id
- chartTheme, err = chart_theme.GetSystemChartTheme(chartThemeType.ChartThemeTypeId)
- // 兼容历史数据,加入新字段LineOptionList
- newConfig, e := ConvertOldChartOptions(chartTheme.Config)
- if e == nil {
- chartTheme.Config = newConfig
- } else {
- err = e
- return
- }
- return
- }
- // 兼容历史数据,加入新字段LineOptionList
- func ConvertOldChartOptions(config string) (newConfig string, err error) {
- var oldTheme chart_theme.OldChartOptions
- var newTheme chart_theme.NewChartOptions
- err = json.Unmarshal([]byte(config), &oldTheme)
- if err != nil {
- return
- }
- if oldTheme.LineOptionList != nil && len(oldTheme.LineOptionList) > 0 {
- newConfig = config
- return
- }
- newTheme.OldChartOptions = oldTheme
- for i := 0; i < 10; i++ {
- newLineOption := chart_theme.NewLineOptions{
- LineOptions: oldTheme.LineOptions,
- Color: oldTheme.ColorsOptions[i],
- DataMark: "none",
- MarkType: "square",
- MarkSize: 5,
- MarkColor: oldTheme.ColorsOptions[i],
- }
- newTheme.LineOptionList = append(newTheme.LineOptionList, newLineOption)
- }
- newThemeStr, _ := json.Marshal(newTheme)
- newConfig = string(newThemeStr)
- return
- }
|