Published
Edited
Jun 29, 2020
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("id", "chart")
.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("path")
.data(data.series)
.join("path")
.attr("d", d => area(d.y_bounds))
.attr("data-index", (d, i) => i)
.attr("data-name", d => d.name)
.attr("fill", (d, i) => colors[i][0])
.attr("stroke", "none")
.style("opacity", 0.1)
.attr("class", "area");
svg.append("g")
.selectAll("path")
.data(data.series)
.join("path")
.attr("d", d => line(d.y))
.attr("data-index", (d, i) => i)
.attr("data-name", d => d.name)
.attr("fill", "none")
.attr("stroke", (d, i) => colors[i][0])
.attr("stroke-width", 1.5)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("class", "line");
svg.append("g")
.selectAll("text")
.data(lineLabels)
.enter().append("text")
.text(d => d.text)
.attr("fill", (d, i) => colors[i][1])
.attr("x", d => margin.left + (width * d.x))
.attr("y", d => margin.top + (height * d.y))
.attr("alignment-baseline", "middle")
.attr("text-anchor", "middle")
.attr("class", "xsmall-copy");
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: 10, bottom: 50, left: yTickSize})
Insert cell
yTickSize = 40
Insert cell
yLabelOffset = -yTickSize + 30
Insert cell
colors = [ // [light, dark] colors for each line
["#5C94D0", "#3775B8"], // multi-agent
["#C4C4D0", "#9191A4"], // baseline
["#E2AB43", "#C78D21"], // count-based
]
Insert cell
lineLabels = [
{text: "Multi-agent", x: 0.45, y: 0.22}, // x and y are percentages
{text: "Baseline", x: 0.50, y: 0.50},
{text: "Count-based", x: 0.30, y: 0.10},
]
Insert cell
xLabel = "Samples"
Insert cell
yLabel = "Normalized Reward"
Insert cell
Insert cell
x = d3.scaleLinear()
.domain([0, d3.max(data.x)])
.range([margin.left, width - margin.right])
Insert cell
y = d3.scaleLinear()
.domain([0, 1]).nice()
.range([height - margin.bottom, margin.top])
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x)
.ticks(width / 140)
.tickFormat(formatTick)
.tickSizeOuter(0))
.call(g => g.selectAll(".tick text") // tick labels
.attr("class", "xsmall-copy"))
// .call(g => g.select(".tick:first-of-type text").clone() // unit label
// .attr("y", 28)
// .attr("class", "xsmall-copy font-bold font-sans-serif")
// .text(`${xLabel}`))
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left + 1},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
area = d3.area()
// .defined(d => !isNaN(d))
.x((d, i) => x(data.x[i]))
.y0(d => y(d[0]))
.y1(d => y(d[1]))
Insert cell
line = d3.line()
// .defined(d => !isNaN(d))
.x((d, i) => x(data.x[i]))
.y(d => y(d))
Insert cell
data = {
const data = await d3.json("https://gist.githubusercontent.com/justinjaywang/f550f718980f2057b6e939c7f008e025/raw/f1b5463667faddc1f17f329beb215b971b347037/object_counting.json");

data.series = Object.keys(data).map(k => ({
name: k,
y: data[k].y,
y_bounds: [data[k].y_lower, data[k].y_upper],
}));
data.series.forEach(s => {
s.y_bounds = transpose(s.y_bounds)
}); // transpose y_bounds from 2 by l array into l by 2
data.x = data[Object.keys(data)[0]].x;
return data;
}
Insert cell
d3 = require("d3@5")
Insert cell
Insert cell
function formatTick(d) {
const s = (d / 1e9).toFixed(1);
const isFirst = this.parentNode.previousSibling.tagName != "g";
return isFirst ? `0 billion` : `${s}`;
}
Insert cell
transpose = {
return (array) => array[0].map((col, i) => array.map(row => row[i]));
}
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