themeFormatting.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import _ from 'lodash'
  2. /**
  3. * @Description: 主题设置格式化
  4. * @author liu.shiyi
  5. * @date 2023/8/17 14:47
  6. */
  7. // 将组件中的setting里面的主题设置(颜色)存放到theme里面
  8. export function settingToTheme (config, type) {
  9. // 考虑与上一次保存过的主题进行合并
  10. // 排除掉主题非暗黑非明亮的情况
  11. if (['dark', 'light'].includes(type)) {
  12. const theme = { dark: { ...config?.theme?.dark }, light: { ...config?.theme?.light } }
  13. config.setting.forEach((setItem) => {
  14. if (['gradual', 'colorPicker', 'colorSelect'].includes(setItem.type)) {
  15. theme[type][setItem.field] = setItem.value
  16. }
  17. })
  18. return theme
  19. } else {
  20. return {}
  21. }
  22. }
  23. // 将保存的theme主题设置(颜色)存放到chartList
  24. export function themeToSetting (chartList, type, _this) {
  25. // 排除掉主题非暗黑非明亮的情况
  26. if (['dark', 'light'].includes(type)) {
  27. chartList.forEach(chart => {
  28. chart.option.theme = type
  29. if (chart.theme && chart.theme[type]) {
  30. for (const item of chart.setting) {
  31. // 检查 obj 中是否存在与 item.field 相对应的属性
  32. if (Object.prototype.hasOwnProperty.call(chart.theme[type], item.field)) {
  33. // 更新 setting 中对应项的 value 值为 theme 中的属性值
  34. item.value = chart.theme[type][item.field]
  35. }
  36. }
  37. }
  38. })
  39. }
  40. return chartList
  41. }