Published
Edited
Jun 29, 2020
1 fork
Insert cell
Insert cell
chart = {
// const svg = d3.select(DOM.svg(width, height));
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)`)
.selectAll("rect")
.data(d => keys.map(key => ({key, value: d[key]})))
.join("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));

return svg.node();
}
Insert cell
Insert cell
height = {
if (width > 800) {
return 450 + margin.top + margin.bottom
} else {
return width * 9/16 + margin.top + margin.bottom
}
}
Insert cell
margin = ({top: 20, right: 0, bottom: 50, left: yTickSize})
Insert cell
yTickSize = 40
Insert cell
yLabelOffset = -yTickSize + 32
Insert cell
xLabel = "Fruit"
Insert cell
yLabel = "People"
Insert cell
Insert cell
x0 = d3.scaleBand()
.domain(data.map(d => d[groupKey]))
.rangeRound([margin.left, width - margin.right])
.paddingInner(0.2) // 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
color = d3.scaleOrdinal()
.range(["#98abc5", "#8a89a6"])
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
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

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