Skip to content

Commit

Permalink
perf(projects): echarts loading style
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Jan 25, 2024
1 parent b5477e8 commit 456c318
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions src/hooks/chart/use-echarts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,7 @@ interface ChartHooks {
* @param optionsFactory echarts options factory function
* @param darkMode dark mode
*/
export function useEcharts<T extends ECOption>(
optionsFactory: () => T,
hooks: ChartHooks = {
onRender(chart) {
chart.showLoading();
},
onUpdated(chart) {
chart.hideLoading();
}
}
) {
export function useEcharts<T extends ECOption>(optionsFactory: () => T, hooks: ChartHooks = {}) {
const scope = effectScope();

const themeStore = useThemeStore();
Expand All @@ -103,6 +93,24 @@ export function useEcharts<T extends ECOption>(
let chart: echarts.ECharts | null = null;
const chartOptions: T = optionsFactory();

const {
onRender = instance => {
const textColor = darkMode.value ? 'rgb(224, 224, 224)' : 'rgb(31, 31, 31)';
const maskColor = darkMode.value ? 'rgba(0, 0, 0, 0.4)' : 'rgba(255, 255, 255, 0.8)';

instance.showLoading({
color: themeStore.themeColor,
textColor,
fontSize: 14,
maskColor
});
},
onUpdated = instance => {
instance.hideLoading();
},
onDestroy
} = hooks;

/**
* whether can render chart
*
Expand Down Expand Up @@ -135,7 +143,7 @@ export function useEcharts<T extends ECOption>(

chart?.setOption({ ...updatedOpts, backgroundColor: 'transparent' });

await hooks?.onUpdated?.(chart!);
await onUpdated?.(chart!);
}

/** render chart */
Expand All @@ -149,7 +157,7 @@ export function useEcharts<T extends ECOption>(

chart.setOption({ ...chartOptions, backgroundColor: 'transparent' });

await hooks?.onRender?.(chart);
await onRender?.(chart);
}
}

Expand All @@ -162,7 +170,7 @@ export function useEcharts<T extends ECOption>(
async function destroy() {
if (!chart) return;

await hooks?.onDestroy?.(chart);
await onDestroy?.(chart);
chart?.dispose();
chart = null;
}
Expand All @@ -171,7 +179,7 @@ export function useEcharts<T extends ECOption>(
async function changeTheme() {
await destroy();
await render();
await hooks?.onUpdated?.(chart!);
await onUpdated?.(chart!);
}

/**
Expand Down

0 comments on commit 456c318

Please sign in to comment.