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")
.attr("class", "fill-red")
.selectAll("rect")
.data(data)
.join("rect")
.attr("x", d => x(d.name))
.attr("y", d => y(d.value))
.attr("height", d => y(0) - y(d.value))
.attr("width", x.bandwidth());

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 = 60
Insert cell
yLabelOffset = -yTickSize + 30
Insert cell
xLabel = "Samples"
Insert cell
yLabel = "Normalized Reward"
Insert cell
Insert cell
x = d3.scaleBand()
.domain(data.map(d => d.name))
.range([margin.left, width - margin.right])
.padding(0.1)
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.value)]).nice()
.range([height - margin.bottom, margin.top])
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).tickSizeOuter(0))
.call(g => g.selectAll(".tick text") // tick labels
.attr("class", "xsmall-copy"))
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 = (await d3.csv("https://gist.githubusercontent.com/mbostock/81aa27912ad9b1ed577016797a780b2c/raw/3a807eb0cbb0f5904053ac2f9edf765e2f87a2f5/alphabet.csv", ({letter, frequency}) => ({name: letter, value: +frequency}))).sort((a, b) => b.value - a.value)
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