echarts-mixin.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * echarts 混入
  3. */
  4. import store from '@/store';
  5. import { THEME_KEY } from 'vue-echarts';
  6. import { ChartTheme, ChartThemeDark } from 'ele-admin';
  7. export function echartsMixin(refs) {
  8. return {
  9. provide() {
  10. return {
  11. // 主题设置
  12. [THEME_KEY]: this.chartsTheme
  13. };
  14. },
  15. data() {
  16. const dark = !!this.$store?.state?.theme?.darkMode;
  17. return {
  18. // 图表主题
  19. chartsTheme: { ...(dark ? ChartThemeDark : ChartTheme) },
  20. // 是否为 deactivated 状态
  21. deactivated: false,
  22. // 当前图表是否是暗黑主题
  23. isDark: dark
  24. };
  25. },
  26. computed: {
  27. // 内容区域宽度
  28. layoutContentWidth() {
  29. return store?.state?.theme?.contentWidth;
  30. },
  31. // 是否是暗黑主题
  32. darkMode() {
  33. return this.$store?.state?.theme?.darkMode;
  34. }
  35. },
  36. watch: {
  37. // 监听内容区域宽度变化
  38. layoutContentWidth() {
  39. this.resizeAllCharts();
  40. },
  41. // 主题跟随暗黑模式
  42. darkMode(dark) {
  43. if (!this.deactivated) {
  44. this.changeChartsTheme(dark);
  45. }
  46. }
  47. },
  48. // 适配 keep-alive
  49. activated() {
  50. this.deactivated = false;
  51. this.$nextTick(() => {
  52. if (this.isDark !== this.darkMode) {
  53. this.changeChartsTheme(this.darkMode);
  54. } else {
  55. this.resizeAllCharts();
  56. }
  57. });
  58. },
  59. deactivated() {
  60. this.deactivated = true;
  61. },
  62. methods: {
  63. /* 重置图表尺寸 */
  64. resizeAllCharts() {
  65. refs.forEach((ref) => {
  66. this.$refs[ref]?.resize();
  67. });
  68. },
  69. /* 更改图表主题 */
  70. changeChartsTheme(dark) {
  71. this.isDark = dark;
  72. Object.assign(this.chartsTheme, dark ? ChartThemeDark : ChartTheme);
  73. }
  74. }
  75. };
  76. }