chart = {
let data = simulate(pop)
let height = 500
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);
let margin = ({top: 20, right: 20, bottom: 30, left: 40})
let x = d3.scaleLinear()
.domain([80,660])
.range([margin.left, width - margin.right])
let bins = d3.histogram()
.domain(x.domain())
.thresholds(x.ticks((x.domain()[1] - x.domain()[0])/10))
(data)
let megabins = d3.histogram()
.domain(x.domain())
.thresholds([100,200,300,400,500,600])
(data)
console.log(megabins)
let xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(width / 80 ).tickSizeOuter(0))
.call(g => g.append("text")
.attr("x", width - margin.right)
.attr("y", -4)
.attr("fill", "currentColor")
.attr("font-weight", "bold")
.attr("text-anchor", "end")
.text(data.x))
let y = d3.scaleLinear()
.domain([0,iterations/2])
.range([height - margin.bottom, margin.top])
let yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).ticks(height / 40).tickFormat(d => `${Math.ceil(100*(d/iterations))}%`))
.call(g => g.select(".domain").remove())
.call(g => g.select(".tick:last-of-type text").clone()
.attr("x", 4)
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text(data.y))
let color = d3.scaleLinear()
.domain([0,1])
.range([d3.color("rgba(255, 201, 38,0)"),d3.color("rgba(255, 201, 38,0.8)")])
svg.append("g")
.selectAll('rect')
.data(megabins)
.join("rect")
.attr('x', d => x(d.x0) + 1)
.attr('width',d => Math.max(0,x(d.x1) - x(d.x0) - 1))
.attr('y',y.range()[1])
.attr('height',y(y.domain()[0]))
.attr('fill',d => color(d.length/data.length))
svg.append('g')
.selectAll('text')
.data(megabins)
.join('text')
.attr('class','label')
.attr('x', d => x(d.x0) + 4)
.attr('width',d => Math.max(0,x(d.x1) - x(d.x0) - 1))
.attr('y',40)
.text(d => {
return `${Math.round(d.length/data.length*100)}%`
})
svg.append("g")
.attr("fill", "steelblue")
.selectAll("rect")
.data(bins)
.join("rect")
.attr("x", d => x(d.x0) + 1)
.attr("width", d => Math.max(0, x(d.x1) - x(d.x0) - 1))
.attr("y", d => y(d.length))
.attr("height", d => y(0) - y(d.length));
svg.append("g")
.call(xAxis);
svg.append("g")
.call(yAxis);
return svg.node();
}