{
const width = 600;
const height = 400;
const marginTop = 20;
const marginRight = 20;
const marginBottom = 40;
const marginLeft = 40;
const x = d3.scaleLinear()
.domain([0, d3.max(survivorshipData, d => d.age)])
.range([marginLeft, width - marginRight])
const y = d3.scaleLinear()
.domain([0, d3.max(survivorshipData, d => d.fare)])
.range([height - marginBottom, marginTop]);
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [0, 0, width, height])
.attr("style", "max-width: 100%; height: auto;");
svg.append("g")
.attr("transform", `translate(0,${height - marginBottom})`)
.call(d3.axisBottom(x));
svg.append("g")
.attr("transform", `translate(${marginLeft},0)`)
.call(d3.axisLeft(y));
svg.append("text")
.attr("x", width / 2)
.attr("y", height - 5)
.attr("text-anchor", "middle")
.style("font-size", "14px")
.text("Age");
svg.append("text")
.attr("transform", "rotate(-90)")
.attr("x", -height / 2)
.attr("y", 10)
.attr("text-anchor", "middle")
.style("font-size", "14px")
.text("Fare");
var points = svg.selectAll("g")
.data(survivorshipData)
.enter()
.append("path");
var symbol = d3.symbol();
points.attr("d", symbol
.type(d => d.survived === 1 ? d3.symbolCircle : d3.symbolCross)
.size(50))
.attr("transform", d => `translate(${x(d.age)}, ${y(d.fare)})`)
.attr("fill", d => d.survived === 1 ? "steelblue" : "tomato");
const legend = svg.append("g")
.attr("transform", `translate(${marginLeft + (width / 10)},${marginTop})`);
const legendData = [
{ label: "Survived", shape: d3.symbolCircle, color: "steelblue" },
{ label: "Died", shape: d3.symbolCross, color: "tomato" }
];
const legendItem = legend.selectAll("g")
.data(legendData)
.enter()
.append("g")
.attr("transform", (d, i) => `translate(0, ${i * 20})`);
legendItem.append("path")
.attr("d", d => d3.symbol().type(d.shape).size(50)())
.attr("fill", d => d.color);
legendItem.append("text")
.attr("x", 10)
.attr("y", 5)
.text(d => d.label)
.style("font-size", "12px")
.attr("alignment-baseline", "middle");
return svg.node();
}