jamis
Version:
一种支持通过JSON配置方式生成页面的组件库
8 lines (7 loc) • 16.1 kB
JavaScript
import{cq as ct,eC as Mt,eD as It,eE as Ct,f8 as kt,fc as Et,bX as bt,f9 as Ft,Z as At,fd as nt,eh as Wt,eW as Pt,eQ as Rt,au as zt,h as dt,ah as Dt,bM as Ot,z as Lt}from"./install.9fa851b9.js";ct([Mt,It]);ct(Ct);kt({type:"series.wordCloud",visualStyleAccessPath:"textStyle",visualStyleMapper:function(f){return{fill:f.get("color")}},visualDrawType:"fill",optionUpdated:function(){var f=this.option;f.gridSize=Math.max(Math.floor(f.gridSize),4)},getInitialData:function(f,o){var a=Et(f.data,{coordDimensions:["value"]}),l=new bt(a,this);return l.initData(f.data),l},defaultOption:{maskImage:null,shape:"circle",keepAspect:!1,left:"center",top:"center",width:"70%",height:"80%",sizeRange:[12,60],rotationRange:[-90,90],rotationStep:45,gridSize:8,drawOutOfBound:!1,shrinkToFit:!1,textStyle:{fontWeight:"normal"}}});Ft({type:"wordCloud",render:function(f,o,a){var l=this.group;l.removeAll();var t=f.getData(),y=f.get("gridSize");f.layoutInstance.ondraw=function(h,r,T,W){var O=t.getItemModel(T),Y=O.getModel("textStyle"),I=new At({style:nt(Y),scaleX:1/W.info.mu,scaleY:1/W.info.mu,x:(W.gx+W.info.gw/2)*y,y:(W.gy+W.info.gh/2)*y,rotation:W.rot});I.setStyle({x:W.info.fillTextOffsetX,y:W.info.fillTextOffsetY+r*.5,text:h,verticalAlign:"middle",fill:t.getItemVisual(T,"style").fill,fontSize:r}),l.add(I),t.setItemGraphicEl(T,I),I.ensureState("emphasis").style=nt(O.getModel(["emphasis","textStyle"]),{state:"emphasis"}),I.ensureState("blur").style=nt(O.getModel(["blur","textStyle"]),{state:"blur"}),Wt(I,O.get(["emphasis","focus"]),O.get(["emphasis","blurScope"])),I.stateTransition={duration:f.get("animation")?f.get(["stateAnimation","duration"]):0,easing:f.get(["stateAnimation","easing"])},I.__highDownDispatcher=!0},this._model=f},remove:function(){this.group.removeAll(),this._model.layoutInstance.dispose()},dispose:function(){this._model.layoutInstance.dispose()}});/*!
* wordcloud2.js
* http://timdream.org/wordcloud2.js/
*
* Copyright 2011 - 2019 Tim Guan-tin Chien and contributors.
* Released under the MIT license
*/window.setImmediate||(window.setImmediate=function(){return window.msSetImmediate||window.webkitSetImmediate||window.mozSetImmediate||window.oSetImmediate||function(){if(!window.postMessage||!window.addEventListener)return null;var a=[void 0],l="zero-timeout-message",t=function(h){var r=a.length;return a.push(h),window.postMessage(l+r.toString(36),"*"),r};return window.addEventListener("message",function(h){if(!(typeof h.data!="string"||h.data.substr(0,l.length)!==l)){h.stopImmediatePropagation();var r=parseInt(h.data.substr(l.length),36);a[r]&&(a[r](),a[r]=void 0)}},!0),window.clearImmediate=function(h){a[h]&&(a[h]=void 0)},t}()||function(a){window.setTimeout(a,0)}}());window.clearImmediate||(window.clearImmediate=function(){return window.msClearImmediate||window.webkitClearImmediate||window.mozClearImmediate||window.oClearImmediate||function(a){window.clearTimeout(a)}}());var lt=function(){var o=document.createElement("canvas");if(!o||!o.getContext)return!1;var a=o.getContext("2d");return!(!a||!a.getImageData||!a.fillText||!Array.prototype.some||!Array.prototype.push)}(),ot=function(){if(lt){for(var o=document.createElement("canvas").getContext("2d"),a=20,l,t;a;){if(o.font=a.toString(10)+"px sans-serif",o.measureText("W").width===l&&o.measureText("m").width===t)return a+1;l=o.measureText("W").width,t=o.measureText("m").width,a--}return 0}}(),Bt=function(f){if(Array.isArray(f)){var o=f.slice();return o.splice(0,2),o}else return[]},Xt=function(o){for(var a,l,t=o.length;t;)a=Math.floor(Math.random()*t),l=o[--t],o[t]=o[a],o[a]=l;return o},Z={},Q=function(o,a){if(!lt)return;var l=Math.floor(Math.random()*Date.now());Array.isArray(o)||(o=[o]),o.forEach(function(c,e){if(typeof c=="string"){if(o[e]=document.getElementById(c),!o[e])throw new Error("The element id specified is not found.")}else if(!c.tagName&&!c.appendChild)throw new Error("You must pass valid HTML elements, or ID of the element.")});var t={list:[],fontFamily:'"Trebuchet MS", "Heiti TC", "微軟正黑體", "Arial Unicode MS", "Droid Fallback Sans", sans-serif',fontWeight:"normal",color:"random-dark",minSize:0,weightFactor:1,clearCanvas:!0,backgroundColor:"#fff",gridSize:8,drawOutOfBound:!1,shrinkToFit:!1,origin:null,drawMask:!1,maskColor:"rgba(255,0,0,0.3)",maskGapWidth:.3,layoutAnimation:!0,wait:0,abortThreshold:0,abort:function(){},minRotation:-Math.PI/2,maxRotation:Math.PI/2,rotationStep:.1,shuffle:!0,rotateRatio:.1,shape:"circle",ellipticity:.65,classes:null,hover:null,click:null};if(a)for(var y in a)y in t&&(t[y]=a[y]);if(typeof t.weightFactor!="function"){var h=t.weightFactor;t.weightFactor=function(e){return e*h}}if(typeof t.shape!="function")switch(t.shape){case"circle":default:t.shape="circle";break;case"cardioid":t.shape=function(e){return 1-Math.sin(e)};break;case"diamond":t.shape=function(e){var i=e%(2*Math.PI/4);return 1/(Math.cos(i)+Math.sin(i))};break;case"square":t.shape=function(e){return Math.min(1/Math.abs(Math.cos(e)),1/Math.abs(Math.sin(e)))};break;case"triangle-forward":t.shape=function(e){var i=e%(2*Math.PI/3);return 1/(Math.cos(i)+Math.sqrt(3)*Math.sin(i))};break;case"triangle":case"triangle-upright":t.shape=function(e){var i=(e+Math.PI*3/2)%(2*Math.PI/3);return 1/(Math.cos(i)+Math.sqrt(3)*Math.sin(i))};break;case"pentagon":t.shape=function(e){var i=(e+.955)%(2*Math.PI/5);return 1/(Math.cos(i)+.726543*Math.sin(i))};break;case"star":t.shape=function(e){var i=(e+.955)%(2*Math.PI/10);return(e+.955)%(2*Math.PI/5)-2*Math.PI/10>=0?1/(Math.cos(2*Math.PI/10-i)+3.07768*Math.sin(2*Math.PI/10-i)):1/(Math.cos(i)+3.07768*Math.sin(i))};break}t.gridSize=Math.max(Math.floor(t.gridSize),4);var r=t.gridSize,T=r-t.maskGapWidth,W=Math.abs(t.maxRotation-t.minRotation),O=Math.min(t.maxRotation,t.minRotation),Y=t.rotationStep,I,C,b,B,F,z,G;function st(c,e){return"hsl("+(Math.random()*360).toFixed()+","+(Math.random()*30+70).toFixed()+"%,"+(Math.random()*(e-c)+c).toFixed()+"%)"}switch(t.color){case"random-dark":G=function(){return st(10,50)};break;case"random-light":G=function(){return st(50,90)};break;default:typeof t.color=="function"&&(G=t.color);break}var _;typeof t.fontWeight=="function"&&(_=t.fontWeight);var $=null;typeof t.classes=="function"&&($=t.classes);var J=!1,U=[],K,ft=function(e){var i=e.currentTarget,n=i.getBoundingClientRect(),u,s;e.touches?(u=e.touches[0].clientX,s=e.touches[0].clientY):(u=e.clientX,s=e.clientY);var d=u-n.left,S=s-n.top,g=Math.floor(d*(i.width/n.width||1)/r),m=Math.floor(S*(i.height/n.height||1)/r);return U[g]?U[g][m]:null},ut=function(e){var i=ft(e);if(K!==i){if(K=i,!i){t.hover(void 0,void 0,e);return}t.hover(i.item,i.dimension,e)}},j=function(e){var i=ft(e);i&&(t.click(i.item,i.dimension,e),e.preventDefault())},tt=[],vt=function(e){if(tt[e])return tt[e];var i=e*8,n=i,u=[];for(e===0&&u.push([B[0],B[1],0]);n--;){var s=1;t.shape!=="circle"&&(s=t.shape(n/i*2*Math.PI)),u.push([B[0]+e*s*Math.cos(-n/i*2*Math.PI),B[1]+e*s*Math.sin(-n/i*2*Math.PI)*t.ellipticity,n/i*2*Math.PI])}return tt[e]=u,u},et=function(){return t.abortThreshold>0&&new Date().getTime()-z>t.abortThreshold},gt=function(){return t.rotateRatio===0||Math.random()>t.rotateRatio?0:W===0?O:O+Math.round(Math.random()*W/Y)*Y},mt=function(e,i,n,u){var s=t.weightFactor(i);if(s<=t.minSize)return!1;var d=1;s<ot&&(d=function(){for(var it=2;it*s<ot;)it+=2;return it}());var S;_?S=_(e,i,s,u):S=t.fontWeight;var g=document.createElement("canvas"),m=g.getContext("2d",{willReadFrequently:!0});m.font=S+" "+(s*d).toString(10)+"px "+t.fontFamily;var A=m.measureText(e).width/d,w=Math.max(s*d,m.measureText("m").width,m.measureText("W").width)/d,p=A+w*2,k=w*3,P=Math.ceil(p/r),R=Math.ceil(k/r);p=P*r,k=R*r;var M=-A/2,v=-w*.4,x=Math.ceil((p*Math.abs(Math.sin(n))+k*Math.abs(Math.cos(n)))/r),E=Math.ceil((p*Math.abs(Math.cos(n))+k*Math.abs(Math.sin(n)))/r),D=E*r,q=x*r;g.setAttribute("width",D),g.setAttribute("height",q),m.scale(1/d,1/d),m.translate(D*d/2,q*d/2),m.rotate(-n),m.font=S+" "+(s*d).toString(10)+"px "+t.fontFamily,m.fillStyle="#000",m.textBaseline="middle",m.fillText(e,M*d,(v+s*.5)*d);var V=m.getImageData(0,0,D,q).data;if(et())return!1;for(var ht=[],H=E,X,at,rt,L=[x/2,E/2,x/2,E/2];H--;)for(X=x;X--;){rt=r;t:for(;rt--;)for(at=r;at--;)if(V[((X*r+rt)*D+(H*r+at))*4+3]){ht.push([H,X]),H<L[3]&&(L[3]=H),H>L[1]&&(L[1]=H),X<L[0]&&(L[0]=X),X>L[2]&&(L[2]=X);break t}}return{mu:d,occupied:ht,bounds:L,gw:E,gh:x,fillTextOffsetX:M,fillTextOffsetY:v,fillTextWidth:A,fillTextHeight:w,fontSize:s}},wt=function(e,i,n,u,s){for(var d=s.length;d--;){var S=e+s[d][0],g=i+s[d][1];if(S>=C||g>=b||S<0||g<0){if(!t.drawOutOfBound)return!1;continue}if(!I[S][g])return!1}return!0},pt=function(e,i,n,u,s,d,S,g,m,A){var w=n.fontSize,p;G?p=G(u,s,w,d,S,A):p=t.color;var k;_?k=_(u,s,w,A):k=t.fontWeight;var P;$?P=$(u,s,w,A):P=t.classes,o.forEach(function(R){if(R.getContext){var M=R.getContext("2d"),v=n.mu;M.save(),M.scale(1/v,1/v),M.font=k+" "+(w*v).toString(10)+"px "+t.fontFamily,M.fillStyle=p,M.translate((e+n.gw/2)*r*v,(i+n.gh/2)*r*v),g!==0&&M.rotate(-g),M.textBaseline="middle",M.fillText(u,n.fillTextOffsetX*v,(n.fillTextOffsetY+w*.5)*v),M.restore()}else{var x=document.createElement("span"),E="";E="rotate("+-g/Math.PI*180+"deg) ",n.mu!==1&&(E+="translateX(-"+n.fillTextWidth/4+"px) scale("+1/n.mu+")");var D={position:"absolute",display:"block",font:k+" "+w*n.mu+"px "+t.fontFamily,left:(e+n.gw/2)*r+n.fillTextOffsetX+"px",top:(i+n.gh/2)*r+n.fillTextOffsetY+"px",width:n.fillTextWidth+"px",height:n.fillTextHeight+"px",lineHeight:w+"px",whiteSpace:"nowrap",transform:E,webkitTransform:E,msTransform:E,transformOrigin:"50% 40%",webkitTransformOrigin:"50% 40%",msTransformOrigin:"50% 40%"};p&&(D.color=p),x.textContent=u;for(var q in D)x.style[q]=D[q];if(m)for(var V in m)x.setAttribute(V,m[V]);P&&(x.className+=P),R.appendChild(x)}})},yt=function(e,i,n,u,s){if(!(e>=C||i>=b||e<0||i<0)){if(I[e][i]=!1,n){var d=o[0].getContext("2d");d.fillRect(e*r,i*r,T,T)}J&&(U[e][i]={item:s,dimension:u})}},xt=function(e,i,n,u,s,d){var S=s.occupied,g=t.drawMask,m;g&&(m=o[0].getContext("2d"),m.save(),m.fillStyle=t.maskColor);var A;if(J){var w=s.bounds;A={x:(e+w[3])*r,y:(i+w[0])*r,w:(w[1]-w[3]+1)*r,h:(w[2]-w[0]+1)*r}}for(var p=S.length;p--;){var k=e+S[p][0],P=i+S[p][1];k>=C||P>=b||k<0||P<0||yt(k,P,g,A,d)}g&&m.restore()},St=function c(e,i){if(i>20)return null;var n,u,s;Array.isArray(e)?(n=e[0],u=e[1]):(n=e.word,u=e.weight,s=e.attributes);var d=gt(),S=Bt(e),g=mt(n,u,d,S);if(!g||et())return!1;if(!t.drawOutOfBound&&!t.shrinkToFit){var m=g.bounds;if(m[1]-m[3]+1>C||m[2]-m[0]+1>b)return!1}for(var A=F+1,w=function(R){var M=Math.floor(R[0]-g.gw/2),v=Math.floor(R[1]-g.gh/2),x=g.gw,E=g.gh;return wt(M,v,x,E,g.occupied)?(pt(M,v,g,n,u,F-A,R[2],d,s,S),xt(M,v,x,E,g,e),{gx:M,gy:v,rot:d,info:g}):!1};A--;){var p=vt(F-A);t.shuffle&&(p=[].concat(p),Xt(p));for(var k=0;k<p.length;k++){var P=w(p[k]);if(P)return P}}return t.shrinkToFit?(Array.isArray(e)?e[1]=e[1]*3/4:e.weight=e.weight*3/4,c(e,i+1)):null},N=function(e,i,n){if(i)return!o.some(function(u){var s=new CustomEvent(e,{detail:n||{}});return!u.dispatchEvent(s)},this);o.forEach(function(u){var s=new CustomEvent(e,{detail:n||{}});u.dispatchEvent(s)},this)},Tt=function(){var e=o[0];if(e.getContext)C=Math.ceil(e.width/r),b=Math.ceil(e.height/r);else{var i=e.getBoundingClientRect();C=Math.ceil(i.width/r),b=Math.ceil(i.height/r)}if(N("wordcloudstart",!0)){B=t.origin?[t.origin[0]/r,t.origin[1]/r]:[C/2,b/2],F=Math.floor(Math.sqrt(C*C+b*b)),I=[];var n,u,s;if(!e.getContext||t.clearCanvas)for(o.forEach(function(v){if(v.getContext){var x=v.getContext("2d");x.fillStyle=t.backgroundColor,x.clearRect(0,0,C*(r+1),b*(r+1)),x.fillRect(0,0,C*(r+1),b*(r+1))}else v.textContent="",v.style.backgroundColor=t.backgroundColor,v.style.position="relative"}),n=C;n--;)for(I[n]=[],u=b;u--;)I[n][u]=!0;else{var d=document.createElement("canvas").getContext("2d");d.fillStyle=t.backgroundColor,d.fillRect(0,0,1,1);var S=d.getImageData(0,0,1,1).data,g=e.getContext("2d").getImageData(0,0,C*r,b*r).data;n=C;for(var m,A;n--;)for(I[n]=[],u=b;u--;){A=r;t:for(;A--;)for(m=r;m--;)for(s=4;s--;)if(g[((u*r+A)*C*r+(n*r+m))*4+s]!==S[s]){I[n][u]=!1;break t}I[n][u]!==!1&&(I[n][u]=!0)}g=d=S=void 0}if(t.hover||t.click){for(J=!0,n=C+1;n--;)U[n]=[];t.hover&&e.addEventListener("mousemove",ut),t.click&&(e.addEventListener("click",j),e.addEventListener("touchstart",j),e.addEventListener("touchend",function(v){v.preventDefault()}),e.style.webkitTapHighlightColor="rgba(0, 0, 0, 0)"),e.addEventListener("wordcloudstart",function v(){e.removeEventListener("wordcloudstart",v),e.removeEventListener("mousemove",ut),e.removeEventListener("click",j),K=void 0})}s=0;var w,p,k=!0;t.layoutAnimation?t.wait!==0?(w=window.setTimeout,p=window.clearTimeout):(w=window.setImmediate,p=window.clearImmediate):(w=function(v){v()},p=function(){k=!1});var P=function(x,E){o.forEach(function(D){D.addEventListener(x,E)},this)},R=function(x,E){o.forEach(function(D){D.removeEventListener(x,E)},this)},M=function v(){R("wordcloudstart",v),p(Z[l])};P("wordcloudstart",M),Z[l]=(t.layoutAnimation?w:setTimeout)(function v(){if(k){if(s>=t.list.length){p(Z[l]),N("wordcloudstop",!1),R("wordcloudstart",M),delete Z[l];return}z=new Date().getTime();var x=St(t.list[s],0),E=!N("wordclouddrawn",!0,{item:t.list[s],drawn:x});if(et()||E){p(Z[l]),t.abort(),N("wordcloudabort",!1),N("wordcloudstop",!1),R("wordcloudstart",M);return}s++,Z[l]=w(v,t.wait)}},t.wait)}};Tt()};Q.isSupported=lt;Q.minFontSize=ot;if(!Q.isSupported)throw new Error("Sorry your browser not support wordCloud");function Yt(f){for(var o=f.getContext("2d"),a=o.getImageData(0,0,f.width,f.height),l=o.createImageData(a),t=0,y=0,h=0;h<a.data.length;h+=4){var r=a.data[h+3];if(r>128){var T=a.data[h]+a.data[h+1]+a.data[h+2];t+=T,++y}}for(var W=t/y,h=0;h<a.data.length;h+=4){var T=a.data[h]+a.data[h+1]+a.data[h+2],r=a.data[h+3];r<128||T>W?(l.data[h]=0,l.data[h+1]=0,l.data[h+2]=0,l.data[h+3]=0):(l.data[h]=255,l.data[h+1]=255,l.data[h+2]=255,l.data[h+3]=255)}o.putImageData(l,0,0)}Pt(function(f,o){f.eachSeriesByType("wordCloud",function(a){var l=Ot(a.getBoxLayoutParams(),{width:o.getWidth(),height:o.getHeight()}),t=a.get("keepAspect"),y=a.get("maskImage"),h=y?y.width/y.height:1;t&&Gt(l,h);var r=a.getData(),T=document.createElement("canvas");T.width=l.width,T.height=l.height;var W=T.getContext("2d");if(y)try{W.drawImage(y,0,0,T.width,T.height),Yt(T)}catch(F){console.error("Invalid mask image"),console.error(F.toString())}var O=a.get("sizeRange"),Y=a.get("rotationRange"),I=r.getDataExtent("value"),C=Math.PI/180,b=a.get("gridSize");Q(T,{list:r.mapArray("value",function(F,z){var G=r.getItemModel(z);return[r.getName(z),G.get("textStyle.fontSize",!0)||Lt(F,I,O),z]}).sort(function(F,z){return z[1]-F[1]}),fontFamily:a.get("textStyle.fontFamily")||a.get("emphasis.textStyle.fontFamily")||f.get("textStyle.fontFamily"),fontWeight:a.get("textStyle.fontWeight")||a.get("emphasis.textStyle.fontWeight")||f.get("textStyle.fontWeight"),gridSize:b,ellipticity:l.height/l.width,minRotation:Y[0]*C,maxRotation:Y[1]*C,clearCanvas:!y,rotateRatio:1,rotationStep:a.get("rotationStep")*C,drawOutOfBound:a.get("drawOutOfBound"),shrinkToFit:a.get("shrinkToFit"),layoutAnimation:a.get("layoutAnimation"),shuffle:!1,shape:a.get("shape")});function B(F){var z=F.detail.item;F.detail.drawn&&a.layoutInstance.ondraw&&(F.detail.drawn.gx+=l.x/b,F.detail.drawn.gy+=l.y/b,a.layoutInstance.ondraw(z[0],z[1],z[2],F.detail.drawn))}T.addEventListener("wordclouddrawn",B),a.layoutInstance&&a.layoutInstance.dispose(),a.layoutInstance={ondraw:null,dispose:function(){T.removeEventListener("wordclouddrawn",B),T.addEventListener("wordclouddrawn",function(F){F.preventDefault()})}}})});Rt(function(f){var o=(f||{}).series;!zt(o)&&(o=o?[o]:[]);var a=["shadowColor","shadowBlur","shadowOffsetX","shadowOffsetY"];dt(o,function(t){if(t&&t.type==="wordCloud"){var y=t.textStyle||{};l(y.normal),l(y.emphasis)}});function l(t){t&&dt(a,function(y){t.hasOwnProperty(y)&&(t["text"+Dt(y)]=t[y])})}});function Gt(f,o){var a=f.width,l=f.height;a>l*o?(f.x+=(a-l*o)/2,f.width=l*o):(f.y+=(l-a/o)/2,f.height=a/o)}