Skip to content

Commit

Permalink
perf: Update useApexCharts.ts (#139)
Browse files Browse the repository at this point in the history
1、setOptions增加callback方法,返回chartInstance,以便于对图表进行再操作,例如调用updateOptions方法更新图表;
2、新增调用ApexCharts的updateOptions方法更新图表
  • Loading branch information
1078889045 committed Dec 21, 2020
1 parent 877311f commit 5eecec0
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/hooks/web/useApexCharts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ApexCharts from 'apexcharts';
export function useApexCharts(elRef: Ref<HTMLDivElement>) {
let chartInstance: Nullable<ApexCharts> = null;

function setOptions(options: any) {
function setOptions(options: any, callback) {
nextTick(() => {
useTimeoutFn(() => {
const el = unref(elRef);
Expand All @@ -16,9 +16,38 @@ export function useApexCharts(elRef: Ref<HTMLDivElement>) {
chartInstance = new ApexCharts(el, options);

chartInstance && chartInstance.render();

// setOptions增加callback方法,返回chartInstance,以便于对图表进行再操作,例如调用updateOptions方法更新图表
callback && callback(chartInstance);

}, 30);
});
}

// 新增调用ApexCharts的updateOptions方法更新图表
function updateOptions(
chartInstance: Nullable<ApexCharts>,
options,
redraw = false,
animate = true,
updateSyncedCharts = true,
overwriteInitialConfig = true,
callback) {
nextTick(() => {
useTimeoutFn(() => {

chartInstance && chartInstance.updateOptions(
options,
redraw,
animate,
updateSyncedCharts,
overwriteInitialConfig);

callback && callback(chartInstance);

}, 30);
});
}

tryOnUnmounted(() => {
if (!chartInstance) return;
Expand All @@ -28,5 +57,6 @@ export function useApexCharts(elRef: Ref<HTMLDivElement>) {

return {
setOptions,
updateOptions,
};
}

0 comments on commit 5eecec0

Please sign in to comment.