Commit d15cd7f7b443b2921697ffb84b1bbd119c21c12b

Authored by fengwotao
1 parent 32319e73

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

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