UNPKG

vislite

Version:

灵活、快速、简单的数据可视化交互式跨端前端库

1 lines 11.9 kB
var t=function(r,e){return t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,r){t.__proto__=r}||function(t,r){for(var e in r)Object.prototype.hasOwnProperty.call(r,e)&&(t[e]=r[e])},t(r,e)};"function"==typeof SuppressedError&&SuppressedError;var r=["href","title","show","type","role","actuate"],e=function(t,r){o(t,"stroke-dasharray",r.lineDash.join(",")),o(t,"stroke-width",r.lineWidth+""),o(t,"stroke-linecap",r.lineCap+""),o(t,"stroke-linejoin",r.lineJoin+"")};function i(t){return document.createElementNS("http://www.w3.org/2000/svg",t)}var n=function(t,e,i){r.indexOf(e)>-1?t.setAttributeNS("http://www.w3.org/1999/xlink","xlink:"+e,i+""):t.setAttribute(e,i+"")},o=n;function s(t,r){e(t,r),n(t,"stroke",r.strokeStyle),n(t,"fill",r.fillStyle)}function a(t,r){n(t,"fill",r.fillStyle)}function h(t,r){e(t,r),n(t,"stroke",r.strokeStyle),n(t,"fill","none")}var _=function(t,r,e,i,n){var o=Math.cos(e),s=Math.sin(e);return[+((i-t)*o-(n-r)*s+t).toFixed(7),+((i-t)*s+(n-r)*o+r).toFixed(7)]};var u=function(t){var r=t.getElementsByTagName("defs");if(r.length<=0){var e=i("defs");return t.appendChild(e),e}return r[0]},f=function(t,r,e,i,n){if("text"!==t.nodeName.toLowerCase())throw new Error("Need a <text> !");o(t,"dy",{top:.5*r.fontSize,middle:0,bottom:.5*-r.fontSize}[r.textBaseline]),function(t,r){for(var e in r)t.style[e]=r[e]}(t,{"text-anchor":{left:"start",right:"end",center:"middle"}[r.textAlign],"dominant-baseline":"central","font-size":r.fontSize+"px","font-family":r.fontFamily}),o(t,"x",e),o(t,"y",i),"number"==typeof n&&o(t,"transform","rotate("+(n%=360)+","+e+","+i+")")},c=function(t,r,e,i){if("circle"!==t.nodeName.toLowerCase())throw new Error("Need a <circle> !");o(t,"cx",r),o(t,"cy",e),o(t,"r",i)},l=function(t,r){if("path"!==t.nodeName.toLowerCase())throw new Error("Need a <path> !");o(t,"d",r)},p=function(t,r,e,i,n,s){if("path"!==t.nodeName.toLowerCase())throw new Error("Need a <path> !");var a=[];if(Array.isArray(r.rectRadius)&&r.rectRadius.length>0)for(var h=0;h<4;h++)a.push.apply(a,r.rectRadius);s<0&&(i-=s*=-1),n<0&&(e-=n*=-1);var _="";a.length>=4?(_="M"+(e+a[0])+" "+i,_+="L"+(e+n-a[1])+" "+i,_+="A"+a[1]+" "+a[1]+" 0 0 1 "+(e+n)+" "+(i+a[1]),_+="L"+(e+n)+" "+(i+s-a[2]),_+="A"+a[2]+" "+a[2]+" 0 0 1 "+(e+n-a[2])+" "+(i+s),_+="L"+(e+a[3])+" "+(i+s),_+="A"+a[3]+" "+a[3]+" 0 0 1 "+e+" "+(i+s-a[3]),_+="L"+e+" "+(i+a[0]),_+="A"+a[0]+" "+a[0]+" 0 0 1 "+(e+a[0])+" "+i):(_="M"+e+" "+i,_+="L"+(e+n)+" "+i,_+="L"+(e+n)+" "+(i+s),_+="L"+e+" "+(i+s),_+="Z"),o(t,"d",_)},d=function(t,r,e,i,n,s,a,h){if("path"!==t.nodeName.toLowerCase())throw new Error("Need a <path> !");if(a=a/180*Math.PI,h=h/180*Math.PI,a%=2*Math.PI,n>s){var u=n;n=s,s=u}h>=1.999999*Math.PI||h<=1.999999*-Math.PI?h=1.999999*Math.PI:h%=2*Math.PI,function(t,r,e,i,n,o,s){r<0&&(t+=r,r*=-1);var a,h=[];a=_(0,0,t,n,0),h[0]=a[0],h[1]=a[1],a=_(0,0,r,a[0],a[1]),h[2]=a[0],h[3]=a[1],a=_(0,0,t,o,0),h[4]=a[0],h[5]=a[1],a=_(0,0,r,a[0],a[1]),h[6]=a[0],h[7]=a[1],s(t,t+r,h[0]+e,h[1]+i,h[4]+e,h[5]+i,h[2]+e,h[3]+i,h[6]+e,h[7]+i,.5*(o-n))}(a,h,e,i,n,s,(function(e,i,a,h,_,u,f,c,l,p,d){var y=i-e>Math.PI?1:0,m="M"+a+" "+h;d<0&&(d=-d),m+="A"+n+" "+n+" 0 "+y+" 1 "+f+" "+c,"round"==r.arcEndCap?m+="A"+d+" "+d+" 0 1 0 "+l+" "+p:"-round"==r.arcEndCap?m+="A"+d+" "+d+" 0 1 1 "+l+" "+p:m+="L"+l+" "+p,m+="A"+s+" "+s+" 0 "+y+" 0 "+_+" "+u,"round"==r.arcStartCap?m+="A"+d+" "+d+" 0 1 0 "+a+" "+h:"-round"==r.arcStartCap?m+="A"+d+" "+d+" 0 1 1 "+a+" "+h:m+="L"+a+" "+h,"butt"==r.arcStartCap&&(m+="Z"),o(t,"d",m)}))};function y(t,r,e,i,n){var o=Math.cos(e),s=Math.sin(e);return[(i-t)*o-(n-r)*s+t,(i-t)*s+(n-r)*o+r]}var m=function(t,r){var e={value:function(){return"url(#"+r+")"},setColor:function(r,n){var s=i("stop");return t.appendChild(s),o(s,"offset",100*r+"%"),o(s,"style","stop-color:"+n+";"),e}};return e};var g=function(){function t(t){this.name="SVG",this.__config={fillStyle:"rgb(0,0,0)",strokeStyle:"rgb(0,0,0)",textAlign:"left",textBaseline:"middle",fontSize:16,fontFamily:"sans-serif",arcStartCap:"butt",arcEndCap:"butt",lineDash:[],lineWidth:1,lineCap:"butt",lineJoin:"miter",rectRadius:[]},this.__path="",this.__currentPosition=[],this.__transform_history=[],this.__transform_current="",this.__svg=t}return t.prototype.config=function(t){if("object"!=typeof t)return this.__config[t];for(var r in t)this.__config[r]=t[r];return this},t.prototype.reset=function(t){return void 0===t&&(t={}),this.config(function(t,r){for(var e in t)r[e]=t[e];return r}(t,{fillStyle:"rgb(0,0,0)",strokeStyle:"rgb(0,0,0)",textAlign:"left",textBaseline:"middle",fontSize:16,fontFamily:"sans-serif",arcStartCap:"butt",arcEndCap:"butt",lineDash:[],lineWidth:1,lineCap:"butt",lineJoin:"miter",rectRadius:[]})),this},t.prototype.useEl=function(t){return this.__useEl=t,this},t.prototype.getEl=function(){return this.__useEl},t.prototype.appendEl=function(t,r){return r=r||this.__svg,"string"==typeof t&&(t=i(t)),r.appendChild(t),this.__useEl=t,this},t.prototype.appendBoard=function(t,r){var e=t;if("string"==typeof t&&(e={text:"text",path:"path",arc:"path",circle:"circle",rect:"path"}[t]||""),""==e)throw new Error("Unsupported drawing method:"+t);return this.appendEl(e,r)},t.prototype.remove=function(){if(!this.__useEl)throw new Error("Currently, no node can be deleted.");return this.__useEl.parentNode.removeChild(this.__useEl),this},t.prototype.attr=function(t){if(!this.__useEl)throw new Error("Currently, no node can be modified or viewed.");if("object"!=typeof t)return function(t,e){return r.indexOf(e)>-1&&(e="xlink:"+e),t.getAttribute(e)}(this.__useEl,t);for(var e in t)o(this.__useEl,e,t[e]);return this},t.prototype.fillText=function(t,r,e,i){return void 0===i&&(i=0),f(this.__useEl,this.__config,r,e,i),this.attr({transform:this.__transform_current}),this.__useEl.textContent=t,a(this.__useEl,this.__config),this},t.prototype.strokeText=function(t,r,e,i){return void 0===i&&(i=0),f(this.__useEl,this.__config,r,e,i),this.attr({transform:this.__transform_current}),this.__useEl.textContent=t,h(this.__useEl,this.__config),this},t.prototype.fullText=function(t,r,e,i){return void 0===i&&(i=0),f(this.__useEl,this.__config,r,e,i),this.attr({transform:this.__transform_current}),this.__useEl.textContent=t,s(this.__useEl,this.__config),this},t.prototype.fillArc=function(t,r,e,i,n,o){return d(this.__useEl,this.__config,t,r,e,i,n,o),this.attr({transform:this.__transform_current}),a(this.__useEl,this.__config),this},t.prototype.strokeArc=function(t,r,e,i,n,o){return d(this.__useEl,this.__config,t,r,e,i,n,o),this.attr({transform:this.__transform_current}),h(this.__useEl,this.__config),this},t.prototype.fullArc=function(t,r,e,i,n,o){return d(this.__useEl,this.__config,t,r,e,i,n,o),this.attr({transform:this.__transform_current}),s(this.__useEl,this.__config),this},t.prototype.fillCircle=function(t,r,e){return c(this.__useEl,t,r,e),this.attr({transform:this.__transform_current}),a(this.__useEl,this.__config),this},t.prototype.strokeCircle=function(t,r,e){return c(this.__useEl,t,r,e),this.attr({transform:this.__transform_current}),h(this.__useEl,this.__config),this},t.prototype.fullCircle=function(t,r,e){return c(this.__useEl,t,r,e),this.attr({transform:this.__transform_current}),s(this.__useEl,this.__config),this},t.prototype.fillRect=function(t,r,e,i){return p(this.__useEl,this.__config,t,r,e,i),this.attr({transform:this.__transform_current}),a(this.__useEl,this.__config),this},t.prototype.strokeRect=function(t,r,e,i){return p(this.__useEl,this.__config,t,r,e,i),this.attr({transform:this.__transform_current}),h(this.__useEl,this.__config),this},t.prototype.fullRect=function(t,r,e,i){return p(this.__useEl,this.__config,t,r,e,i),this.attr({transform:this.__transform_current}),s(this.__useEl,this.__config),this},t.prototype.beginPath=function(){return this.__currentPosition=[],this.__path="",this},t.prototype.closePath=function(){return this.__path+="Z",this},t.prototype.moveTo=function(t,r){return this.__currentPosition=[t,r],this.__path+="M"+t+" "+r,this},t.prototype.lineTo=function(t,r){return this.__currentPosition=[t,r],this.__path+=(""==this.__path?"M":"L")+t+" "+r,this},t.prototype.fill=function(){return l(this.__useEl,this.__path),this.attr({transform:this.__transform_current}),a(this.__useEl,this.__config),this},t.prototype.stroke=function(){return l(this.__useEl,this.__path),this.attr({transform:this.__transform_current}),h(this.__useEl,this.__config),this},t.prototype.full=function(){return l(this.__useEl,this.__path),this.attr({transform:this.__transform_current}),s(this.__useEl,this.__config),this},t.prototype.save=function(){return this.__transform_history.push(this.__transform_current),this},t.prototype.restore=function(){return this.__transform_history.length>0&&(this.__transform_current=this.__transform_history.pop()),this},t.prototype.arc=function(t,r,e,i,n){var o=y(t,r,i/180*Math.PI,t+e,r),s=y(t,r,(i+n)/180*Math.PI,t+e,r);return""==this.__path?this.__path+="M"+o[0]+","+o[1]:o[0]==this.__currentPosition[0]&&o[1]==this.__currentPosition[1]||(this.__path+="L"+o[0]+","+o[1]),this.__path+="A"+e+","+e+" 0 "+(n>180||n<-180?1:0)+","+(n>0?1:0)+" "+s[0]+","+s[1],this},t.prototype.quadraticCurveTo=function(t,r,e,i){return this.__path+="Q"+t+" "+r+","+e+" "+i,this},t.prototype.bezierCurveTo=function(t,r,e,i,n,o){return this.__path+="C"+t+" "+r+","+e+" "+i+","+n+" "+o,this},t.prototype.bind=function(t,r){return this.__useEl.addEventListener(t,(function(t){r.call(this,t,this)}),!1),this},t.prototype.createLinearGradient=function(t,r,e,n){return function(t,r,e,n,s){var a=u(t),h="visliteLg"+(new Date).valueOf()+(1e6*Math.random()).toFixed(0),_=i("linearGradient");return a.appendChild(_),o(_,"id",h),o(_,"x1",r+"%"),o(_,"y1",e+"%"),o(_,"x2",n+"%"),o(_,"y2",s+"%"),m(_,h)}(this.__svg,t,r,e,n)},t.prototype.createRadialGradient=function(t,r,e){return function(t,r,e,n){var s=u(t),a="visliteRg"+(new Date).valueOf()+(1e6*Math.random()).toFixed(0),h=i("radialGradient");return s.appendChild(h),o(h,"id",a),o(h,"cx",r+"%"),o(h,"cy",e+"%"),o(h,"r",n+"%"),m(h,a)}(this.__svg,t,r,e)},t.prototype.translate=function(t,r){return this.__transform_current+=" translate("+t+","+r+")",this},t.prototype.rotate=function(t){return this.__transform_current+=" rotate("+t+")",this},t.prototype.install=function(t){var r=this,e=function(e){if(e in i)throw new Error("VISLite SVG:Method already exists and cannot be overwritten.");i[e]=function(){for(var i=[],n=0;n<arguments.length;n++)i[n]=arguments[n];var o=t[e].apply(r,i);return null!=o?o:r}},i=this;for(var n in t)e(n);return this},t}(),v=function(r){function e(t){var e=this;if(!t)throw new Error("VISLite SVG:The mount point requires an HTMLElement type but encountered null.");var i,n=t.clientWidth,o=t.clientHeight,s=t;return s._vislite_svg_?i=s._vislite_svg_[0]:(i=document.createElementNS("http://www.w3.org/2000/svg","svg"),t.appendChild(i),i.setAttribute("width",n+""),i.setAttribute("height",o+""),i.setAttribute("viewBox","0 0 "+n+" "+o),s._vislite_svg_=[i],t.setAttribute("vislite","SVG")),(e=r.call(this,i)||this).__el=t,e}return function(r,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function i(){this.constructor=r}t(r,e),r.prototype=null===e?Object.create(e):(i.prototype=e.prototype,new i)}(e,r),e.prototype.toDataURL=function(){var t=this;return new Promise((function(r){var e=t.__el.clientWidth,i=t.__el.clientHeight,n=document.createElement("img");n.setAttribute("width",e+""),n.setAttribute("height",i+"");var o="data:image/svg+xml;charset=utf-8,<svg xmlns='http://www.w3.org/2000/svg' width='"+e+"' height='"+i+"'>"+t.__svg.innerHTML.replace(/"/g,"'")+"</svg>";n.setAttribute("src",o),setTimeout((function(){var t=document.createElement("canvas");t.setAttribute("width",e+""),t.setAttribute("height",i+"");var o=t.getContext("2d");o&&o.drawImage(n,0,0,e,i),r(t.toDataURL())}),100)}))},e}(g);export{v as default};