Published unlisted
Edited
Feb 28, 2020
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.select(DOM.svg(width, height))
.attr("id", "chart");
svg.append("g")
.attr("id", "x-axis")
.attr("transform", `translate(${margin.left}, ${height - margin.bottom})`)
.call(xaxis);
svg.append("g")
.attr("id", "y-axis")
.attr("transform", `translate(${margin.left}, ${margin.top})`)
.call(yaxis);
svg.append("g")
.attr("id", "bars")
.attr("transform", `translate(${margin.left}, ${margin.top})`)
.selectAll("rect")
.data(letters)
.enter()
.append("rect")
.attr("x", d => x(d.letter))
.attr("y", d => y(d.frequency))
.attr("width", x.bandwidth())
.attr("height", d => innerHeight - y(d.frequency));
return svg.node();
}
Insert cell
bars = d3.select(chart).select("g#bars").selectAll("rect");
Insert cell
brush = {
const svg = d3.select("svg#chart");
bars.on("mouseover.brush", function(d) {
bars.filter(e => (d.letter !== e.letter))
.transition()
.style("fill", "#bbbbbb");
//.style("fill-opacity", "50%");
});
bars.on("mouseout.brush", function(d) {
bars.transition()
.style("fill", "");
//.style("fill-opacity", "100%");
});
}
Insert cell
line = {
const svgChart = d3.select("svg#chart");
bars.on("mouseover.line", function(d) {
svgChart.append("line")
.attr("id", "refLine")
.attr("x1", 0)
.attr("x2", innerWidth + margin.left)
.attr("y1", y(d.frequency))
.attr("y2", y(d.frequency))
.style("stroke", "red")
.style("stroke-dasharray", 2)
.attr("transform", `translate(${margin.left}, ${margin.top})`);
});
bars.on("mouseout.brush", function(d) {
svgChart.selectAll("line").remove();
});
}
Insert cell
Insert cell
x = d3.scaleBand()
.domain(letters.map(d => d.letter))
.range([0, innerWidth])
.paddingInner(0.2)
.paddingOuter(0.1);
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(letters, d => d.frequency)])
.range([innerHeight, 0])
.nice();
Insert cell
xaxis = d3.axisBottom(x)
.tickSize(0)
.tickPadding(5);
Insert cell
yaxis = d3.axisLeft(y)
.tickValues(y.domain())
.tickPadding(5);
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more