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

svg.append("g")
.style("font-size", "1rem")
.call(xAxis)
.append("text")
.attr("x", (width - yTickSize) / 2)
.attr("y", 40)
.attr("class", "xsmall-copy font-bold font-sans-serif")
.attr("fill", "currentColor")
.attr("text-anchor", "middle")
.text(`${xLabel}`);

// svg.append("g")
// .style("font-size", "1rem")
// .call(yAxis);
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 > 800) {
return 400 + margin.top + margin.bottom
} else {
return width * 1/2 + margin.top + margin.bottom
}
}
Insert cell
margin = ({top: 20, right: 0, bottom: 50, left: yTickSize})
Insert cell
yTickSize = 0
Insert cell
yLabelOffset = -yTickSize + 32
Insert cell
xLabel = "Fruit"
Insert cell
yLabel = "People"
Insert cell
color = d3.scaleOrdinal()
.range(["#5C94D0", "#E2AB43"])
Insert cell
darkColor = d3.scaleOrdinal()
.range(["#3775B8", "#C78D21"])
Insert cell
Insert cell
x0 = d3.scaleBand()
.domain(data.map(d => d[groupKey]))
.rangeRound([margin.left, width - margin.right])
.paddingInner(0.25) // spacing between groups
Insert cell
x1 = d3.scaleBand()
.domain(keys)
.rangeRound([0, x0.bandwidth()])
// .padding(0.05) // spacing within a group
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(data, d => d3.max(keys, key => d[key]))]).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))
.call(g => g.selectAll(".tick text") // tick labels
.attr("class", "xsmall-copy"))
// .call(g => g.select(".domain").remove())
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.attr("stroke-opacity", 0.05)
.call(d3.axisLeft(y)
.ticks(height / 100)
.tickSize(yTickSize)) // left axis
.call(g => g.selectAll(".tick line").clone() // grid lines
.attr("stroke-opacity", 0.05)
.attr("x2", width - margin.left - margin.right))
.call(g => g.select(".tick:first-of-type") // first tick opacity
.attr("stroke-opacity", 1))
.call(g => g.selectAll(".tick text") // tick labels
.style("text-anchor", "start")
.attr("x", -yTickSize)
.attr("y", -9)
.attr("class", "xsmall-copy"))
.call(g => g.select(".domain").remove()) // remove domain line
.call(g => g.select(".tick:last-of-type text").clone() // unit label
.attr("x", `${yLabelOffset}`)
.attr("text-anchor", "start")
.attr("class", "xsmall-copy font-bold font-sans-serif")
.text(`${yLabel}`))
// .call(g => g.select(".tick:first-of-type text") // first tick
// .remove())
Insert cell
bars = g => {
g.append("rect")
.attr("x", d => x1(d.key))
.attr("y", d => y(d.value))
.attr("width", x1.bandwidth())
.attr("height", d => y(0) - y(d.value))
.attr("fill", d => color(d.key));
g.append("text")
.attr("x", d => x1(d.key) + x1.bandwidth() / 2)
.attr("y", d => y(d.value) - 8)
.attr("text-anchor", "middle")
.attr("fill", d => darkColor(d.key))
.attr("class", "xsmall-copy label")
.text(d => 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(`${yLabel}`))

Insert cell
data = Object.assign(await d3.csv("https://gist.githubusercontent.com/justinjaywang/2e99bcaf9278dae7565fd9c020188f44/raw/e983d0d3b11b65e23437921635b12925fe67829a/batch-size-test.csv", 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