Published
Edited
Jun 29, 2020
Importers
1 star
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("class", "color-cool-gray-3");

svg.append("g")
.style("font-size", "1rem")
.call(xAxis)
.append("text")
.attr("x", width / 2)
.attr("y", 40)
.attr("class", "xsmall-copy")
.attr("fill", "currentColor")
.attr("text-anchor", "middle")
.text(xLabel);
svg.append("g")
.selectAll("g")
.data(data)
.join("g")
.attr("transform", d => `translate(${x0(d[groupKey])},0)`)
.attr("data-index", (d, i) => i)
.selectAll("rect")
.data(d => keys.map(key => ({key, value: d[key]})))
.join("g")
.call(bars);
svg.select("g[data-index='0']")
.call(units);

return svg.node();
}
Insert cell
Insert cell
height = {
if (width > 720) {
return 240 + margin.bottom
} else {
return width * 1/3 + margin.bottom
}
}
Insert cell
margin = ({top: 40, right: 0, bottom: 50, left: 0})
Insert cell
xLabel = "Batch Size"
Insert cell
color = d3.scaleOrdinal()
.range(["#27B5EA", "#F4AC36"])
Insert cell
darkColor = d3.scaleOrdinal()
.range(["#0A9FD7", "#D68E18"])
Insert cell
paddingInner = 0.2
Insert cell
Insert cell
x0 = d3.scaleBand()
.domain(data.map(d => d[groupKey]))
.rangeRound([margin.left, width - margin.right])
.paddingInner(paddingInner) // spacing between groups
Insert cell
x1 = d3.scaleBand()
.domain(keys)
.rangeRound([0, x0.bandwidth()])
// .padding(0.05) // spacing within a group
Insert cell
y0 = d3.scaleLinear()
.domain([0, d3.max(data, d => d[keys[0]])])//.nice()
.rangeRound([height - margin.bottom, margin.top])
Insert cell
y1 = d3.scaleLinear()
.domain([0, d3.max(data, d => d[keys[1]])])//.nice()
.rangeRound([height - margin.bottom, margin.top])
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x0)
.tickSizeOuter(0)
.tickFormat(formatTick))
.call(g => g.selectAll(".tick text") // tick labels
.attr("class", "xsmall-copy"))
// .call(g => g.select(".domain").remove())
Insert cell
function formatTick(d) {
return (d / 1e3).toFixed(0) + "k";
}
Insert cell
bars = g => {
g.append("rect")
.attr("x", d => x1(d.key))
.attr("y", d => (d.key == keys[0]) ? y0(d.value) : y1(d.value))
.attr("width", x1.bandwidth())
.attr("height", d => (d.key == keys[0]) ? y0(0) - y0(d.value) : y1(0) - y1(d.value)) // conditional scale
.attr("fill", d => color(d.key));
g.append("text")
.attr("x", d => x1(d.key) + x1.bandwidth() / 2)
.attr("y", d => (d.key == keys[0]) ? y0(d.value) - 8 : y1(d.value) - 8)
.attr("text-anchor", "middle")
.attr("fill", d => darkColor(d.key))
.attr("class", "xsmall-copy label")
.text(d => (d.key == keys[0]) ? d3.format(".4s")(d.value) : d3.format(".1f")(d.value));
}
Insert cell
units = g => g
.call(g => g.selectAll("text") // text
.attr("dy", "-16")
.clone() // unit labels
.attr("dy", "0")
.attr("class", "xsmall-copy font-bold font-sans-serif")
.text(d => d.key))

Insert cell
file = "https://d4mucfpksywv.cloudfront.net/emergent-tool-use/data/batch-size-20190905a.csv"
Insert cell
data = Object.assign(await d3.csv(file, d3.autoType))
Insert cell
groupKey = data.columns[0]
Insert cell
keys = data.columns.slice(1)
Insert cell
d3 = require("d3@5")
Insert cell
Insert cell
openaicss = {
return html`<link rel="stylesheet" type="text/css" href="https://cdn.openai.com/miscellaneous/observable-1.3.5/styles/openai-css.css">`
}
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