@mineadmin/echarts
Version:
基于 echarts 封装的hook,快速便捷使用 echarts
2 lines (1 loc) • 2.76 kB
JavaScript
(function(v,i){typeof exports=="object"&&typeof module<"u"?i(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],i):i((v=typeof globalThis<"u"?globalThis:v||self).useEcharts={},v.Vue)})(this,function(v,i){"use strict";typeof WorkerGlobalScope<"u"&&WorkerGlobalScope;const y=()=>{};function b(...u){let o,d,r,t,s,l,a=0,n=!0,e=y;i.isRef(u[0])||typeof u[0]!="object"?[r,t=!0,s=!0,l=!1]=u:{delay:r,trailing:t=!0,leading:s=!0,rejectOnCancel:l=!1}=u[0];const f=()=>{o&&(clearTimeout(o),o=void 0,e(),e=y)};return p=>{const c=typeof(g=r)=="function"?g():i.unref(g);var g;const m=Date.now()-a,h=()=>d=p();return f(),c<=0?(a=Date.now(),h()):(m>c&&(s||!n)?(a=Date.now(),h()):t&&(d=new Promise((w,T)=>{e=l?T:w,o=setTimeout(()=>{a=Date.now(),n=!0,w(h()),f()},Math.max(0,c-m))})),s||o||(o=setTimeout(()=>n=!0,c)),n=!1,d)}}function D(u,o=200,d=!1,r=!0,t=!1){return function(s,l){return function(...a){return new Promise((n,e)=>{Promise.resolve(s(()=>l.apply(this,a),{fn:l,thisArg:this,args:a})).then(n).catch(e)})}}(b(o,d,r,t),u)}v.default=function(u,o){const{appContext:d}=i.getCurrentInstance(),r=d.config.globalProperties.$echarts;if(!r)return null;const t=i.shallowRef(null),s=()=>t.value&&t.value.resize(),l=D(()=>{s()},100);return i.onMounted(async()=>{await(async()=>{await i.nextTick(),u.value&&(t.value=r.init(u.value,(o==null?void 0:o.theme)??"default",o))})(),window.addEventListener("resize",l),await l()}),i.onActivated(async()=>await l()),{echarts:r,getInstance:()=>t.value,setOption:(a,n)=>{i.watchEffect(()=>{t.value&&t.value.setOption(a),n&&(n==null?void 0:n.length)>0&&t.value&&n.map(e=>{var f,p;(e==null?void 0:e.type)!=="zrender"&&typeof(e==null?void 0:e.callback)=="function"&&((f=t.value)==null||f.on(e==null?void 0:e.name,(e==null?void 0:e.query)??"",c=>{e==null||e.callback(c)})),(e==null?void 0:e.type)==="zrender"&&typeof(e==null?void 0:e.callback)=="function"&&((p=t.value)==null||p.getZr().on(e==null?void 0:e.name,c=>{c.target||(e==null||e.callback(c))}))})})},showLoading:a=>{const n=(a==null?void 0:a.type)??"default",e=(a==null?void 0:a.opts)??{};t.value&&t.value.showLoading(n,e)},hideLoading:()=>t.value&&t.value.hideLoading(),clear:()=>{var a;t.value&&(t.value.dispose(),t.value.clear(),window.removeEventListener("resize",l),t.value=null,(a=u.value)==null||a.remove())},resize:s,appendData:a=>{t.value&&t.value.appendData(a)},getDom:()=>{var a;return((a=t.value)==null?void 0:a.getDom())??void 0},getWidth:()=>{var a;return((a=t.value)==null?void 0:a.getWidth())??void 0},getHeight:()=>{var a;return((a=t==null?void 0:t.value)==null?void 0:a.getHeight())??void 0},getOption:()=>t.value&&t.value.getOption()}},Object.defineProperties(v,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});