Public
Edited
Apr 9
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
metrics = data.columns.filter(d => d.startsWith("std_mean_"));
Insert cell
metrics.forEach(m => {
svg.selectAll(".axis")
.filter(d => d === m)
.call(d3.brushY()
.extent([[-10, 0], [10, height]])
.on("brush", brushed)
);
});
Insert cell
yScales = new Map(metrics.map(m => [
m,
d3.scaleLinear()
.domain(d3.extent(data, d => d[m]))
.range([height - marginBottom, marginTop])
]));
Insert cell
xScale = d3.scalePoint()
.domain(metrics)
.range([marginLeft, width - marginRight]);
Insert cell
colorScale = d3.scaleOrdinal(d3.schemeCategory10)
.domain(data.map(d => d.diet_group).sort());
Insert cell
svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [0, 0, width, height]);
Insert cell
svg.selectAll(".axis")
.data(metrics)
.enter()
.append("g")
.attr("class", "axis")
.attr("transform", d => `translate(${xScale(d)}, 0)`)
.each(function(d) { d3.select(this).call(d3.axisLeft(yScales.get(d))); })
.append("text")
.attr("y", marginTop - 10)
.attr("x", 0)
.attr("text-anchor", "middle")
.attr("fill", "black")
.text(d => d.replace("std_mean_", ""));
Insert cell
path = d => d3.line()(metrics.map(m => [xScale(m), yScales.get(m)(d[m])]));
Insert cell
lines = svg.append("g")
.selectAll("path")
.data(data)
.enter()
.append("path")
.attr("d", path)
.style("fill", "none")
.style("stroke", d => colorScale(d.diet_group))
.style("opacity", 0.5);
Insert cell
tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("position", "absolute")
.style("background", "lightgray")
.style("border", "1px solid black")
.style("padding", "5px")
.style("border-radius", "5px")
.style("visibility", "hidden");
Insert cell
lines.on("mouseover", function(event, d) {
d3.select(this).style("opacity", 1);
tooltip.style("visibility", "visible")
.html(`Sex: ${d.sex}<br>Diet: ${d.diet_group}<br>Age: ${d.age_group}`);
})
.on("mousemove", function(event) {
tooltip.style("top", (event.pageY - 10) + "px")
.style("left", (event.pageX + 10) + "px");
})
.on("mouseout", function() {
d3.select(this).style("opacity", 0.5);
tooltip.style("visibility", "hidden");
});
Insert cell
function brushed(event, d) {
const selection = event.selection;
if (selection) {
const [minY, maxY] = selection.map(yScales.get(d).invert);
svg.selectAll("path")
.style("stroke", p => (p[d] >= minY && p[d] <= maxY ? "#69b3a2" : "#ccc"));
} else {
svg.selectAll("path").style("stroke", "#69b3a2");
}
}
Insert cell
svg.append("text")
.attr("x", width / 5)
.attr("y", height - marginBottom + 20)
.style("font-size", "20px")
.text("Parallel Coordinates Plot of Standardized Environmental Metrics");
Insert cell
function updatePlot(selectedSex, selectedDiet, selectedAge) {
lines.style("display", d =>
(!selectedSex || d.sex === selectedSex) &&
(!selectedDiet || d.diet_group === selectedDiet) &&
(!selectedAge || d.age_group === selectedAge) ? "block" : "none"
);
}
Insert cell
sexSelect.addEventListener("change", function() {
updatePlot(this.value, dietSelect.value, ageSelect.value);
});
Insert cell
dietSelect.addEventListener("change", function() {
updatePlot(sexSelect.value, this.value, ageSelect.value);
});
Insert cell
ageSelect.addEventListener("change", function() {
updatePlot(sexSelect.value, dietSelect.value, this.value);
});
Insert cell
Insert cell
Insert cell
Insert cell
html`<div>
<label>Select Sex:</label> ${sexSelect}<br>
<label>Select Diet:</label> ${dietSelect}<br>
<label>Select Age Group:</label> ${ageSelect}
</div>`
Insert cell
html`<div>${svg.node()}</div>`
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more