chart = {
const svg = d3
.create("svg")
.attr("width", width)
.attr("height", height);
const y_axis = d3
.axisLeft()
.scale(y_scale)
.ticks(4, "f");
svg
.append('g')
.call(y_axis)
.attr('transform', 'translate(' + margin.left + ', 0)');
const x_axis = d3.axisBottom().scale(x_scale);
svg
.append('g')
.call(x_axis)
.attr('transform', 'translate(0, ' + (height - margin.bottom) + ')');
svg
.append("text")
.attr("text-anchor", "end")
.attr("x", width - margin.right)
.attr("y", height - margin.top)
.text("Age");
svg
.append("text")
.attr("text-anchor", "middle")
.attr("x", width / 2)
.attr("y", margin.top - 20)
.text("Resting Blood Pressure by Age & Gender");
svg
.append("text")
.attr("text-anchor", "end")
.attr("transform", "rotate(-90)")
.attr("y", margin.left - 30)
.attr("x", -margin.left)
.text("Resting Blood Pressure");
const dots = svg
.append('g')
.selectAll('dot')
.data(chart_data)
.enter()
.append('circle')
.attr('cx', d => x_scale(0))
.attr('cy', d => y_scale(0))
.attr('r', 4)
.style('fill', d => {
if (d["color"] === "male") {
return "blue";
} else {
return "orange";
}
})
.style('opacity', 0.4)
.transition()
.duration(500)
.attr('cx', d => x_scale(d['x']))
.attr('cy', d => y_scale(d['y']));
return svg.node();
}