Published
Edited
Oct 19, 2020
Insert cell
Insert cell
Insert cell
Insert cell
data = d3.csvParse(await FileAttachment("stream-data.csv").text())
Insert cell
Insert cell
d3 = require("d3@6")
Insert cell
Insert cell
size = d3.min([document.documentElement.clientHeight*0.9,document.documentElement.clientWidth*0.9])
Insert cell
Insert cell
name = data.columns.slice(1)[2]
Insert cell
Insert cell
dimensions = ({ width: size,
height: size/3,
margin: {
top: 10,
right: 10,
left: 50,
bottom: 50,
}
})
Insert cell
Insert cell
dimensions.boundedWidth = dimensions.width - dimensions.margin.left - dimensions.margin.right
Insert cell
dimensions.boundedHeight = dimensions.height - dimensions.margin.top - dimensions.margin.bottom
Insert cell
Insert cell
xDomain = data.map( d=> d.year)
Insert cell
xScale = d3.scaleBand()
.domain(xDomain)
.range([0,dimensions.boundedWidth])
Insert cell
Insert cell
Insert cell
yDomain = data.map( d => d[name])
Insert cell
yScale = d3.scaleLinear()
.domain([0, d3.max(yDomain, s => +s)])
.range([dimensions.boundedHeight,0])
Insert cell
Insert cell
svg = d3.select(DOM.svg(dimensions.width,dimensions.height))
Insert cell
svg.node()
Insert cell
Insert cell
bounds = svg.append("g")
.style("transform", `translate(${dimensions.margin.left}px, ${dimensions.margin.top}px)`)
Insert cell
bounds.append("g")
.attr("fill", "steelblue")
.selectAll("rect")
.data(data)
.join("rect")
.attr("x", d => xScale(d.year))
.attr("y", d => yScale(d[name]))
.attr("height", d => dimensions.boundedHeight - yScale(d[name]))
.attr("width", xScale.bandwidth());
Insert cell
bounds.append("g")
.attr("transform", "translate("+ dimensions.margin.left + "," + "0" + ")")
.call(yAxisGen);
Insert cell
Insert cell
Insert cell
Insert cell
xAxisGen = d3.axisBottom(xScale)
.tickValues(xDomain.filter(function(d, i) {return !(i % 4);}))
Insert cell
Insert cell
yAxisGen = d3.axisLeft(yScale)
.ticks(9)
Insert cell
Insert cell
bounds.append("g")
.attr("transform", "translate("+ dimensions.margin.left + "," + (dimensions.boundedHeight) + ")")
.call(xAxisGen)
.selectAll("text")
.attr("dx", "-2em")
.attr("dy", ".15em")
.attr("transform", "rotate(-65)");
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