Skip to content

Instantly share code, notes, and snippets.

@aeshirey aeshirey/venn.min.js
Created Sep 13, 2018

Embed
What would you like to do?
A minified, publicly available version of https://github.com/benfred/venn.js
!function(t,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("d3-selection"),require("d3-transition")):"function"==typeof define&&define.amd?define(["exports","d3-selection","d3-transition"],r):r(t.venn={},t.d3,t.d3)}(this,function(t,C,r){"use strict";var w=1e-10;function x(r,t){var n,e=function(t){for(var r=[],n=0;n<t.length;++n)for(var e=n+1;e<t.length;++e)for(var a=k(t[n],t[e]),i=0;i<a.length;++i){var s=a[i];s.parentIndex=[n,e],r.push(s)}return r}(r),a=e.filter(function(t){return function(t,r){for(var n=0;n<r.length;++n)if(R(t,r[n])>r[n].radius+w)return!1;return!0}(t,r)}),i=0,s=0,u=[];if(1<a.length){var o=P(a);for(n=0;n<a.length;++n){var f=a[n];f.angle=Math.atan2(f.x-o.x,f.y-o.y)}a.sort(function(t,r){return r.angle-t.angle});var l=a[a.length-1];for(n=0;n<a.length;++n){var h=a[n];s+=(l.x+h.x)*(h.y-l.y);for(var x={x:(h.x+l.x)/2,y:(h.y+l.y)/2},c=null,g=0;g<h.parentIndex.length;++g)if(-1<l.parentIndex.indexOf(h.parentIndex[g])){var v=r[h.parentIndex[g]],p=Math.atan2(h.x-v.x,h.y-v.y),d=Math.atan2(l.x-v.x,l.y-v.y),y=d-p;y<0&&(y+=2*Math.PI);var m=d-y/2,M=R(x,{x:v.x+v.radius*Math.sin(m),y:v.y+v.radius*Math.cos(m)});M>2*v.radius&&(M=2*v.radius),(null===c||c.width>M)&&(c={circle:v,width:M,p1:h,p2:l})}null!==c&&(u.push(c),i+=I(c.circle.radius,c.width),l=h)}}else{var z=r[0];for(n=1;n<r.length;++n)r[n].radius<z.radius&&(z=r[n]);var b=!1;for(n=0;n<r.length;++n)if(R(r[n],z)>Math.abs(z.radius-r[n].radius)){b=!0;break}b?i=s=0:(i=z.radius*z.radius*Math.PI,u.push({circle:z,p1:{x:z.x,y:z.y+z.radius},p2:{x:z.x-w,y:z.y+z.radius},width:2*z.radius}))}return s/=2,t&&(t.area=i+s,t.arcArea=i,t.polygonArea=s,t.arcs=u,t.innerPoints=a,t.intersectionPoints=e),i+s}function I(t,r){return t*t*Math.acos(1-r/t)-(t-r)*Math.sqrt(r*(2*t-r))}function R(t,r){return Math.sqrt((t.x-r.x)*(t.x-r.x)+(t.y-r.y)*(t.y-r.y))}function o(t,r,n){if(t+r<=n)return 0;if(n<=Math.abs(t-r))return Math.PI*Math.min(t,r)*Math.min(t,r);var e=r-(n*n-t*t+r*r)/(2*n);return I(t,t-(n*n-r*r+t*t)/(2*n))+I(r,e)}function k(t,r){var n=R(t,r),e=t.radius,a=r.radius;if(e+a<=n||n<=Math.abs(e-a))return[];var i=(e*e-a*a+n*n)/(2*n),s=Math.sqrt(e*e-i*i),u=t.x+i*(r.x-t.x)/n,o=t.y+i*(r.y-t.y)/n,f=-(r.y-t.y)*(s/n),l=-(r.x-t.x)*(s/n);return[{x:u+f,y:o-l},{x:u-f,y:o+l}]}function P(t){for(var r={x:0,y:0},n=0;n<t.length;++n)r.x+=t[n].x,r.y+=t[n].y;return r.x/=t.length,r.y/=t.length,r}function A(t){for(var r=new Array(t),n=0;n<t;++n)r[n]=0;return r}function F(t,r){return A(t).map(function(){return A(r)})}function v(t,r){for(var n=0,e=0;e<t.length;++e)n+=t[e]*r[e];return n}function O(t){return Math.sqrt(v(t,t))}function q(t,r,n){for(var e=0;e<r.length;++e)t[e]=r[e]*n}function j(t,r,n,e,a){for(var i=0;i<t.length;++i)t[i]=r*n[i]+e*a[i]}function c(t,r,n){var e,a=(n=n||{}).maxIterations||200*r.length,i=n.nonZeroDelta||1.05,s=n.zeroDelta||.001,u=n.minErrorDelta||1e-6,o=n.minErrorDelta||1e-5,f=void 0!==n.rho?n.rho:1,l=void 0!==n.chi?n.chi:2,h=void 0!==n.psi?n.psi:-.5,x=void 0!==n.sigma?n.sigma:.5,c=r.length,g=new Array(c+1);g[0]=r,g[0].fx=t(r);for(var v=g[0].id=0;v<c;++v){var p=r.slice();p[v]=p[v]?p[v]*i:s,g[v+1]=p,g[v+1].fx=t(p),g[v+1].id=v+1}function d(t){for(var r=0;r<t.length;r++)g[c][r]=t[r];g[c].fx=t.fx}for(var y=function(t,r){return t.fx-r.fx},m=r.slice(),M=r.slice(),z=r.slice(),b=r.slice(),w=0;w<a;++w){if(g.sort(y),n.history){var I=g.map(function(t){var r=t.slice();return r.fx=t.fx,r.id=t.id,r});I.sort(function(t,r){return t.id-r.id}),n.history.push({x:g[0].slice(),fx:g[0].fx,simplex:I})}for(v=e=0;v<c;++v)e=Math.max(e,Math.abs(g[0][v]-g[1][v]));if(Math.abs(g[0].fx-g[c].fx)<u&&e<o)break;for(v=0;v<c;++v){for(var R=m[v]=0;R<c;++R)m[v]+=g[R][v];m[v]/=c}var P=g[c];if(j(M,1+f,m,-f,P),M.fx=t(M),M.fx<g[0].fx)j(b,1+l,m,-l,P),b.fx=t(b),b.fx<M.fx?d(b):d(M);else if(M.fx>=g[c-1].fx){var A=!1;if(M.fx>P.fx?(j(z,1+h,m,-h,P),z.fx=t(z),z.fx<P.fx?d(z):A=!0):(j(z,1-h*f,m,h*f,P),z.fx=t(z),z.fx<M.fx?d(z):A=!0),A){if(1<=x)break;for(v=1;v<g.length;++v)j(g[v],1-x,g[0],x,g[v]),g[v].fx=t(g[v])}}else d(M)}return g.sort(y),{fx:g[0].fx,x:g[0]}}function g(a,i,s,u,o,f,l){var h=s.fx,x=v(s.fxprime,i),c=h,t=h,g=x,r=0;function n(t,r,n){for(var e=0;e<16;++e)if(o=(t+r)/2,j(u.x,1,s.x,o,i),c=u.fx=a(u.x,u.fxprime),g=v(u.fxprime,i),h+f*o*x<c||n<=c)r=o;else{if(Math.abs(g)<=-l*x)return o;0<=g*(r-t)&&(r=t),t=o,n=c}return 0}o=o||1,f=f||1e-6,l=l||.1;for(var e=0;e<10;++e){if(j(u.x,1,s.x,o,i),c=u.fx=a(u.x,u.fxprime),g=v(u.fxprime,i),h+f*o*x<c||e&&t<=c)return n(r,o,t);if(Math.abs(g)<=-l*x)return o;if(0<=g)return n(o,r,c);t=c,r=o,o*=2}return o}function D(t,r,n){var e,a,i,s={x:r.slice(),fx:0,fxprime:r.slice()},u={x:r.slice(),fx:0,fxprime:r.slice()},o=r.slice(),f=1;i=(n=n||{}).maxIterations||20*r.length,s.fx=t(s.x,s.fxprime),q(e=s.fxprime.slice(),s.fxprime,-1);for(var l=0;l<i;++l){if(f=g(t,e,s,u,f),n.history&&n.history.push({x:s.x.slice(),fx:s.fx,fxprime:s.fxprime.slice(),alpha:f}),f){j(o,1,u.fxprime,-1,s.fxprime);var h=v(s.fxprime,s.fxprime);j(e,Math.max(0,v(o,u.fxprime)/h),e,-1,u.fxprime),a=s,s=u,u=a}else q(e,s.fxprime,-1);if(O(s.fxprime)<=1e-5)break}return n.history&&n.history.push({x:s.x.slice(),fx:s.fx,fxprime:s.fxprime.slice(),alpha:f}),s}function i(a,t){(t=t||{}).maxIterations=t.maxIterations||500;var r,n=t.initialLayout||l,i=t.lossFunction||L,s=n(a=function(t){t=t.slice();var r,n,e,a,i=[],s={};for(r=0;r<t.length;++r){var u=t[r];1==u.sets.length?i.push(u.sets[0]):2==u.sets.length&&(e=u.sets[0],a=u.sets[1],s[[e,a]]=!0,s[[a,e]]=!0)}for(i.sort(function(t,r){return r<t}),r=0;r<i.length;++r)for(e=i[r],n=r+1;n<i.length;++n)a=i[n],[e,a]in s||t.push({sets:[e,a],size:0});return t}(a),t),e=[],u=[];for(r in s)s.hasOwnProperty(r)&&(e.push(s[r].x),e.push(s[r].y),u.push(r));for(var o=c(function(t){for(var r={},n=0;n<u.length;++n){var e=u[n];r[e]={x:t[2*n],y:t[2*n+1],radius:s[e].radius}}return i(r,a)},e,t).x,f=0;f<u.length;++f)r=u[f],s[r].x=o[2*f],s[r].y=o[2*f+1];return s}var E=1e-10;function T(r,n,e){return Math.min(r,n)*Math.min(r,n)*Math.PI<=e+E?Math.abs(r-n):function(t,r,n,e){var a=(e=e||{}).maxIterations||100,i=e.tolerance||1e-10,s=t(r),u=t(n),o=n-r;if(0<s*u)throw"Initial bisect points must have opposite signs";if(0===s)return r;if(0===u)return n;for(var f=0;f<a;++f){var l=r+(o/=2),h=t(l);if(0<=h*s&&(r=l),Math.abs(o)<i||0===h)return l}return r+o}(function(t){return o(r,n,t)-e},0,r+n)}function l(t,r){var n=s(t,r),e=r.lossFunction||L;if(8<=t.length){var a=function(t,r){var n,e=(r=r||{}).restarts||10,a=[],i={};for(n=0;n<t.length;++n){var s=t[n];1==s.sets.length&&(i[s.sets[0]]=a.length,a.push(s))}var u=(h=t,x=a,c=i,g=F(x.length,x.length),v=F(x.length,x.length),h.filter(function(t){return 2==t.sets.length}).map(function(t){var r=c[t.sets[0]],n=c[t.sets[1]],e=T(Math.sqrt(x[r].size/Math.PI),Math.sqrt(x[n].size/Math.PI),t.size);g[r][n]=g[n][r]=e;var a=0;t.size+1e-10>=Math.min(x[r].size,x[n].size)?a=1:t.size<=1e-10&&(a=-1),v[r][n]=v[n][r]=a}),{distances:g,constraints:v}),o=u.distances,f=u.constraints,l=O(o.map(O))/o.length;var h,x,c,g,v;o=o.map(function(t){return t.map(function(t){return t/l})});var p,d,y=function(t,r){return function(t,r,n,e){var a,i=0;for(a=0;a<r.length;++a)r[a]=0;for(a=0;a<n.length;++a)for(var s=t[2*a],u=t[2*a+1],o=a+1;o<n.length;++o){var f=t[2*o],l=t[2*o+1],h=n[a][o],x=e[a][o],c=(f-s)*(f-s)+(l-u)*(l-u),g=Math.sqrt(c),v=c-h*h;0<x&&g<=h||x<0&&h<=g||(i+=2*v*v,r[2*a]+=4*v*(s-f),r[2*a+1]+=4*v*(u-l),r[2*o]+=4*v*(f-s),r[2*o+1]+=4*v*(l-u))}return i}(t,r,o,f)};for(n=0;n<e;++n){var m=A(2*o.length).map(Math.random);d=D(y,m,r),(!p||d.fx<p.fx)&&(p=d)}var M=p.x,z={};for(n=0;n<a.length;++n){var b=a[n];z[b.sets[0]]={x:M[2*n]*l,y:M[2*n+1]*l,radius:Math.sqrt(b.size/Math.PI)}}if(r.history)for(n=0;n<r.history.length;++n)q(r.history[n].x,l);return z}(t,r);e(a,t)+1e-8<e(n,t)&&(n=a)}return n}function s(t,r){for(var n,e=r&&r.lossFunction?r.lossFunction:L,a={},i={},s=0;s<t.length;++s){var u=t[s];1==u.sets.length&&(n=u.sets[0],a[n]={x:1e10,y:1e10,rowid:a.length,size:u.size,radius:Math.sqrt(u.size/Math.PI)},i[n]=[])}for(t=t.filter(function(t){return 2==t.sets.length}),s=0;s<t.length;++s){var o=t[s],f=o.hasOwnProperty("weight")?o.weight:1,l=o.sets[0],h=o.sets[1];o.size+E>=Math.min(a[l].size,a[h].size)&&(f=0),i[l].push({set:h,size:o.size,weight:f}),i[h].push({set:l,size:o.size,weight:f})}var x=[];for(n in i)if(i.hasOwnProperty(n)){var c=0;for(s=0;s<i[n].length;++s)c+=i[n][s].size*i[n][s].weight;x.push({set:n,size:c})}function g(t,r){return r.size-t.size}x.sort(g);var v={};function p(t){return t.set in v}function d(t,r){a[r].x=t.x,a[r].y=t.y,v[r]=!0}for(d({x:0,y:0},x[0].set),s=1;s<x.length;++s){var y=x[s].set,m=i[y].filter(p);if(n=a[y],m.sort(g),0===m.length)throw"ERROR: missing pairwise overlap information";for(var M=[],z=0;z<m.length;++z){var b=a[m[z].set],w=T(n.radius,b.radius,m[z].size);M.push({x:b.x+w,y:b.y}),M.push({x:b.x-w,y:b.y}),M.push({y:b.y+w,x:b.x}),M.push({y:b.y-w,x:b.x});for(var I=z+1;I<m.length;++I)for(var R=a[m[I].set],P=T(n.radius,R.radius,m[I].size),A=k({x:b.x,y:b.y,radius:w},{x:R.x,y:R.y,radius:P}),F=0;F<A.length;++F)M.push(A[F])}var O=1e50,q=M[0];for(z=0;z<M.length;++z){a[y].x=M[z].x,a[y].y=M[z].y;var j=e(a,t);j<O&&(O=j,q=M[z])}d(q,y)}return a}function L(r,t){var n=0;for(var e=0;e<t.length;++e){var a,i=t[e];if(1!=i.sets.length){if(2==i.sets.length){var s=r[i.sets[0]],u=r[i.sets[1]];a=o(s.radius,u.radius,R(s,u))}else a=x(i.sets.map(function(t){return r[t]}));n+=(i.hasOwnProperty("weight")?i.weight:1)*(a-i.size)*(a-i.size)}}return n}function p(t,r,n){var e;if(null===n?t.sort(function(t,r){return r.radius-t.radius}):t.sort(n),0<t.length){var a=t[0].x,i=t[0].y;for(e=0;e<t.length;++e)t[e].x-=a,t[e].y-=i}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(1<t.length){var s,u,o=Math.atan2(t[1].x,t[1].y)-r,f=Math.cos(o),l=Math.sin(o);for(e=0;e<t.length;++e)s=t[e].x,u=t[e].y,t[e].x=f*s-l*u,t[e].y=l*s+f*u}if(2<t.length){for(var h=Math.atan2(t[2].x,t[2].y)-r;h<0;)h+=2*Math.PI;for(;h>2*Math.PI;)h-=2*Math.PI;if(h>Math.PI){var x=t[1].y/(1e-10+t[1].x);for(e=0;e<t.length;++e){var c=(t[e].x+x*t[e].y)/(1+x*x);t[e].x=2*c-t[e].x,t[e].y=2*c*x-t[e].y}}}}function d(t){function r(t){return t.parent!==t&&(t.parent=r(t.parent)),t.parent}t.map(function(t){t.parent=t});for(var n=0;n<t.length;++n)for(var e=n+1;e<t.length;++e){var a=t[n].radius+t[e].radius;R(t[n],t[e])+1e-10<a&&(i=t[e],s=t[n],u=void 0,u=r(i),o=r(s),u.parent=o)}var i,s,u,o,f,l={};for(n=0;n<t.length;++n)(f=r(t[n]).parent.setid)in l||(l[f]=[]),l[f].push(t[n]);t.map(function(t){delete t.parent});var h=[];for(f in l)l.hasOwnProperty(f)&&h.push(l[f]);return h}function y(t){var r=function(r){return{max:Math.max.apply(null,t.map(function(t){return t[r]+t.radius})),min:Math.min.apply(null,t.map(function(t){return t[r]-t.radius}))}};return{xRange:r("x"),yRange:r("y")}}function S(t,r,n){null===r&&(r=Math.PI/2);var e,a,o=[];for(a in t)if(t.hasOwnProperty(a)){var i=t[a];o.push({x:i.x,y:i.y,radius:i.radius,setid:a})}var s=d(o);for(e=0;e<s.length;++e){p(s[e],r,n);var u=y(s[e]);s[e].size=(u.xRange.max-u.xRange.min)*(u.yRange.max-u.yRange.min),s[e].bounds=u}s.sort(function(t,r){return r.size-t.size});var f=(o=s[0]).bounds,l=(f.xRange.max-f.xRange.min)/50;function h(t,r,n){if(t){var e,a,i,s=t.bounds;r?e=f.xRange.max-s.xRange.min+l:(e=f.xRange.max-s.xRange.max,(i=(s.xRange.max-s.xRange.min)/2-(f.xRange.max-f.xRange.min)/2)<0&&(e+=i)),n?a=f.yRange.max-s.yRange.min+l:(a=f.yRange.max-s.yRange.max,(i=(s.yRange.max-s.yRange.min)/2-(f.yRange.max-f.yRange.min)/2)<0&&(a+=i));for(var u=0;u<t.length;++u)t[u].x+=e,t[u].y+=a,o.push(t[u])}}for(var x=1;x<s.length;)h(s[x],!0,!1),h(s[x+1],!1,!0),h(s[x+2],!0,!0),x+=3,f=y(o);var c={};for(e=0;e<o.length;++e)c[o[e].setid]=o[e];return c}function _(t,r,n,e){var a=[],i=[];for(var s in t)t.hasOwnProperty(s)&&(i.push(s),a.push(t[s]));r-=2*e,n-=2*e;var u=y(a),o=u.xRange,f=u.yRange;if(o.max==o.min||f.max==f.min)return console.log("not scaling solution: zero size detected"),t;for(var l=r/(o.max-o.min),h=n/(f.max-f.min),x=Math.min(h,l),c=(r-(o.max-o.min)*x)/2,g=(n-(f.max-f.min)*x)/2,v={},p=0;p<a.length;++p){var d=a[p];v[i[p]]={radius:x*d.radius,x:e+c+(d.x-o.min)*x,y:e+g+(d.y-f.min)*x}}return v}function N(g,v){return function(){for(var t,r=C.select(this),n=r.datum(),e=g[n.sets[0]].radius||50,a=v(n)||"",i=a.split(/\s+/).reverse(),s=(a.length+i.length)/3,u=i.pop(),o=[u],f=0,l=r.text(null).append("tspan").text(u);u=i.pop();)o.push(u),t=o.join(" "),l.text(t),t.length>s&&l.node().getComputedTextLength()>e&&(o.pop(),l.text(o.join(" ")),o=[u],l=r.append("tspan").text(u),f++);var h=.35-1.1*f/2,x=r.attr("x"),c=r.attr("y");r.selectAll("tspan").attr("x",x).attr("y",c).attr("dy",function(t,r){return h+1.1*r+"em"})}}function m(t,r,n){var e,a,i=r[0].radius-R(r[0],t);for(e=1;e<r.length;++e)(a=r[e].radius-R(r[e],t))<=i&&(i=a);for(e=0;e<n.length;++e)(a=R(n[e],t)-n[e].radius)<=i&&(i=a);return i}function M(r,n){var t,e=[];for(t=0;t<r.length;++t){var a=r[t];e.push({x:a.x,y:a.y}),e.push({x:a.x+a.radius/2,y:a.y}),e.push({x:a.x-a.radius/2,y:a.y}),e.push({x:a.x,y:a.y+a.radius/2}),e.push({x:a.x,y:a.y-a.radius/2})}var i=e[0],s=m(e[0],r,n);for(t=1;t<e.length;++t){var u=m(e[t],r,n);s<=u&&(i=e[t],s=u)}var o=c(function(t){return-1*m({x:t[0],y:t[1]},r,n)},[i.x,i.y],{maxIterations:500,minErrorDelta:1e-10}).x,f={x:o[0],y:o[1]},l=!0;for(t=0;t<r.length;++t)if(R(f,r[t])>r[t].radius){l=!1;break}for(t=0;t<n.length;++t)if(R(f,n[t])<n[t].radius){l=!1;break}if(!l)if(1==r.length)f={x:r[0].x,y:r[0].y};else{var h={};x(r,h),f=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}:n.length?M(r,[]):P(h.arcs.map(function(t){return t.p1}))}return f}function z(t){var r={},n=[];for(var e in t)n.push(e),r[e]=[];for(var a=0;a<n.length;a++)for(var i=t[n[a]],s=a+1;s<n.length;++s){var u=t[n[s]],o=R(i,u);o+u.radius<=i.radius+1e-10?r[n[s]].push(n[a]):o+i.radius<=u.radius+1e-10&&r[n[a]].push(n[s])}return r}function G(t,r){for(var n={},e=z(t),a=0;a<r.length;++a){for(var i=r[a].sets,s={},u={},o=0;o<i.length;++o){s[i[o]]=!0;for(var f=e[i[o]],l=0;l<f.length;++l)u[f[l]]=!0}var h=[],x=[];for(var c in t)c in s?h.push(t[c]):c in u||x.push(t[c]);var g=M(h,x);(n[i]=g).disjoint&&0<r[a].size&&console.log("WARNING: area "+i+" not represented on screen")}return n}function f(t,r,n){var e=[];return e.push("\nM",t,r),e.push("\nm",-n,0),e.push("\na",n,n,0,1,0,2*n,0),e.push("\na",n,n,0,1,0,2*-n,0),e.join(" ")}function V(t){var r=t.split(" ");return{x:parseFloat(r[1]),y:parseFloat(r[2]),radius:-parseFloat(r[4])}}function W(t){var r={};x(t,r);var n=r.arcs;if(0===n.length)return"M 0 0";if(1==n.length){var e=n[0].circle;return f(e.x,e.y,e.radius)}for(var a=["\nM",n[0].p2.x,n[0].p2.y],i=0;i<n.length;++i){var s=n[i],u=s.circle.radius,o=s.width>u;a.push("\nA",u,u,0,o?1:0,1,s.p1.x,s.p1.y)}return a.join(" ")}t.intersectionArea=x,t.circleCircleIntersection=k,t.circleOverlap=o,t.circleArea=I,t.distance=R,t.venn=i,t.greedyLayout=s,t.scaleSolution=_,t.normalizeSolution=S,t.bestInitialLayout=l,t.lossFunction=L,t.disjointCluster=d,t.distanceFromIntersectArea=T,t.VennDiagram=function(){var M=600,z=350,b=15,w=1e3,I=Math.PI/2,R=!0,P=!0,A=!0,F=null,O=null,n={},e=["#1f77b4","#ff7f0e","#2ca02c","#d62728","#9467bd","#8c564b","#e377c2","#7f7f7f","#bcbd22","#17becf"],a=0,q=function(t){if(t in n)return n[t];var r=n[t]=e[a];return e.length<=(a+=1)&&(a=0),r},j=i,k=L;function r(t){var r=t.datum(),n={};r.forEach(function(t){0==t.size&&1==t.sets.length&&(n[t.sets[0]]=1)}),r=r.filter(function(t){return!t.sets.some(function(t){return t in n})});var a={},e={};if(0<r.length){var i=j(r,{lossFunction:k});R&&(i=S(i,I,O)),a=_(i,M,z,b),e=G(a,r)}var s={};function u(t){return t.sets in s?s[t.sets]:1==t.sets.length?""+t.sets[0]:void 0}r.forEach(function(t){t.label&&(s[t.sets]=t.label)}),t.selectAll("svg").data([a]).enter().append("svg");var o=t.select("svg").attr("width",M).attr("height",z),f={},l=!1;o.selectAll(".venn-area path").each(function(t){var r=C.select(this).attr("d");1==t.sets.length&&r&&(l=!0,f[t.sets[0]]=V(r))});var h=function(t){return function(e){return W(t.sets.map(function(t){var r=f[t],n=a[t];return r||(r={x:M/2,y:z/2,radius:1}),n||(n={x:M/2,y:z/2,radius:1}),{x:r.x*(1-e)+n.x*e,y:r.y*(1-e)+n.y*e,radius:r.radius*(1-e)+n.radius*e}}))}},x=o.selectAll(".venn-area").data(r,function(t){return t.sets}),c=x.enter().append("g").attr("class",function(t){return"venn-area venn-"+(1==t.sets.length?"circle":"intersection")}).attr("data-venn-sets",function(t){return t.sets.join("_")}),g=c.append("path"),v=c.append("text").attr("class","label").text(function(t){return u(t)}).attr("text-anchor","middle").attr("dy",".35em").attr("x",M/2).attr("y",z/2);A&&(g.style("fill-opacity","0").filter(function(t){return 1==t.sets.length}).style("fill",function(t){return q(t.sets)}).style("fill-opacity",".25"),v.style("fill",function(t){return 1==t.sets.length?q(t.sets):"#444"}));var p=t;l?(p=t.transition("venn").duration(w)).selectAll("path").attrTween("d",h):p.selectAll("path").attr("d",function(t){return W(t.sets.map(function(t){return a[t]}))});var d=p.selectAll("text").filter(function(t){return t.sets in e}).text(function(t){return u(t)}).attr("x",function(t){return Math.floor(e[t.sets].x)}).attr("y",function(t){return Math.floor(e[t.sets].y)});P&&(l?"on"in d?d.on("end",N(a,u)):d.each("end",N(a,u)):d.each(N(a,u)));var y=x.exit().transition("venn").duration(w).remove();y.selectAll("path").attrTween("d",h);var m=y.selectAll("text").attr("x",M/2).attr("y",z/2);return null!==F&&(v.style("font-size","0px"),d.style("font-size",F),m.style("font-size","0px")),{circles:a,textCentres:e,nodes:x,enter:c,update:p,exit:y}}return r.wrap=function(t){return arguments.length?(P=t,r):P},r.width=function(t){return arguments.length?(M=t,r):M},r.height=function(t){return arguments.length?(z=t,r):z},r.padding=function(t){return arguments.length?(b=t,r):b},r.colours=function(t){return arguments.length?(q=t,r):q},r.fontSize=function(t){return arguments.length?(F=t,r):F},r.duration=function(t){return arguments.length?(w=t,r):w},r.layoutFunction=function(t){return arguments.length?(j=t,r):j},r.normalize=function(t){return arguments.length?(R=t,r):R},r.styled=function(t){return arguments.length?(A=t,r):A},r.orientation=function(t){return arguments.length?(I=t,r):I},r.orientationOrder=function(t){return arguments.length?(O=t,r):O},r.lossFunction=function(t){return arguments.length?(k=t,r):k},r},t.wrapText=N,t.computeTextCentres=G,t.computeTextCentre=M,t.sortAreas=function(t,n){for(var r=z(t.selectAll("svg").datum()),e={},a=0;a<n.sets.length;++a){var i=n.sets[a];for(var s in r)for(var u=r[s],o=0;o<u.length;++o)if(u[o]==i){e[s]=!0;break}}function f(t){for(var r=0;r<t.length;++r)if(!(t[r]in e))return!1;return!0}t.selectAll("g").sort(function(t,r){return t.sets.length!=r.sets.length?t.sets.length-r.sets.length:t==n?f(r.sets)?-1:1:r==n?f(t.sets)?1:-1:r.size-t.size})},t.circlePath=f,t.circleFromPath=V,t.intersectionAreaPath=W,Object.defineProperty(t,"__esModule",{value:!0})});
@aeshirey

This comment has been minimized.

Copy link
Owner Author

aeshirey commented Sep 13, 2018

This gist is available on rawgit via this url: https://cdn.rawgit.com/aeshirey/2d2817bb1548585fa7c72455928930db/raw/02db972c7ad95ee8b8ed8782159b84dd7b61bda1/venn.min.js

You can use it like this:

<!doctype html>
<html lang="en">
<body>
	<div id="venn"></div>
</body>

<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://cdn.rawgit.com/aeshirey/2d2817bb1548585fa7c72455928930db/raw/02db972c7ad95ee8b8ed8782159b84dd7b61bda1/venn.min.js"></script>
<script>
// define sets and set set intersections
var sets = [ {sets: ['A'], size: 12},
			 {sets: ['B'], size: 12},
			 {sets: ['A','B'], size: 2}];

var chart = venn.VennDiagram();
d3.select("#venn").datum(sets).call(chart);
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.