2 lines
20 KiB
JavaScript
2 lines
20 KiB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).venn={})}(this,(function(t){"use strict";const e=1e-10;function n(t,n){const o=function(t){const e=[];for(let n=0;n<t.length;++n)for(let s=n+1;s<t.length;++s){const r=i(t[n],t[s]);for(const t of r)t.parentIndex=[n,s],e.push(t)}return e}(t),l=o.filter((n=>function(t,n){return n.every((n=>r(t,n)<n.radius+e))}(n,t)));let c=0,u=0;const f=[];if(l.length>1){const e=a(l);for(let t=0;t<l.length;++t){const n=l[t];n.angle=Math.atan2(n.x-e.x,n.y-e.y)}l.sort(((t,e)=>e.angle-t.angle));let n=l[l.length-1];for(let e=0;e<l.length;++e){const o=l[e];u+=(n.x+o.x)*(o.y-n.y);const i={x:(o.x+n.x)/2,y:(o.y+n.y)/2};let a=null;for(let e=0;e<o.parentIndex.length;++e)if(n.parentIndex.includes(o.parentIndex[e])){const s=t[o.parentIndex[e]],l=Math.atan2(o.x-s.x,o.y-s.y),c=Math.atan2(n.x-s.x,n.y-s.y);let u=c-l;u<0&&(u+=2*Math.PI);const f=c-u/2;let h=r(i,{x:s.x+s.radius*Math.sin(f),y:s.y+s.radius*Math.cos(f)});h>2*s.radius&&(h=2*s.radius),(null==a||a.width>h)&&(a={circle:s,width:h,p1:o,p2:n,large:h>s.radius,sweep:!0})}null!=a&&(f.push(a),c+=s(a.circle.radius,a.width),n=o)}}else{let n=t[0];for(let e=1;e<t.length;++e)t[e].radius<n.radius&&(n=t[e]);let s=!1;for(let e=0;e<t.length;++e)if(r(t[e],n)>Math.abs(n.radius-t[e].radius)){s=!0;break}s?c=u=0:(c=n.radius*n.radius*Math.PI,f.push({circle:n,p1:{x:n.x,y:n.y+n.radius},p2:{x:n.x-e,y:n.y+n.radius},width:2*n.radius,large:!0,sweep:!0}))}return u/=2,n&&(n.area=c+u,n.arcArea=c,n.polygonArea=u,n.arcs=f,n.innerPoints=l,n.intersectionPoints=o),c+u}function s(t,e){return t*t*Math.acos(1-e/t)-(t-e)*Math.sqrt(e*(2*t-e))}function r(t,e){return Math.sqrt((t.x-e.x)*(t.x-e.x)+(t.y-e.y)*(t.y-e.y))}function o(t,e,n){if(n>=t+e)return 0;if(n<=Math.abs(t-e))return Math.PI*Math.min(t,e)*Math.min(t,e);const r=e-(n*n-t*t+e*e)/(2*n);return s(t,t-(n*n-e*e+t*t)/(2*n))+s(e,r)}function i(t,e){const n=r(t,e),s=t.radius,o=e.radius;if(n>=s+o||n<=Math.abs(s-o))return[];const i=(s*s-o*o+n*n)/(2*n),a=Math.sqrt(s*s-i*i),l=t.x+i*(e.x-t.x)/n,c=t.y+i*(e.y-t.y)/n,u=-(e.y-t.y)*(a/n),f=-(e.x-t.x)*(a/n);return[{x:l+u,y:c-f},{x:l-u,y:c+f}]}function a(t){const e={x:0,y:0};for(const n of t)e.x+=n.x,e.y+=n.y;return e.x/=t.length,e.y/=t.length,e}function l(t){const e=new Array(t);for(let n=0;n<t;++n)e[n]=0;return e}function c(t,e){return l(t).map((()=>l(e)))}function u(t,e){let n=0;for(let s=0;s<t.length;++s)n+=t[s]*e[s];return n}function f(t){return Math.sqrt(u(t,t))}function h(t,e,n){for(let s=0;s<e.length;++s)t[s]=e[s]*n}function x(t,e,n,s,r){for(let o=0;o<t.length;++o)t[o]=e*n[o]+s*r[o]}function g(t,e,n){const s=(n=n||{}).maxIterations||200*e.length,r=n.nonZeroDelta||1.05,o=n.zeroDelta||.001,i=n.minErrorDelta||1e-6,a=n.minErrorDelta||1e-5,l=void 0!==n.rho?n.rho:1,c=void 0!==n.chi?n.chi:2,u=void 0!==n.psi?n.psi:-.5,f=void 0!==n.sigma?n.sigma:.5;let h;const g=e.length,d=new Array(g+1);d[0]=e,d[0].fx=t(e),d[0].id=0;for(let n=0;n<g;++n){const s=e.slice();s[n]=s[n]?s[n]*r:o,d[n+1]=s,d[n+1].fx=t(s),d[n+1].id=n+1}function p(t){for(let e=0;e<t.length;e++)d[g][e]=t[e];d[g].fx=t.fx}const y=(t,e)=>t.fx-e.fx,m=e.slice(),M=e.slice(),b=e.slice(),z=e.slice();for(let e=0;e<s;++e){if(d.sort(y),n.history){const t=d.map((t=>{const e=t.slice();return e.fx=t.fx,e.id=t.id,e}));t.sort(((t,e)=>t.id-e.id)),n.history.push({x:d[0].slice(),fx:d[0].fx,simplex:t})}h=0;for(let t=0;t<g;++t)h=Math.max(h,Math.abs(d[0][t]-d[1][t]));if(Math.abs(d[0].fx-d[g].fx)<i&&h<a)break;for(let t=0;t<g;++t){m[t]=0;for(let e=0;e<g;++e)m[t]+=d[e][t];m[t]/=g}const e=d[g];if(x(M,1+l,m,-l,e),M.fx=t(M),M.fx<d[0].fx)x(z,1+c,m,-c,e),z.fx=t(z),z.fx<M.fx?p(z):p(M);else if(M.fx>=d[g-1].fx){let n=!1;if(M.fx>e.fx?(x(b,1+u,m,-u,e),b.fx=t(b),b.fx<e.fx?p(b):n=!0):(x(b,1-u*l,m,u*l,e),b.fx=t(b),b.fx<M.fx?p(b):n=!0),n){if(f>=1)break;for(let e=1;e<d.length;++e)x(d[e],1-f,d[0],f,d[e]),d[e].fx=t(d[e])}}else p(M)}return d.sort(y),{fx:d[0].fx,x:d[0]}}function d(t,e,n,s,r,o,i){const a=n.fx,l=u(n.fxprime,e);let c=a,f=a,h=l,g=0;function d(f,g,d){for(let p=0;p<16;++p)if(r=(f+g)/2,x(s.x,1,n.x,r,e),c=s.fx=t(s.x,s.fxprime),h=u(s.fxprime,e),c>a+o*r*l||c>=d)g=r;else{if(Math.abs(h)<=-i*l)return r;h*(g-f)>=0&&(g=f),f=r,d=c}return 0}r=r||1,o=o||1e-6,i=i||.1;for(let p=0;p<10;++p){if(x(s.x,1,n.x,r,e),c=s.fx=t(s.x,s.fxprime),h=u(s.fxprime,e),c>a+o*r*l||p&&c>=f)return d(g,r,f);if(Math.abs(h)<=-i*l)return r;if(h>=0)return d(r,g,c);f=c,g=r,r*=2}return r}function p(t,e,n){let s={x:e.slice(),fx:0,fxprime:e.slice()},r={x:e.slice(),fx:0,fxprime:e.slice()};const o=e.slice();let i,a,l,c=1;l=(n=n||{}).maxIterations||20*e.length,s.fx=t(s.x,s.fxprime),i=s.fxprime.slice(),h(i,s.fxprime,-1);for(let e=0;e<l;++e){if(c=d(t,i,s,r,c),n.history&&n.history.push({x:s.x.slice(),fx:s.fx,fxprime:s.fxprime.slice(),alpha:c}),c){x(o,1,r.fxprime,-1,s.fxprime);const t=u(s.fxprime,s.fxprime);x(i,Math.max(0,u(o,r.fxprime)/t),i,-1,r.fxprime),a=s,s=r,r=a}else h(i,s.fxprime,-1);if(f(s.fxprime)<=1e-5)break}return n.history&&n.history.push({x:s.x.slice(),fx:s.fx,fxprime:s.fxprime.slice(),alpha:c}),s}function y(t,e={}){e.maxIterations=e.maxIterations||500;const n=e.initialLayout||b,s=e.lossFunction||I,r=function(t,e={}){const n=e.distinct,s=t.map((t=>Object.assign({},t)));function r(t){return t.join(";")}if(n){const t=new Map;for(const e of s)for(let n=0;n<e.sets.length;n++){const s=String(e.sets[n]);t.set(s,e.size+(t.get(s)||0));for(let r=n+1;r<e.sets.length;r++){const n=String(e.sets[r]),o=`${s};${n}`,i=`${n};${s}`;t.set(o,e.size+(t.get(o)||0)),t.set(i,e.size+(t.get(i)||0))}}for(const e of s)e.sets.length<3&&(e.size=t.get(r(e.sets)))}const o=[],i=new Set;for(const t of s)if(1===t.sets.length)o.push(t.sets[0]);else if(2===t.sets.length){const e=t.sets[0],n=t.sets[1];i.add(r(t.sets)),i.add(r([n,e]))}o.sort(((t,e)=>t===e?0:t<e?-1:1));for(let t=0;t<o.length;++t){const e=o[t];for(let n=t+1;n<o.length;++n){const t=o[n];i.has(r([e,t]))||s.push({sets:[e,t],size:0})}}return s}(t,e),o=n(r,e),i=Object.keys(o),a=[];for(const t of i)a.push(o[t].x),a.push(o[t].y);const l=g((t=>{const e={};for(let n=0;n<i.length;++n){const s=i[n];e[s]={x:t[2*n],y:t[2*n+1],radius:o[s].radius}}return s(e,r)}),a,e).x;for(let t=0;t<i.length;++t){const e=i[t];o[e].x=l[2*t],o[e].y=l[2*t+1]}return o}const m=1e-10;function M(t,e,n){return Math.min(t,e)*Math.min(t,e)*Math.PI<=n+m?Math.abs(t-e):function(t,e,n,s){const r=(s=s||{}).maxIterations||100,o=s.tolerance||1e-10,i=t(e),a=t(n);let l=n-e;if(i*a>0)throw"Initial bisect points must have opposite signs";if(0===i)return e;if(0===a)return n;for(let n=0;n<r;++n){l/=2;const n=e+l,s=t(n);if(s*i>=0&&(e=n),Math.abs(l)<o||0===s)return n}return e+l}((s=>o(t,e,s)-n),0,t+e)}function b(t,e={}){let n=z(t,e);const s=e.lossFunction||I;if(t.length>=8){const r=function(t,e={}){const n=e.restarts||10,s=[],r={};for(const e of t)1===e.sets.length&&(r[e.sets[0]]=s.length,s.push(e));let{distances:o,constraints:i}=function(t,e,n){const s=c(e.length,e.length),r=c(e.length,e.length);return t.filter((t=>2===t.sets.length)).forEach((t=>{const o=n[t.sets[0]],i=n[t.sets[1]],a=M(Math.sqrt(e[o].size/Math.PI),Math.sqrt(e[i].size/Math.PI),t.size);s[o][i]=s[i][o]=a;let l=0;t.size+1e-10>=Math.min(e[o].size,e[i].size)?l=1:t.size<=1e-10&&(l=-1),r[o][i]=r[i][o]=l})),{distances:s,constraints:r}}(t,s,r);const a=f(o.map(f))/o.length;o=o.map((t=>t.map((t=>t/a))));const u=(t,e)=>function(t,e,n,s){for(let t=0;t<e.length;++t)e[t]=0;let r=0;for(let o=0;o<n.length;++o){const i=t[2*o],a=t[2*o+1];for(let l=o+1;l<n.length;++l){const c=t[2*l],u=t[2*l+1],f=n[o][l],h=s[o][l],x=(c-i)*(c-i)+(u-a)*(u-a),g=Math.sqrt(x),d=x-f*f;h>0&&g<=f||h<0&&g>=f||(r+=2*d*d,e[2*o]+=4*d*(i-c),e[2*o+1]+=4*d*(a-u),e[2*l]+=4*d*(c-i),e[2*l+1]+=4*d*(u-a))}}return r}(t,e,o,i);let x=null;for(let t=0;t<n;++t){const t=p(u,l(2*o.length).map(Math.random),e);(!x||t.fx<x.fx)&&(x=t)}const g=x.x,d={};for(let t=0;t<s.length;++t){const e=s[t];d[e.sets[0]]={x:g[2*t]*a,y:g[2*t+1]*a,radius:Math.sqrt(e.size/Math.PI)}}if(e.history)for(const t of e.history)h(t.x,a);return d}(t,e);s(r,t)+1e-8<s(n,t)&&(n=r)}return n}function z(t,e){const n=e&&e.lossFunction?e.lossFunction:I,s={},r={};for(const e of t)if(1===e.sets.length){const t=e.sets[0];s[t]={x:1e10,y:1e10,rowid:s.length,size:e.size,radius:Math.sqrt(e.size/Math.PI)},r[t]=[]}t=t.filter((t=>2===t.sets.length));for(const e of t){let t=null!=e.weight?e.weight:1;const n=e.sets[0],o=e.sets[1];e.size+m>=Math.min(s[n].size,s[o].size)&&(t=0),r[n].push({set:o,size:e.size,weight:t}),r[o].push({set:n,size:e.size,weight:t})}const o=[];function a(t,e){return e.size-t.size}Object.keys(r).forEach((t=>{let e=0;for(let n=0;n<r[t].length;++n)e+=r[t][n].size*r[t][n].weight;o.push({set:t,size:e})})),o.sort(a);const l={};function c(t){return t.set in l}function u(t,e){s[e].x=t.x,s[e].y=t.y,l[e]=!0}u({x:0,y:0},o[0].set);for(let e=1;e<o.length;++e){const l=o[e].set,h=r[l].filter(c),x=s[l];if(h.sort(a),0===h.length)throw"ERROR: missing pairwise overlap information";const g=[];for(var f=0;f<h.length;++f){const t=s[h[f].set],e=M(x.radius,t.radius,h[f].size);g.push({x:t.x+e,y:t.y}),g.push({x:t.x-e,y:t.y}),g.push({y:t.y+e,x:t.x}),g.push({y:t.y-e,x:t.x});for(let n=f+1;n<h.length;++n){const r=s[h[n].set],o=M(x.radius,r.radius,h[n].size),a=i({x:t.x,y:t.y,radius:e},{x:r.x,y:r.y,radius:o});g.push(...a)}}let d=1e50,p=g[0];for(const e of g){s[l].x=e.x,s[l].y=e.y;const r=n(s,t);r<d&&(d=r,p=e)}u(p,l)}return s}function I(t,e){let s=0;for(const i of e){if(1===i.sets.length)continue;let e;if(2===i.sets.length){const n=t[i.sets[0]],s=t[i.sets[1]];e=o(n.radius,s.radius,r(n,s))}else e=n(i.sets.map((e=>t[e])));s+=(null!=i.weight?i.weight:1)*(e-i.size)*(e-i.size)}return s}function w(t,e){let s=0;for(const i of e){if(1===i.sets.length)continue;let e;if(2===i.sets.length){const n=t[i.sets[0]],s=t[i.sets[1]];e=o(n.radius,s.radius,r(n,s))}else e=n(i.sets.map((e=>t[e])));const a=null!=i.weight?i.weight:1,l=Math.log((e+1)/(i.size+1));s+=a*l*l}return s}function R(t,e,n){if(null==n?t.sort(((t,e)=>e.radius-t.radius)):t.sort(n),t.length>0){const e=t[0].x,n=t[0].y;for(const s of t)s.x-=e,s.y-=n}if(2===t.length){r(t[0],t[1])<Math.abs(t[1].radius-t[0].radius)&&(t[1].x=t[0].x+t[0].radius-t[1].radius-1e-10,t[1].y=t[0].y)}if(t.length>1){const n=Math.atan2(t[1].x,t[1].y)-e,s=Math.cos(n),r=Math.sin(n);for(const e of t){const t=e.x,n=e.y;e.x=s*t-r*n,e.y=r*t+s*n}}if(t.length>2){let n=Math.atan2(t[2].x,t[2].y)-e;for(;n<0;)n+=2*Math.PI;for(;n>2*Math.PI;)n-=2*Math.PI;if(n>Math.PI){const e=t[1].y/(1e-10+t[1].x);for(const n of t){var s=(n.x+e*n.y)/(1+e*e);n.x=2*s-n.x,n.y=2*s*e-n.y}}}}function v(t){function e(t){return t.parent!==t&&(t.parent=e(t.parent)),t.parent}function n(t,n){const s=e(t),r=e(n);s.parent=r}t.forEach((t=>{t.parent=t}));for(let e=0;e<t.length;++e)for(let s=e+1;s<t.length;++s){const o=t[e].radius+t[s].radius;r(t[e],t[s])+1e-10<o&&n(t[s],t[e])}const s=new Map;for(let n=0;n<t.length;++n){const r=e(t[n]).parent.setid;s.has(r)||s.set(r,[]),s.get(r).push(t[n])}return t.forEach((t=>{delete t.parent})),Array.from(s.values())}function A(t){const e=e=>({max:t.reduce(((t,n)=>Math.max(t,n[e]+n.radius)),Number.NEGATIVE_INFINITY),min:t.reduce(((t,n)=>Math.min(t,n[e]-n.radius)),Number.POSITIVE_INFINITY)});return{xRange:e("x"),yRange:e("y")}}function F(t,e,n){null==e&&(e=Math.PI/2);let s=T(t).map((t=>Object.assign({},t)));const r=v(s);for(const t of r){R(t,e,n);const s=A(t);t.size=(s.xRange.max-s.xRange.min)*(s.yRange.max-s.yRange.min),t.bounds=s}r.sort(((t,e)=>e.size-t.size)),s=r[0];let o=s.bounds;const i=(o.xRange.max-o.xRange.min)/50;function a(t,e,n){if(!t)return;const r=t.bounds;let a,l;if(e)a=o.xRange.max-r.xRange.min+i;else{a=o.xRange.max-r.xRange.max;const t=(r.xRange.max-r.xRange.min)/2-(o.xRange.max-o.xRange.min)/2;t<0&&(a+=t)}if(n)l=o.yRange.max-r.yRange.min+i;else{l=o.yRange.max-r.yRange.max;const t=(r.yRange.max-r.yRange.min)/2-(o.yRange.max-o.yRange.min)/2;t<0&&(l+=t)}for(const e of t)e.x+=a,e.y+=l,s.push(e)}let l=1;for(;l<r.length;)a(r[l],!0,!1),a(r[l+1],!1,!0),a(r[l+2],!0,!0),l+=3,o=A(s);return j(s)}function P(t,e,n,s,r){const o=T(t);e-=2*s,n-=2*s;const{xRange:i,yRange:a}=A(o);if(i.max===i.min||a.max===a.min)return console.log("not scaling solution: zero size detected"),t;let l,c;if(r){const t=2*Math.sqrt(r/Math.PI);l=e/t,c=n/t}else l=e/(i.max-i.min),c=n/(a.max-a.min);const u=Math.min(c,l),f=(e-(i.max-i.min)*u)/2,h=(n-(a.max-a.min)*u)/2;return j(o.map((t=>({radius:u*t.radius,x:s+f+(t.x-i.min)*u,y:s+h+(t.y-a.min)*u,setid:t.setid}))))}function j(t){const e={};for(const n of t)e[n.setid]=n;return e}function T(t){return Object.keys(t).map((e=>Object.assign(t[e],{setid:e})))}function E(t,e){return function(n){const s=this,r=t[n.sets[0]].radius||50,o=e(n)||"",i=o.split(/\s+/).reverse(),a=(o.length+i.length)/3;let l=i.pop(),c=[l],u=0;s.textContent=null;const f=[];function h(t){const e=s.ownerDocument.createElementNS(s.namespaceURI,"tspan");return e.textContent=t,f.push(e),s.append(e),e}let x=h(l);for(;l=i.pop(),l;){c.push(l);const t=c.join(" ");x.textContent=t,t.length>a&&x.getComputedTextLength()>r&&(c.pop(),x.textContent=c.join(" "),c=[l],x=h(l),u++)}const g=.35-1.1*u/2,d=s.getAttribute("x"),p=s.getAttribute("y");f.forEach(((t,e)=>{t.setAttribute("x",d),t.setAttribute("y",p),t.setAttribute("dy",`${g+1.1*e}em`)}))}}function S(t,e,n){let s=e[0].radius-r(e[0],t);for(let n=1;n<e.length;++n){const o=e[n].radius-r(e[n],t);o<=s&&(s=o)}for(let e=0;e<n.length;++e){const o=r(n[e],t)-n[e].radius;o<=s&&(s=o)}return s}function C(t,e,s){const o=[];for(const e of t)o.push({x:e.x,y:e.y}),o.push({x:e.x+e.radius/2,y:e.y}),o.push({x:e.x-e.radius/2,y:e.y}),o.push({x:e.x,y:e.y+e.radius/2}),o.push({x:e.x,y:e.y-e.radius/2});let i=o[0],l=S(o[0],t,e);for(let n=1;n<o.length;++n){const s=S(o[n],t,e);s>=l&&(i=o[n],l=s)}const c=g((n=>-1*S({x:n[0],y:n[1]},t,e)),[i.x,i.y],{maxIterations:500,minErrorDelta:1e-10}).x,u={x:s?0:c[0],y:c[1]};let f=!0;for(const e of t)if(r(u,e)>e.radius){f=!1;break}for(const t of e)if(r(u,t)<t.radius){f=!1;break}if(f)return u;if(1==t.length)return{x:t[0].x,y:t[0].y};const h={};return n(t,h),0===h.arcs.length?{x:0,y:-1e3,disjoint:!0}:1==h.arcs.length?{x:h.arcs[0].circle.x,y:h.arcs[0].circle.y}:e.length?C(t,[]):a(h.arcs.map((t=>t.p1)))}function N(t){const e={},n=Object.keys(t);for(const t of n)e[t]=[];for(let s=0;s<n.length;s++){const o=n[s],i=t[o];for(let a=s+1;a<n.length;++a){const s=n[a],l=t[s],c=r(i,l);c+l.radius<=i.radius+1e-10?e[s].push(o):c+i.radius<=l.radius+1e-10&&e[o].push(s)}}return e}function O(t,e,n){const s={},r=N(t);for(let o=0;o<e.length;++o){const i=e[o].sets,a={},l={};for(let t=0;t<i.length;++t){a[i[t]]=!0;const e=r[i[t]];for(let t=0;t<e.length;++t)l[e[t]]=!0}const c=[],u=[];for(let e in t)e in a?c.push(t[e]):e in l||u.push(t[e]);const f=C(c,u,n);s[i]=f,f.disjoint&&e[o].size>0&&console.log("WARNING: area "+i+" not represented on screen")}return s}function k(t,e,n){const s=[];return s.push("\nM",t,e),s.push("\nm",-n,0),s.push("\na",n,n,0,1,0,2*n,0),s.push("\na",n,n,0,1,0,2*-n,0),s.join(" ")}function q(t){const e=t.split(" ");return{x:Number.parseFloat(e[1]),y:Number.parseFloat(e[2]),radius:-Number.parseFloat(e[4])}}function $(t){if(0===t.length)return[];const e={};return n(t,e),e.arcs}function D(t,e){if(0===t.length)return"M 0 0";const n=Math.pow(10,e||0),s=null!=e?t=>Math.round(t*n)/n:t=>t;if(1==t.length){const e=t[0].circle;return k(s(e.x),s(e.y),s(e.radius))}const r=["\nM",s(t[0].p2.x),s(t[0].p2.y)];for(const e of t){const t=s(e.circle.radius);r.push("\nA",t,t,0,e.large?1:0,e.sweep?1:0,s(e.p1.x),s(e.p1.y))}return r.join(" ")}function L(t,e){return D($(t),e)}t.VennDiagram=function(t={}){let e=!1,n=600,s=350,r=15,o=1e3,i=Math.PI/2,a=!0,l=null,c=!0,u=!0,f=null,h=null,x=!1,g=null,d=!(!t||!t.symmetricalTextCentre)&&t.symmetricalTextCentre,p={},m=t&&t.colourScheme?t.colourScheme:t&&t.colorScheme?t.colorScheme:["#1f77b4","#ff7f0e","#2ca02c","#d62728","#9467bd","#8c564b","#e377c2","#7f7f7f","#bcbd22","#17becf"],M=0,b=function(t){if(t in p)return p[t];var e=p[t]=m[M];return M+=1,M>=m.length&&(M=0),e},z=y,R=I;function v(p){let y=p.datum();const m=new Set;y.forEach((t=>{0==t.size&&1==t.sets.length&&m.add(t.sets[0])})),y=y.filter((t=>!t.sets.some((t=>m.has(t)))));let M={},I={};if(y.length>0){let t=z(y,{lossFunction:R,distinct:x});a&&(t=F(t,i,h)),M=P(t,n,s,r,l),I=O(M,y,d)}const w={};function v(t){return t.sets in w?w[t.sets]:1==t.sets.length?""+t.sets[0]:void 0}y.forEach((t=>{t.label&&(w[t.sets]=t.label)})),p.selectAll("svg").data([M]).enter().append("svg");const A=p.select("svg");e?A.attr("viewBox",`0 0 ${n} ${s}`):A.attr("width",n).attr("height",s);const j={};let T=!1;function S(t){return e=>L(t.sets.map((t=>{let r=j[t],o=M[t];return r||(r={x:n/2,y:s/2,radius:1}),o||(o={x:n/2,y:s/2,radius:1}),{x:r.x*(1-e)+o.x*e,y:r.y*(1-e)+o.y*e,radius:r.radius*(1-e)+o.radius*e}})),g)}A.selectAll(".venn-area path").each((function(t){const e=this.getAttribute("d");1==t.sets.length&&e&&!x&&(T=!0,j[t.sets[0]]=q(e))}));const C=A.selectAll(".venn-area").data(y,(t=>t.sets)),N=C.enter().append("g").attr("class",(t=>`venn-area venn-${1==t.sets.length?"circle":"intersection"}${t.colour||t.color?" venn-coloured":""}`)).attr("data-venn-sets",(t=>t.sets.join("_"))),k=N.append("path"),$=N.append("text").attr("class","label").text((t=>v(t))).attr("text-anchor","middle").attr("dy",".35em").attr("x",n/2).attr("y",s/2);function D(t){return"function"==typeof t.transition?t.transition("venn").duration(o):t}u&&(k.style("fill-opacity","0").filter((t=>1==t.sets.length)).style("fill",(t=>t.colour?t.colour:t.color?t.color:b(t.sets))).style("fill-opacity",".25"),$.style("fill",(e=>e.colour||e.color?"#FFF":t.textFill?t.textFill:1==e.sets.length?b(e.sets):"#444")));let V=p;T&&"function"==typeof V.transition?(V=D(p),V.selectAll("path").attrTween("d",S)):V.selectAll("path").attr("d",(t=>L(t.sets.map((t=>M[t])))),g);const _=V.selectAll("text").filter((t=>t.sets in I)).text((t=>v(t))).attr("x",(t=>Math.floor(I[t.sets].x))).attr("y",(t=>Math.floor(I[t.sets].y)));c&&(T?"on"in _?_.on("end",E(M,v)):_.each("end",E(M,v)):_.each(E(M,v)));const B=D(C.exit()).remove();"function"==typeof C.transition&&B.selectAll("path").attrTween("d",S);const G=B.selectAll("text").attr("x",n/2).attr("y",s/2);return null!==f&&($.style("font-size","0px"),_.style("font-size",f),G.style("font-size","0px")),{circles:M,textCentres:I,nodes:C,enter:N,update:V,exit:B}}return v.wrap=function(t){return arguments.length?(c=t,v):c},v.useViewBox=function(){return e=!0,v},v.width=function(t){return arguments.length?(n=t,v):n},v.height=function(t){return arguments.length?(s=t,v):s},v.padding=function(t){return arguments.length?(r=t,v):r},v.distinct=function(t){return arguments.length?(x=t,v):x},v.colours=function(t){return arguments.length?(b=t,v):b},v.colors=function(t){return arguments.length?(b=t,v):b},v.fontSize=function(t){return arguments.length?(f=t,v):f},v.round=function(t){return arguments.length?(g=t,v):g},v.duration=function(t){return arguments.length?(o=t,v):o},v.layoutFunction=function(t){return arguments.length?(z=t,v):z},v.normalize=function(t){return arguments.length?(a=t,v):a},v.scaleToFit=function(t){return arguments.length?(l=t,v):l},v.styled=function(t){return arguments.length?(u=t,v):u},v.orientation=function(t){return arguments.length?(i=t,v):i},v.orientationOrder=function(t){return arguments.length?(h=t,v):h},v.lossFunction=function(t){return arguments.length?(R="default"===t?I:"logRatio"===t?w:t,v):R},v},t.bestInitialLayout=b,t.circleArea=s,t.circleCircleIntersection=i,t.circleFromPath=q,t.circleOverlap=o,t.circlePath=k,t.computeTextCentre=C,t.computeTextCentres=O,t.disjointCluster=v,t.distance=r,t.distanceFromIntersectArea=M,t.greedyLayout=z,t.intersectionArea=n,t.intersectionAreaPath=L,t.layout=function(t,e={}){const{lossFunction:n,layoutFunction:s=y,normalize:r=!0,orientation:o=Math.PI/2,orientationOrder:i,width:a=600,height:l=350,padding:c=15,scaleToFit:u=!1,symmetricalTextCentre:f=!1,distinct:h,round:x=2}=e;let g=s(t,{lossFunction:"default"!==n&&n?"logRatio"===n?w:n:I,distinct:h});r&&(g=F(g,o,i));const d=P(g,a,l,c,u),p=O(d,t,f),m=new Map(Object.keys(d).map((t=>[t,{set:t,x:d[t].x,y:d[t].y,radius:d[t].radius}]))),M=t.map((t=>{const e=t.sets.map((t=>m.get(t))),n=$(e);return{circles:e,arcs:n,path:D(n,x),area:t,has:new Set(t.sets)}}));function b(t){let e="";for(const n of M)n.has.size>t.length&&t.every((t=>n.has.has(t)))&&(e+=" "+n.path);return e}return M.map((({circles:t,arcs:e,path:n,area:s})=>({data:s,text:p[s.sets],circles:t,arcs:e,path:n,distinctPath:n+b(s.sets)})))},t.logRatioLossFunction=w,t.lossFunction=I,t.normalizeSolution=F,t.scaleSolution=P,t.sortAreas=function(t,e){const n=N(t.selectAll("svg").datum()),s=new Set;for(const t of e.sets)for(let e in n){const r=n[e];for(let n=0;n<r.length;++n)if(r[n]==t){s.add(e);break}}function r(t){return t.every((t=>!s.has(t)))}t.selectAll("g").sort(((t,n)=>t.sets.length!=n.sets.length?t.sets.length-n.sets.length:t==e?r(n.sets)?-1:1:n==e?r(t.sets)?1:-1:n.size-t.size))},t.venn=y,t.wrapText=E}));
|