histogramChart = {
const margin = { top: 30, right: 30, bottom: 50, left: 50 },
width = 600 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
const svg = d3.create("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", `translate(${margin.left}, ${margin.top})`);
const attributes = ["Displacement", "Length", "Beam", "Draft", "Speed", "Crew"];
let selectedAttribute = "Displacement";
const xScale = d3.scaleLinear().range([0, width]);
const yScale = d3.scaleLinear().range([height, 0]);
const xAxis = svg.append("g").attr("transform", `translate(0, ${height})`);
const yAxis = svg.append("g");
function updateHistogram(attribute) {
selectedAttribute = attribute;
const values = data_cleaned.map(d => +d[attribute]).filter(d => !isNaN(d));
const binGenerator = d3.bin()
.domain([d3.min(values), d3.max(values)])
.thresholds(10);
const bins = binGenerator(values);
xScale.domain([bins[0].x0, bins[bins.length - 1].x1]);
yScale.domain([0, d3.max(bins, d => d.length)]);
const bars = svg.selectAll("rect").data(bins);
bars.enter()
.append("rect")
.merge(bars)
.attr("x", d => xScale(d.x0))
.attr("width", d => xScale(d.x1) - xScale(d.x0) - 1)
.attr("y", d => yScale(d.length))
.attr("height", d => height - yScale(d.length))
.attr("fill", "steelblue");
bars.exit().remove();
xAxis.call(d3.axisBottom(xScale).ticks(10));
yAxis.call(d3.axisLeft(yScale));
}
const dropdown = d3.create("select")
.on("change", function() {
updateHistogram(this.value);
});
dropdown.selectAll("option")
.data(attributes)
.enter()
.append("option")
.text(d => d)
.attr("value", d => d);
updateHistogram(selectedAttribute);
return html`<div>${dropdown.node()}</div><div>${svg.node()}</div>`;
}