Published
Edited
Oct 31, 2020
Insert cell
Insert cell
html`In a class of N_ALL students, each of the students likes either sports or learning English or both. N_BOTH students like both, N_A like sports and N_B like learning English. `
Insert cell
N_ALL = 25
Insert cell
N_A = 7
Insert cell
N_B = 12
Insert cell
N_BOTH = 0.5
Insert cell
d3 = require('d3@5')
Insert cell
Insert cell
R_MAX = 400
Insert cell
rA = N_A / N_ALL * R_MAX
Insert cell
rB = N_B / N_ALL * R_MAX
Insert cell
rBoth = N_BOTH / N_ALL * R_MAX
Insert cell
delta = (rA + rB) / 10
Insert cell
html`d_A_B: Distance from center of A to center of B`
Insert cell
Insert cell
Insert cell
kA = (N_BOTH / N_A) * Math.PI
Insert cell
kB = (N_BOTH / N_B) * Math.PI
Insert cell
function getTheta (k) {
let theta = 0;
let minDetal = 1000;
for(let i=0; i< Math.PI; i=i+0.1){
const detal = Math.abs(i - Math.sin(i) -k);
if(detal < minDetal){
minDetal = k;
theta = i;
}
}
return theta
}
Insert cell
theta_a = getTheta(kA)
Insert cell
theta_b = getTheta(kB)
Insert cell
d1 = Math.cos(theta_a /2 ) * rA
Insert cell
d2 = Math.cos(theta_b / 2) * rB
Insert cell
d = d1 + d2
Insert cell
lRect = rA + rB + d + 2 * delta
Insert cell
wRect = 2 * d3.max([rA, rB]) + 2 * delta
Insert cell
Insert cell
function draw_venn(){
const node = DOM.svg(lRect, wRect);
const svg = d3.select(node);
svg.attr('style', 'border: 1px solid salmon');
svg.append('circle').attr('r', rA).attr('cx', rA + delta).attr('cy', d3.max([rA, rB]) + delta).attr("fill", 'rgb(31, 119, 180)').attr("fill-opacity", 0.25).attr("stroke", 'steelblue').attr("stroke-opacity", 0.55).attr("stroke-width", 3)
svg.append('circle').attr('r', rB).attr('cx', delta + rA + d).attr('cy', d3.max([rA, rB]) + delta).attr("fill", 'rgb(255, 127, 14)').attr("fill-opacity", 0.25).attr("stroke", 'orange').attr("stroke-opacity", 0.55).attr("stroke-width", 3)
svg.append('text').text("A").attr('x', rA + delta).attr('y', d3.max([rA, rB]) + delta).attr("value", "ABC").attr("color", "white").attr("font-size", "30px")
svg.append('text').text("B").attr('x', lRect - rB - delta).attr('y', d3.max([rA, rB]) + delta).attr("value", "ABC").attr("color", "white").attr("font-size", "30px")
return svg.node();
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more