UNPKG

@upsetjs/venn.js

Version:

Area Proportional Venn and Euler Diagrams

1 lines 22.3 kB
!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,f=0;const u=[];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];f+=(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 f=c-l;f<0&&(f+=2*Math.PI);const u=c-f/2;let x=r(i,{x:s.x+s.radius*Math.sin(u),y:s.y+s.radius*Math.cos(u)});x>2*s.radius&&(x=2*s.radius),(null==a||a.width>x)&&(a={circle:s,width:x,p1:o,p2:n,large:x>s.radius,sweep:!0})}null!=a&&(u.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=f=0:(c=n.radius*n.radius*Math.PI,u.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 f/=2,n&&(n.area=c+f,n.arcArea=c,n.polygonArea=f,n.arcs=u,n.innerPoints=l,n.intersectionPoints=o),c+f}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,f=-(e.y-t.y)*(a/n),u=-(e.x-t.x)*(a/n);return[{x:l+f,y:c-u},{x:l-f,y:c+u}]}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}"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self&&self;var l={exports:{}};function c(t,e={}){e.maxIterations=e.maxIterations||500;const n=e.initialLayout||x,s=e.lossFunction||p,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 c=l.exports.nelderMead(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=c[2*t],o[e].y=c[2*t+1]}return o}!function(t){function e(t){for(var e=new Array(t),n=0;n<t;++n)e[n]=0;return e}function n(t,e){for(var n=0,s=0;s<t.length;++s)n+=t[s]*e[s];return n}function s(t){return Math.sqrt(n(t,t))}function r(t,e,n){for(var s=0;s<e.length;++s)t[s]=e[s]*n}function o(t,e,n,s,r){for(var o=0;o<t.length;++o)t[o]=e*n[o]+s*r[o]}function i(t,e,s,r,i,a,l){var c=s.fx,f=n(s.fxprime,e),u=c,x=c,h=f,p=0;function g(x,p,g){for(var d=0;d<16;++d)if(i=(x+p)/2,o(r.x,1,s.x,i,e),u=r.fx=t(r.x,r.fxprime),h=n(r.fxprime,e),u>c+a*i*f||u>=g)p=i;else{if(Math.abs(h)<=-l*f)return i;h*(p-x)>=0&&(p=x),x=i,g=u}return 0}i=i||1,a=a||1e-6,l=l||.1;for(var d=0;d<10;++d){if(o(r.x,1,s.x,i,e),u=r.fx=t(r.x,r.fxprime),h=n(r.fxprime,e),u>c+a*i*f||d&&u>=x)return g(p,i,x);if(Math.abs(h)<=-l*f)return i;if(h>=0)return g(i,p,u);x=u,p=i,i*=2}return i}t.bisect=function(t,e,n,s){var r=(s=s||{}).maxIterations||100,o=s.tolerance||1e-10,i=t(e),a=t(n),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(var c=0;c<r;++c){var f=e+(l/=2),u=t(f);if(u*i>=0&&(e=f),Math.abs(l)<o||0===u)return f}return e+l},t.nelderMead=function(t,e,n){var s,r=(n=n||{}).maxIterations||200*e.length,i=n.nonZeroDelta||1.05,a=n.zeroDelta||.001,l=n.minErrorDelta||1e-6,c=n.minErrorDelta||1e-5,f=void 0!==n.rho?n.rho:1,u=void 0!==n.chi?n.chi:2,x=void 0!==n.psi?n.psi:-.5,h=void 0!==n.sigma?n.sigma:.5,p=e.length,g=new Array(p+1);g[0]=e,g[0].fx=t(e),g[0].id=0;for(var d=0;d<p;++d){var y=e.slice();y[d]=y[d]?y[d]*i:a,g[d+1]=y,g[d+1].fx=t(y),g[d+1].id=d+1}function m(t){for(var e=0;e<t.length;e++)g[p][e]=t[e];g[p].fx=t.fx}for(var M=function(t,e){return t.fx-e.fx},b=e.slice(),v=e.slice(),z=e.slice(),w=e.slice(),I=0;I<r;++I){if(g.sort(M),n.history){var R=g.map(function(t){var e=t.slice();return e.fx=t.fx,e.id=t.id,e});R.sort(function(t,e){return t.id-e.id}),n.history.push({x:g[0].slice(),fx:g[0].fx,simplex:R})}for(s=0,d=0;d<p;++d)s=Math.max(s,Math.abs(g[0][d]-g[1][d]));if(Math.abs(g[0].fx-g[p].fx)<l&&s<c)break;for(d=0;d<p;++d){b[d]=0;for(var A=0;A<p;++A)b[d]+=g[A][d];b[d]/=p}var F=g[p];if(o(v,1+f,b,-f,F),v.fx=t(v),v.fx<g[0].fx)o(w,1+u,b,-u,F),w.fx=t(w),w.fx<v.fx?m(w):m(v);else if(v.fx>=g[p-1].fx){var P=!1;if(v.fx>F.fx?(o(z,1+x,b,-x,F),z.fx=t(z),z.fx<F.fx?m(z):P=!0):(o(z,1-x*f,b,x*f,F),z.fx=t(z),z.fx<v.fx?m(z):P=!0),P){if(h>=1)break;for(d=1;d<g.length;++d)o(g[d],1-h,g[0],h,g[d]),g[d].fx=t(g[d])}}else m(v)}return g.sort(M),{fx:g[0].fx,x:g[0]}},t.conjugateGradient=function(t,e,a){var l,c,f,u={x:e.slice(),fx:0,fxprime:e.slice()},x={x:e.slice(),fx:0,fxprime:e.slice()},h=e.slice(),p=1;f=(a=a||{}).maxIterations||20*e.length,u.fx=t(u.x,u.fxprime),r(l=u.fxprime.slice(),u.fxprime,-1);for(var g=0;g<f;++g){if(p=i(t,l,u,x,p),a.history&&a.history.push({x:u.x.slice(),fx:u.fx,fxprime:u.fxprime.slice(),alpha:p}),p){o(h,1,x.fxprime,-1,u.fxprime);var d=n(u.fxprime,u.fxprime),y=Math.max(0,n(h,x.fxprime)/d);o(l,y,l,-1,x.fxprime),c=u,u=x,x=c}else r(l,u.fxprime,-1);if(s(u.fxprime)<=1e-5)break}return a.history&&a.history.push({x:u.x.slice(),fx:u.fx,fxprime:u.fxprime.slice(),alpha:p}),u},t.gradientDescent=function(t,e,n){for(var r=(n=n||{}).maxIterations||100*e.length,i=n.learnRate||.001,a={x:e.slice(),fx:0,fxprime:e.slice()},l=0;l<r&&(a.fx=t(a.x,a.fxprime),n.history&&n.history.push({x:a.x.slice(),fx:a.fx,fxprime:a.fxprime.slice()}),o(a.x,1,a.x,-i,a.fxprime),!(s(a.fxprime)<=1e-5));++l);return a},t.gradientDescentLineSearch=function(t,e,n){n=n||{};var o,a={x:e.slice(),fx:0,fxprime:e.slice()},l={x:e.slice(),fx:0,fxprime:e.slice()},c=n.maxIterations||100*e.length,f=n.learnRate||1,u=e.slice(),x=n.c1||.001,h=n.c2||.1,p=[];if(n.history){var g=t;t=function(t,e){return p.push(t.slice()),g(t,e)}}a.fx=t(a.x,a.fxprime);for(var d=0;d<c&&(r(u,a.fxprime,-1),f=i(t,u,a,l,f,x,h),n.history&&(n.history.push({x:a.x.slice(),fx:a.fx,fxprime:a.fxprime.slice(),functionCalls:p,learnRate:f,alpha:f}),p=[]),o=a,a=l,l=o,!(0===f||s(a.fxprime)<1e-5));++d);return a},t.zeros=e,t.zerosM=function(t,n){return e(t).map(function(){return e(n)})},t.norm2=s,t.weightedSum=o,t.scale=r}(l.exports);const f=1e-10;function u(t,e,n){return Math.min(t,e)*Math.min(t,e)*Math.PI<=n+f?Math.abs(t-e):l.exports.bisect(s=>o(t,e,s)-n,0,t+e)}function x(t,e={}){let n=h(t,e);const s=e.lossFunction||p;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=l.exports.zerosM(e.length,e.length),r=l.exports.zerosM(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=u(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=l.exports.norm2(o.map(l.exports.norm2))/o.length;o=o.map(t=>t.map(t=>t/a));const c=(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],f=t[2*l+1],u=n[o][l],x=s[o][l],h=(c-i)*(c-i)+(f-a)*(f-a),p=Math.sqrt(h),g=h-u*u;x>0&&p<=u||x<0&&p>=u||(r+=2*g*g,e[2*o]+=4*g*(i-c),e[2*o+1]+=4*g*(a-f),e[2*l]+=4*g*(c-i),e[2*l+1]+=4*g*(f-a))}}return r})(t,e,o,i);let f=null;for(let t=0;t<n;++t){const t=l.exports.zeros(2*o.length).map(Math.random),n=l.exports.conjugateGradient(c,t,e);(!f||n.fx<f.fx)&&(f=n)}const x=f.x,h={};for(let t=0;t<s.length;++t){const e=s[t];h[e.sets[0]]={x:x[2*t]*a,y:x[2*t+1]*a,radius:Math.sqrt(e.size/Math.PI)}}if(e.history)for(const t of e.history)l.exports.scale(t.x,a);return h}(t,e);s(r,t)+1e-8<s(n,t)&&(n=r)}return n}function h(t,e){const n=e&&e.lossFunction?e.lossFunction:p,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+f>=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 x(t,e){s[e].x=t.x,s[e].y=t.y,l[e]=!0}x({x:0,y:0},o[0].set);for(let e=1;e<o.length;++e){const l=o[e].set,f=r[l].filter(c),p=s[l];if(f.sort(a),0===f.length)throw"ERROR: missing pairwise overlap information";const g=[];for(var h=0;h<f.length;++h){const t=s[f[h].set],e=u(p.radius,t.radius,f[h].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=h+1;n<f.length;++n){const r=s[f[n].set],o=u(p.radius,r.radius,f[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,y=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,y=e)}x(y,l)}return s}function p(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 g(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 d(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 y(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 m(t){const e=e=>{return{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 M(t,e,n){null==e&&(e=Math.PI/2);let s=z(t).map(t=>Object.assign({},t));const r=y(s);for(const t of r){d(t,e,n);const s=m(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);let o=(s=r[0]).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=m(s);return v(s)}function b(t,e,n,s,r){const o=z(t);e-=2*s,n-=2*s;const{xRange:i,yRange:a}=m(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 f=Math.min(c,l),u=(e-(i.max-i.min)*f)/2,x=(n-(a.max-a.min)*f)/2;return v(o.map(t=>({radius:f*t.radius,x:s+u+(t.x-i.min)*f,y:s+x+(t.y-a.min)*f,setid:t.setid})))}function v(t){const e={};for(const n of t)e[n.setid]=n;return e}function z(t){return Object.keys(t).map(e=>Object.assign(t[e],{setid:e}))}function w(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],f=0;s.textContent=null;const u=[];function x(t){const e=s.ownerDocument.createElementNS(s.namespaceURI,"tspan");return e.textContent=t,u.push(e),s.append(e),e}let h=x(l);for(;l=i.pop();){c.push(l);const t=c.join(" ");h.textContent=t,t.length>a&&h.getComputedTextLength()>r&&(c.pop(),h.textContent=c.join(" "),c=[l],h=x(l),f++)}const p=.35-1.1*f/2,g=s.getAttribute("x"),d=s.getAttribute("y");u.forEach((t,e)=>{t.setAttribute("x",g),t.setAttribute("y",d),t.setAttribute("dy",`${p+1.1*e}em`)})}}function I(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 R(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],c=I(o[0],t,e);for(let n=1;n<o.length;++n){const s=I(o[n],t,e);s>=c&&(i=o[n],c=s)}const f=l.exports.nelderMead(n=>-1*I({x:n[0],y:n[1]},t,e),[i.x,i.y],{maxIterations:500,minErrorDelta:1e-10}).x,u={x:s?0:f[0],y:f[1]};let x=!0;for(const e of t)if(r(u,e)>e.radius){x=!1;break}for(const t of e)if(r(u,t)<t.radius){x=!1;break}if(x)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?R(t,[]):a(h.arcs.map(t=>t.p1))}function A(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 F(t,e,n){const s={},r=A(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=[],f=[];for(let e in t)e in a?c.push(t[e]):e in l||f.push(t[e]);const u=R(c,f,n);s[i]=u,u.disjoint&&e[o].size>0&&console.log("WARNING: area "+i+" not represented on screen")}return s}function P(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 j(t){const e=t.split(" ");return{x:Number.parseFloat(e[1]),y:Number.parseFloat(e[2]),radius:-Number.parseFloat(e[4])}}function T(t){if(0===t.length)return[];const e={};return n(t,e),e.arcs}function S(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 P(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 E(t,e){return S(T(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,f=!0,u=!0,x=null,h=null,d=!1,y=null,m=!(!t||!t.symmetricalTextCentre)&&t.symmetricalTextCentre,v={},z=t&&t.colourScheme?t.colourScheme:t&&t.colorScheme?t.colorScheme:["#1f77b4","#ff7f0e","#2ca02c","#d62728","#9467bd","#8c564b","#e377c2","#7f7f7f","#bcbd22","#17becf"],I=0,R=function(t){if(t in v)return v[t];var e=v[t]=z[I];return(I+=1)>=z.length&&(I=0),e},A=c,P=p;function T(c){let p=c.datum();const g=new Set;p.forEach(t=>{0==t.size&&1==t.sets.length&&g.add(t.sets[0])}),p=p.filter(t=>!t.sets.some(t=>g.has(t)));let v={},z={};if(p.length>0){let t=A(p,{lossFunction:P,distinct:d});a&&(t=M(t,i,h)),v=b(t,n,s,r,l),z=F(v,p,m)}const I={};function T(t){return t.sets in I?I[t.sets]:1==t.sets.length?""+t.sets[0]:void 0}p.forEach(t=>{t.label&&(I[t.sets]=t.label)}),c.selectAll("svg").data([v]).enter().append("svg");const S=c.select("svg");e?S.attr("viewBox",`0 0 ${n} ${s}`):S.attr("width",n).attr("height",s);const C={};let O=!1;function N(t){return e=>E(t.sets.map(t=>{let r=C[t],o=v[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}}),y)}S.selectAll(".venn-area path").each(function(t){const e=this.getAttribute("d");1==t.sets.length&&e&&!d&&(O=!0,C[t.sets[0]]=j(e))});const k=S.selectAll(".venn-area").data(p,t=>t.sets),q=k.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("_")),D=q.append("path"),$=q.append("text").attr("class","label").text(t=>T(t)).attr("text-anchor","middle").attr("dy",".35em").attr("x",n/2).attr("y",s/2);function L(t){return"function"==typeof t.transition?t.transition("venn").duration(o):t}u&&(D.style("fill-opacity","0").filter(t=>1==t.sets.length).style("fill",t=>t.colour?t.colour:t.color?t.color:R(t.sets)).style("fill-opacity",".25"),$.style("fill",e=>e.colour||e.color?"#FFF":t.textFill?t.textFill:1==e.sets.length?R(e.sets):"#444"));let _=c;O&&"function"==typeof _.transition?(_=L(c)).selectAll("path").attrTween("d",N):_.selectAll("path").attr("d",t=>E(t.sets.map(t=>v[t])),y);const G=_.selectAll("text").filter(t=>t.sets in z).text(t=>T(t)).attr("x",t=>Math.floor(z[t.sets].x)).attr("y",t=>Math.floor(z[t.sets].y));f&&(O?"on"in G?G.on("end",w(v,T)):G.each("end",w(v,T)):G.each(w(v,T)));const V=L(k.exit()).remove();"function"==typeof k.transition&&V.selectAll("path").attrTween("d",N);const B=V.selectAll("text").attr("x",n/2).attr("y",s/2);return null!==x&&($.style("font-size","0px"),G.style("font-size",x),B.style("font-size","0px")),{circles:v,textCentres:z,nodes:k,enter:q,update:_,exit:V}}return T.wrap=function(t){return arguments.length?(f=t,T):f},T.useViewBox=function(){return e=!0,T},T.width=function(t){return arguments.length?(n=t,T):n},T.height=function(t){return arguments.length?(s=t,T):s},T.padding=function(t){return arguments.length?(r=t,T):r},T.distinct=function(t){return arguments.length?(d=t,T):d},T.colours=function(t){return arguments.length?(R=t,T):R},T.colors=function(t){return arguments.length?(R=t,T):colors},T.fontSize=function(t){return arguments.length?(x=t,T):x},T.round=function(t){return arguments.length?(y=t,T):y},T.duration=function(t){return arguments.length?(o=t,T):o},T.layoutFunction=function(t){return arguments.length?(A=t,T):A},T.normalize=function(t){return arguments.length?(a=t,T):a},T.scaleToFit=function(t){return arguments.length?(l=t,T):l},T.styled=function(t){return arguments.length?(u=t,T):u},T.orientation=function(t){return arguments.length?(i=t,T):i},T.orientationOrder=function(t){return arguments.length?(h=t,T):h},T.lossFunction=function(t){return arguments.length?(P="default"===t?p:"logRatio"===t?g:t,T):P},T},t.bestInitialLayout=x,t.circleArea=s,t.circleCircleIntersection=i,t.circleFromPath=j,t.circleOverlap=o,t.circlePath=P,t.computeTextCentre=R,t.computeTextCentres=F,t.disjointCluster=y,t.distance=r,t.distanceFromIntersectArea=u,t.greedyLayout=h,t.intersectionArea=n,t.intersectionAreaPath=E,t.layout=function(t,e={}){const{lossFunction:n,layoutFunction:s=c,normalize:r=!0,orientation:o=Math.PI/2,orientationOrder:i,width:a=600,height:l=350,padding:f=15,scaleToFit:u=!1,symmetricalTextCentre:x=!1,distinct:h,round:d=2}=e;let y=s(t,{lossFunction:"default"!==n&&n?"logRatio"===n?g:n:p,distinct:h});r&&(y=M(y,o,i));const m=b(y,a,l,f,u),v=F(m,t,x),z=new Map(Object.keys(m).map(t=>[t,{set:t,x:m[t].x,y:m[t].y,radius:m[t].radius}])),w=t.map(t=>{const e=t.sets.map(t=>z.get(t)),n=T(e);return{circles:e,arcs:n,path:S(n,d),area:t,has:new Set(t.sets)}});return w.map(({circles:t,arcs:e,path:n,area:s})=>({data:s,text:v[s.sets],circles:t,arcs:e,path:n,distinctPath:n+function(t){let e="";for(const n of w)n.has.size>t.length&&t.every(t=>n.has.has(t))&&(e+=" "+n.path);return e}(s.sets)}))},t.logRatioLossFunction=g,t.lossFunction=p,t.normalizeSolution=M,t.scaleSolution=b,t.sortAreas=function(t,e){const n=A(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=c,t.wrapText=w,Object.defineProperty(t,"__esModule",{value:!0})});