Commit d15cd7f7b443b2921697ffb84b1bbd119c21c12b

Authored by fengwotao
1 parent 32319e73

fix: 修复首页饼状图暗黑模式切换

... ... @@ -22,9 +22,12 @@
22 22 shallowReactive,
23 23 onUnmounted,
24 24 nextTick,
  25 + computed,
  26 + watch,
25 27 } from 'vue';
26 28 import * as echarts from 'echarts';
27 29 import { seriesDataT } from './props';
  30 + import { useAppStore } from '/@/store/modules/app';
28 31
29 32 export default defineComponent({
30 33 props: {
... ... @@ -35,10 +38,29 @@
35 38 },
36 39
37 40 setup(props) {
  41 + const appStore = useAppStore();
  42 + const skinName = computed(() => {
  43 + return appStore.getDarkMode === 'light' ? '#ffffff' : '#151515';
  44 + });
  45 +
38 46 const chartsInstance = shallowReactive<{ [key: string]: echarts.ECharts }>({});
39 47
40 48 const { seriesStatusData } = toRefs(props);
41 49
  50 + watch(
  51 + () => appStore.getDarkMode,
  52 + (target) => {
  53 + const backgroundColor = target === 'light' ? '#ffffff' : '#151515';
  54 + for (const item of seriesStatusData.value) {
  55 + const { key } = item;
  56 + chartsInstance[key!]?.setOption({ backgroundColor });
  57 + }
  58 + },
  59 + {
  60 + immediate: true,
  61 + }
  62 + );
  63 +
42 64 const total = seriesStatusData.value
43 65 .map((m) => m.value)
44 66 .reduce((prev, cur) => prev! + cur!, 0);
... ... @@ -61,8 +83,9 @@
61 83 chartsInstance[key!] = echarts.init(
62 84 document.getElementById(`chartPie${key}`) as HTMLElement
63 85 );
  86 + console.log('11', skinName.value);
64 87 const option = {
65   - backgroundColor: '#ffffff',
  88 + backgroundColor: skinName.value,
66 89 tooltip: {
67 90 trigger: 'item',
68 91 formatter: `${legendKey}设备${((value! / total!) * 100).toFixed(2)}%`,
... ... @@ -101,6 +124,9 @@
101 124 ],
102 125 };
103 126 chartsInstance[key!].setOption(option);
  127 + // chartsInstance[key!].setOption({
  128 + // backgroundColor: skinName,
  129 + // });
104 130 }
105 131 });
106 132
... ...
... ... @@ -5,10 +5,11 @@
5 5 /></div>
6 6 </template>
7 7 <script lang="ts">
8   - import { defineComponent, PropType, ref, Ref, onMounted, toRefs } from 'vue';
  8 + import { defineComponent, PropType, ref, Ref, onMounted, toRefs, computed, watch } from 'vue';
9 9 import { useECharts } from '/@/hooks/web/useECharts';
10 10 import { Empty } from 'ant-design-vue';
11 11 import { seriesDataT } from './props';
  12 + import { useAppStore } from '/@/store/modules/app';
12 13
13 14 export default defineComponent({
14 15 components: { Empty },
... ... @@ -35,23 +36,22 @@
35 36 },
36 37 },
37 38 setup(props) {
  39 + const appStore = useAppStore();
38 40 const { legendData, seriesData } = toRefs(props);
39 41 const dataSeries: Ref<seriesDataT[]> = ref([]);
40 42 const legendDatas: Ref<seriesDataT[]> = ref([]);
41 43 dataSeries.value = seriesData.value as unknown as seriesDataT[];
42 44 legendDatas.value = legendData.value as unknown as seriesDataT[];
43   -
  45 + const skinName = computed(() => {
  46 + return appStore.getDarkMode === 'light' ? '#ffffff' : '#151515';
  47 + });
44 48 const chartRef = ref<HTMLDivElement | null>(null);
  49 +
45 50 const { setOptions, resize } = useECharts(chartRef as Ref<HTMLDivElement>);
46   - const labelLine = {
47   - normal: {
48   - show: true,
49   - length2: 1,
50   - },
51   - } as any;
52   - onMounted(() => {
53   - setOptions({
54   - backgroundColor: '#ffffff',
  51 +
  52 + const getOptions: any = () => {
  53 + return {
  54 + backgroundColor: skinName.value,
55 55 tooltip: {
56 56 trigger: 'item',
57 57 formatter: '{b} {d}%',
... ... @@ -72,7 +72,29 @@
72 72 labelLine,
73 73 },
74 74 ],
75   - });
  75 + };
  76 + };
  77 +
  78 + watch(
  79 + () => appStore.getDarkMode,
  80 + (target) => {
  81 + const backgroundColor = target === 'light' ? '#ffffff' : '#151515';
  82 + setOptions &&
  83 + setOptions({
  84 + ...getOptions(),
  85 + backgroundColor,
  86 + });
  87 + }
  88 + );
  89 +
  90 + const labelLine = {
  91 + normal: {
  92 + show: true,
  93 + length2: 1,
  94 + },
  95 + } as any;
  96 + onMounted(() => {
  97 + setOptions(getOptions());
76 98 //自适应
77 99 window.addEventListener('resize', () => resize());
78 100 });
... ...