UNPKG

rolandbergerprofiler

Version:
2 lines (1 loc) 16.9 kB
var rolandberger=function(){"use strict";var t="http://www.w3.org/1999/xhtml",n={svg:"http://www.w3.org/2000/svg",xhtml:t,xlink:"http://www.w3.org/1999/xlink",xml:"http://www.w3.org/XML/1998/namespace",xmlns:"http://www.w3.org/2000/xmlns/"};function e(t){var e=t+="",r=e.indexOf(":");return r>=0&&"xmlns"!==(e=t.slice(0,r))&&(t=t.slice(r+1)),n.hasOwnProperty(e)?{space:n[e],local:t}:t}function r(n){return function(){var e=this.ownerDocument,r=this.namespaceURI;return r===t&&e.documentElement.namespaceURI===t?e.createElement(n):e.createElementNS(r,n)}}function i(t){return function(){return this.ownerDocument.createElementNS(t.space,t.local)}}function o(t){var n=e(t);return(n.local?i:r)(n)}function a(){}function s(t){return null==t?a:function(){return this.querySelector(t)}}function u(){return[]}function h(t){return function(){return null==(n=t.apply(this,arguments))?[]:Array.isArray(n)?n:Array.from(n);var n}}function l(t){return function(n){return n.matches(t)}}var c=Array.prototype.find;function f(){return this.firstElementChild}var p=Array.prototype.filter;function d(){return Array.from(this.children)}function _(t){return new Array(t.length)}function y(t,n){this.ownerDocument=t.ownerDocument,this.namespaceURI=t.namespaceURI,this._next=null,this._parent=t,this.__data__=n}function g(t,n,e,r,i,o){for(var a,s=0,u=n.length,h=o.length;s<h;++s)(a=n[s])?(a.__data__=o[s],r[s]=a):e[s]=new y(t,o[s]);for(;s<u;++s)(a=n[s])&&(i[s]=a)}function m(t,n,e,r,i,o,a){var s,u,h,l=new Map,c=n.length,f=o.length,p=new Array(c);for(s=0;s<c;++s)(u=n[s])&&(p[s]=h=a.call(u,u.__data__,s,n)+"",l.has(h)?i[s]=u:l.set(h,u));for(s=0;s<f;++s)h=a.call(t,o[s],s,o)+"",(u=l.get(h))?(r[s]=u,u.__data__=o[s],l.delete(h)):e[s]=new y(t,o[s]);for(s=0;s<c;++s)(u=n[s])&&l.get(p[s])===u&&(i[s]=u)}function v(t){return t.__data__}function w(t){return"object"==typeof t&&"length"in t?t:Array.from(t)}function x(t,n){return t<n?-1:t>n?1:t>=n?0:NaN}function A(t){return function(){this.removeAttribute(t)}}function b(t){return function(){this.removeAttributeNS(t.space,t.local)}}function E(t,n){return function(){this.setAttribute(t,n)}}function M(t,n){return function(){this.setAttributeNS(t.space,t.local,n)}}function S(t,n){return function(){var e=n.apply(this,arguments);null==e?this.removeAttribute(t):this.setAttribute(t,e)}}function N(t,n){return function(){var e=n.apply(this,arguments);null==e?this.removeAttributeNS(t.space,t.local):this.setAttributeNS(t.space,t.local,e)}}function C(t){return t.ownerDocument&&t.ownerDocument.defaultView||t.document&&t||t.defaultView}function D(t){return function(){this.style.removeProperty(t)}}function B(t,n,e){return function(){this.style.setProperty(t,n,e)}}function L(t,n,e){return function(){var r=n.apply(this,arguments);null==r?this.style.removeProperty(t):this.style.setProperty(t,r,e)}}function I(t){return function(){delete this[t]}}function R(t,n){return function(){this[t]=n}}function z(t,n){return function(){var e=n.apply(this,arguments);null==e?delete this[t]:this[t]=e}}function O(t){return t.trim().split(/^|\s+/)}function P(t){return t.classList||new q(t)}function q(t){this._node=t,this._names=O(t.getAttribute("class")||"")}function k(t,n){for(var e=P(t),r=-1,i=n.length;++r<i;)e.add(n[r])}function T(t,n){for(var e=P(t),r=-1,i=n.length;++r<i;)e.remove(n[r])}function $(t){return function(){k(this,t)}}function W(t){return function(){T(this,t)}}function j(t,n){return function(){(n.apply(this,arguments)?k:T)(this,t)}}function H(){this.textContent=""}function U(t){return function(){this.textContent=t}}function V(t){return function(){var n=t.apply(this,arguments);this.textContent=null==n?"":n}}function F(){this.innerHTML=""}function X(t){return function(){this.innerHTML=t}}function G(t){return function(){var n=t.apply(this,arguments);this.innerHTML=null==n?"":n}}function J(){this.nextSibling&&this.parentNode.appendChild(this)}function K(){this.previousSibling&&this.parentNode.insertBefore(this,this.parentNode.firstChild)}function Q(){return null}function Y(){var t=this.parentNode;t&&t.removeChild(this)}function Z(){var t=this.cloneNode(!1),n=this.parentNode;return n?n.insertBefore(t,this.nextSibling):t}function tt(){var t=this.cloneNode(!0),n=this.parentNode;return n?n.insertBefore(t,this.nextSibling):t}function nt(t){return function(){var n=this.__on;if(n){for(var e,r=0,i=-1,o=n.length;r<o;++r)e=n[r],t.type&&e.type!==t.type||e.name!==t.name?n[++i]=e:this.removeEventListener(e.type,e.listener,e.options);++i?n.length=i:delete this.__on}}}function et(t,n,e){return function(){var r,i=this.__on,o=function(t){return function(n){t.call(this,n,this.__data__)}}(n);if(i)for(var a=0,s=i.length;a<s;++a)if((r=i[a]).type===t.type&&r.name===t.name)return this.removeEventListener(r.type,r.listener,r.options),this.addEventListener(r.type,r.listener=o,r.options=e),void(r.value=n);this.addEventListener(t.type,o,e),r={type:t.type,name:t.name,value:n,listener:o,options:e},i?i.push(r):this.__on=[r]}}function rt(t,n,e){var r=C(t),i=r.CustomEvent;"function"==typeof i?i=new i(n,e):(i=r.document.createEvent("Event"),e?(i.initEvent(n,e.bubbles,e.cancelable),i.detail=e.detail):i.initEvent(n,!1,!1)),t.dispatchEvent(i)}function it(t,n){return function(){return rt(this,t,n)}}function ot(t,n){return function(){return rt(this,t,n.apply(this,arguments))}}y.prototype={constructor:y,appendChild:function(t){return this._parent.insertBefore(t,this._next)},insertBefore:function(t,n){return this._parent.insertBefore(t,n)},querySelector:function(t){return this._parent.querySelector(t)},querySelectorAll:function(t){return this._parent.querySelectorAll(t)}},q.prototype={add:function(t){this._names.indexOf(t)<0&&(this._names.push(t),this._node.setAttribute("class",this._names.join(" ")))},remove:function(t){var n=this._names.indexOf(t);n>=0&&(this._names.splice(n,1),this._node.setAttribute("class",this._names.join(" ")))},contains:function(t){return this._names.indexOf(t)>=0}};var at=[null];function st(t,n){this._groups=t,this._parents=n}function ut(t){return"string"==typeof t?new st([[document.querySelector(t)]],[document.documentElement]):new st([[t]],at)}st.prototype={constructor:st,select:function(t){"function"!=typeof t&&(t=s(t));for(var n=this._groups,e=n.length,r=new Array(e),i=0;i<e;++i)for(var o,a,u=n[i],h=u.length,l=r[i]=new Array(h),c=0;c<h;++c)(o=u[c])&&(a=t.call(o,o.__data__,c,u))&&("__data__"in o&&(a.__data__=o.__data__),l[c]=a);return new st(r,this._parents)},selectAll:function(t){t="function"==typeof t?h(t):function(t){return null==t?u:function(){return this.querySelectorAll(t)}}(t);for(var n=this._groups,e=n.length,r=[],i=[],o=0;o<e;++o)for(var a,s=n[o],l=s.length,c=0;c<l;++c)(a=s[c])&&(r.push(t.call(a,a.__data__,c,s)),i.push(a));return new st(r,i)},selectChild:function(t){return this.select(null==t?f:function(t){return function(){return c.call(this.children,t)}}("function"==typeof t?t:l(t)))},selectChildren:function(t){return this.selectAll(null==t?d:function(t){return function(){return p.call(this.children,t)}}("function"==typeof t?t:l(t)))},filter:function(t){"function"!=typeof t&&(t=function(t){return function(){return this.matches(t)}}(t));for(var n=this._groups,e=n.length,r=new Array(e),i=0;i<e;++i)for(var o,a=n[i],s=a.length,u=r[i]=[],h=0;h<s;++h)(o=a[h])&&t.call(o,o.__data__,h,a)&&u.push(o);return new st(r,this._parents)},data:function(t,n){if(!arguments.length)return Array.from(this,v);var e,r=n?m:g,i=this._parents,o=this._groups;"function"!=typeof t&&(e=t,t=function(){return e});for(var a=o.length,s=new Array(a),u=new Array(a),h=new Array(a),l=0;l<a;++l){var c=i[l],f=o[l],p=f.length,d=w(t.call(c,c&&c.__data__,l,i)),_=d.length,y=u[l]=new Array(_),x=s[l]=new Array(_);r(c,f,y,x,h[l]=new Array(p),d,n);for(var A,b,E=0,M=0;E<_;++E)if(A=y[E]){for(E>=M&&(M=E+1);!(b=x[M])&&++M<_;);A._next=b||null}}return(s=new st(s,i))._enter=u,s._exit=h,s},enter:function(){return new st(this._enter||this._groups.map(_),this._parents)},exit:function(){return new st(this._exit||this._groups.map(_),this._parents)},join:function(t,n,e){var r=this.enter(),i=this,o=this.exit();return"function"==typeof t?(r=t(r))&&(r=r.selection()):r=r.append(t+""),null!=n&&(i=n(i))&&(i=i.selection()),null==e?o.remove():e(o),r&&i?r.merge(i).order():i},merge:function(t){for(var n=t.selection?t.selection():t,e=this._groups,r=n._groups,i=e.length,o=r.length,a=Math.min(i,o),s=new Array(i),u=0;u<a;++u)for(var h,l=e[u],c=r[u],f=l.length,p=s[u]=new Array(f),d=0;d<f;++d)(h=l[d]||c[d])&&(p[d]=h);for(;u<i;++u)s[u]=e[u];return new st(s,this._parents)},selection:function(){return this},order:function(){for(var t=this._groups,n=-1,e=t.length;++n<e;)for(var r,i=t[n],o=i.length-1,a=i[o];--o>=0;)(r=i[o])&&(a&&4^r.compareDocumentPosition(a)&&a.parentNode.insertBefore(r,a),a=r);return this},sort:function(t){function n(n,e){return n&&e?t(n.__data__,e.__data__):!n-!e}t||(t=x);for(var e=this._groups,r=e.length,i=new Array(r),o=0;o<r;++o){for(var a,s=e[o],u=s.length,h=i[o]=new Array(u),l=0;l<u;++l)(a=s[l])&&(h[l]=a);h.sort(n)}return new st(i,this._parents).order()},call:function(){var t=arguments[0];return arguments[0]=this,t.apply(null,arguments),this},nodes:function(){return Array.from(this)},node:function(){for(var t=this._groups,n=0,e=t.length;n<e;++n)for(var r=t[n],i=0,o=r.length;i<o;++i){var a=r[i];if(a)return a}return null},size:function(){let t=0;for(const n of this)++t;return t},empty:function(){return!this.node()},each:function(t){for(var n=this._groups,e=0,r=n.length;e<r;++e)for(var i,o=n[e],a=0,s=o.length;a<s;++a)(i=o[a])&&t.call(i,i.__data__,a,o);return this},attr:function(t,n){var r=e(t);if(arguments.length<2){var i=this.node();return r.local?i.getAttributeNS(r.space,r.local):i.getAttribute(r)}return this.each((null==n?r.local?b:A:"function"==typeof n?r.local?N:S:r.local?M:E)(r,n))},style:function(t,n,e){return arguments.length>1?this.each((null==n?D:"function"==typeof n?L:B)(t,n,null==e?"":e)):function(t,n){return t.style.getPropertyValue(n)||C(t).getComputedStyle(t,null).getPropertyValue(n)}(this.node(),t)},property:function(t,n){return arguments.length>1?this.each((null==n?I:"function"==typeof n?z:R)(t,n)):this.node()[t]},classed:function(t,n){var e=O(t+"");if(arguments.length<2){for(var r=P(this.node()),i=-1,o=e.length;++i<o;)if(!r.contains(e[i]))return!1;return!0}return this.each(("function"==typeof n?j:n?$:W)(e,n))},text:function(t){return arguments.length?this.each(null==t?H:("function"==typeof t?V:U)(t)):this.node().textContent},html:function(t){return arguments.length?this.each(null==t?F:("function"==typeof t?G:X)(t)):this.node().innerHTML},raise:function(){return this.each(J)},lower:function(){return this.each(K)},append:function(t){var n="function"==typeof t?t:o(t);return this.select((function(){return this.appendChild(n.apply(this,arguments))}))},insert:function(t,n){var e="function"==typeof t?t:o(t),r=null==n?Q:"function"==typeof n?n:s(n);return this.select((function(){return this.insertBefore(e.apply(this,arguments),r.apply(this,arguments)||null)}))},remove:function(){return this.each(Y)},clone:function(t){return this.select(t?tt:Z)},datum:function(t){return arguments.length?this.property("__data__",t):this.node().__data__},on:function(t,n,e){var r,i,o=function(t){return t.trim().split(/^|\s+/).map((function(t){var n="",e=t.indexOf(".");return e>=0&&(n=t.slice(e+1),t=t.slice(0,e)),{type:t,name:n}}))}(t+""),a=o.length;if(!(arguments.length<2)){for(s=n?et:nt,r=0;r<a;++r)this.each(s(o[r],n,e));return this}var s=this.node().__on;if(s)for(var u,h=0,l=s.length;h<l;++h)for(r=0,u=s[h];r<a;++r)if((i=o[r]).type===u.type&&i.name===u.name)return u.value},dispatch:function(t,n){return this.each(("function"==typeof n?ot:it)(t,n))},[Symbol.iterator]:function*(){for(var t=this._groups,n=0,e=t.length;n<e;++n)for(var r,i=t[n],o=0,a=i.length;o<a;++o)(r=i[o])&&(yield r)}};var ht=0;function lt(){this._="@"+(++ht).toString(36)}lt.prototype={constructor:lt,get:function(t){for(var n=this._;!(n in t);)if(!(t=t.parentNode))return;return t[n]},set:function(t,n){return t[this._]=n},remove:function(t){return this._ in t&&delete t[this._]},toString:function(){return this._}};class ct{options;_w=0;_h=0;_data=[];_container;_canvas;constructor(t){this.options=t,this.init()}init(){const{width:t,height:n,margin:e,threshold:r}=this.options;this._w=t-e.left-e.right,this._h=n-e.top-e.bottom,this._data=this.options.data.map((t=>({...t,x:t.x*this._w,y:t.y*this._h,value:t.value-r}))),this._container=document.getElementById(this.options.id),this._container.style.width=`${t}px`,this._container.style.height=`${n}px`,this._container.style.position="relative",this._canvas=document.getElementById(`${this.options.id}_canvas`),this.drawWord(),this.prepareData()}getWeight(t,n,e,r,i){return Math.pow(Math.E,-.5*(Math.pow(t-e,2)+Math.pow(n-r,2))/i/i)}draw(t,n){this._canvas&&this._container.removeChild(this._canvas),this._canvas=document.createElement("canvas");const e=this._canvas.getContext("2d");this._canvas.id=`${this.options.id}_canvas`,this._canvas.style.cssText="position:absolute;top:20px;left:20px;",this._container.appendChild(this._canvas);const r=document.createElement("canvas"),i=r.getContext("2d"),o=this._w,a=this._h;this._canvas.width=r.width=o,this._canvas.height=r.height=a,i.clearRect(0,0,o,a);let s=i.getImageData(0,0,o,a),u=s.data;const{pos:h,neg:l}=this.options.color,{r:c,g:f,b:p}=h,{r:d,g:_,b:y}=l;for(let e=0,r=u.length;e<r;e+=4){let r=t[Math.floor(e/4)];r<0?(u[e]=c,u[e+1]=f,u[e+2]=p,u[e+3]=Math.round(.1*Math.floor(Math.abs(r)/n/.1)*255)):(u[e]=d,u[e+1]=_,u[e+2]=y,u[e+3]=Math.round(.1*Math.floor(Math.abs(r)/n/.1)*255))}e&&e.putImageData(s,0,0)}prepareData(){const t=this._w,n=this._h,e=this._data.slice();let r=[],i=0,o=this.options.rangeRatio*Math.min(t,n);for(let a=0,s=t*n;a<s;a++){let n=a%t,s=Math.floor(a/t),u=0;e.forEach((t=>{let e=o*t.value,r=this.getWeight(n,s,t.x,t.y,e);u+=r*t.value})),r[a]=u,Math.abs(u)>i&&(i=Math.abs(u))}this.draw(r,i)}drawWord(){const{margin:t,id:n}=this.options,e=this._w,r=this._h;ut(this._container).selectAll("svg").remove();const i=ut(this._container).append("svg").attr("width",e+t.left+t.right).attr("height",r+t.top+t.bottom).attr("style","position:absolute;z-index:50;").append("g").attr("transform","translate("+t.left+","+t.top+")");[{x1:0,y1:r/2,x2:e,y2:r/2},{x1:e/2,y1:0,x2:e/2,y2:r}].forEach((t=>{i.append("line").attr("x1",t.x1).attr("y1",t.y1).attr("x2",t.x2).attr("y2",t.y2).attr("stroke","#E4E9EDFF").attr("stroke-width","1px")})),i.selectAll(`.tag-name-${n}`).data(this._data).enter().append("text").attr("class",`tag-name-${n}`).attr("x",(t=>t.x)).attr("y",(t=>t.y)).attr("dominant-baseline","central").attr("text-anchor","middle").attr("data",(t=>t.name)).style("cursor","pointer").style("fill","#333").text((t=>t.name)).style("font-size","14px"),i.append("rect").attr("fill","none").attr("stroke","#E4E9ED").attr("width",e).attr("height",r);const o=[{x:0,y:(s=r)/2+1,text:"-",width:26,height:26},{x:a=e,y:s/2+1,text:"+",width:26,height:26},{x:a/2,y:0,text:"E",width:26,height:26},{x:a/2,y:s,text:"R",width:26,height:26}];var a,s;const u=[{x:10,y:20,text:"简约感性区"},{x:e-90,y:20,text:"消费升级区"},{x:10,y:r-10,text:"价格敏感区"},{x:e-90,y:r-10,text:"体验理性区"}];i.selectAll(".title").data(o).enter().append("rect").attr("class","title").attr("fill","#fff").attr("width",(t=>t.width)).attr("height",(t=>t.height)).attr("stroke","#E4E9ED").attr("x",(t=>t.x-t.width/2)).attr("y",(t=>t.y-t.height/2)).attr("rx",4).attr("ry",4),i.selectAll(".area").data(u).enter().append("text").attr("class","area").attr("x",(t=>t.x)).attr("y",(t=>t.y)).style("fill","#A0A6AE").text((t=>t.text)),i.selectAll(".orient").data(o).enter().append("text").attr("class","orient").attr("x",(t=>t.x)).attr("y",(t=>t.y)).attr("dominant-baseline","central").attr("text-anchor","middle").style("font-size","12px").style("fill","#666").style("font-family","monospace").text((t=>t.text))}}const ft={pos:{r:255,g:0,b:0},neg:{r:0,g:0,b:255}},pt={top:20,right:20,bottom:20,left:20};class dt{id="";_dom;_opts;_model;constructor(t,n){this._opts=n,this._dom=t}getDom(){return this._dom}getOptions(){return this._opts}setOption(t){if(!this._model){const n={...t,width:this._opts?.size||512,height:this._opts?.size||512,color:t.color??ft,margin:t.margin??pt,threshold:t.threshold??.5,rangeRatio:t.rangeRatio??.2};this._model=new ct({...n,id:this._dom.id})}this._model.drawWord(),this._model.prepareData()}}const _t={},yt="_rolandBerger_instance_";let gt=+new Date-0;function mt(t,n){if(!t)throw new Error("Initialize failed: invalid dom.");const e=function(t){return _t[function(t,n){return t.getAttribute?t.getAttribute(n):t[n]}(t,yt)]}(t);if(e)return console.warn("There is a chart instance already initialized on the dom."),e;const r="rolandBerger_"+gt++,i=new dt(t,n);return i.id=r,_t[i.id]=i,function(t,n,e){t.setAttribute?t.setAttribute(n,e):t[n]=e}(t,yt,i.id),i}return{init:(t,n)=>mt(t,n)}}();